def __call__(self, argv, known_only=False): """Parses flags from argv; stores parsed flags into this FlagValues object. All unparsed arguments are returned. Args: argv: a tuple/list of strings. known_only: bool, if True, parse and remove known flags; return the rest untouched. Unknown flags specified by --undefok are not returned. Returns: The list of arguments not parsed as options, including argv[0]. Raises: Error: Raised on any parsing error. TypeError: Raised on passing wrong type of arguments. ValueError: Raised on flag value parsing error. """ if _helpers.is_bytes_or_string(argv): raise TypeError( 'argv should be a tuple/list of strings, not bytes or string.') if not argv: raise ValueError( 'argv cannot be an empty list, and must contain the program name as ' 'the first element.') # This pre parses the argv list for --flagfile=<> options. program_name = argv[0] args = self.read_flags_from_files(argv[1:], force_gnu=False) # Parse the arguments. unknown_flags, unparsed_args = self._parse_args(args, known_only) # Handle unknown flags by raising UnrecognizedFlagError. # Note some users depend on us raising this particular error. for name, value in unknown_flags: suggestions = _helpers.get_flag_suggestions(name, list(self)) raise _exceptions.UnrecognizedFlagError(name, value, suggestions=suggestions) self.mark_as_parsed() self._assert_all_validators() return [program_name] + unparsed_args
def __call__(self, argv, known_only=False): """Parses flags from argv; stores parsed flags into this FlagValues object. All unparsed arguments are returned. Args: argv: a tuple/list of strings. known_only: bool, if True, parse and remove known flags; return the rest untouched. Unknown flags specified by --undefok are not returned. Returns: The list of arguments not parsed as options, including argv[0]. Raises: Error: Raised on any parsing error. TypeError: Raised on passing wrong type of arguments. ValueError: Raised on flag value parsing error. """ if _helpers.is_bytes_or_string(argv): raise TypeError( 'argv should be a tuple/list of strings, not bytes or string.') if not argv: raise ValueError( 'argv cannot be an empty list, and must contain the program name as ' 'the first element.') # This pre parses the argv list for --flagfile=<> options. program_name = argv[0] args = self.read_flags_from_files(argv[1:], force_gnu=False) # Parse the arguments. unknown_flags, unparsed_args = self._parse_args(args, known_only) # Handle unknown flags by raising UnrecognizedFlagError. # Note some users depend on us raising this particular error. for name, value in unknown_flags: suggestions = _helpers.get_flag_suggestions(name, list(self)) raise _exceptions.UnrecognizedFlagError( name, value, suggestions=suggestions) self.mark_as_parsed() self._assert_all_validators() return [program_name] + unparsed_args
def test_list(self): self.assertFalse(_helpers.is_bytes_or_string(['str']))
def test_unicode(self): self.assertTrue(_helpers.is_bytes_or_string(u'unicode'))
def test_str(self): self.assertTrue(_helpers.is_bytes_or_string('str'))
def test_bytes(self): self.assertTrue(_helpers.is_bytes_or_string(b'bytes'))