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_suggestions_are_sorted(self): sorted_flags = sorted(['aab', 'aac', 'aad']) misspelt_flag = 'aaa' suggestions = _helpers.get_flag_suggestions(misspelt_flag, reversed(sorted_flags)) self.assertEqual(sorted_flags, suggestions)
def test_crazy_suggestion(self): suggestions = _helpers.get_flag_suggestions('asdfasdgasdfa', self.longopts) self.assertEqual([], suggestions)
def test_misspelled_ambiguous_prefix_suggestion(self): suggestions = _helpers.get_flag_suggestions('stack', self.longopts) self.assertEqual(['fstack-protector', 'fstack-protector-all'], suggestions)
def test_mispelled_suggestions(self): suggestions = _helpers.get_flag_suggestions('fstack_protector_all', self.longopts) self.assertEqual(['fstack-protector-all'], suggestions)