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: argument list. Can be of any type that may be converted to a list. known_only: parse and remove known flags, return rest untouched. Returns: The list of arguments not parsed as options, including argv[0]. Raises: Error: on any parsing error. ValueError: on flag value parsing error. """ if not argv: # Unfortunately, the old parser used to accept an empty argv, and some # users rely on that behaviour. Allow it as a special case for now. self.MarkAsParsed() self._AssertAllValidators() return [] # This pre parses the argv list for --flagfile=<> options. program_name = argv[0] args = self.ReadFlagsFromFiles(argv[1:], force_gnu=False) # Parse the arguments. unknown_flags, unparsed_args, undefok = self._ParseArgs( 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: if name in undefok: continue suggestions = _helpers.GetFlagSuggestions(name, self.RegisteredFlags()) raise exceptions.UnrecognizedFlagError(name, value, suggestions=suggestions) self.MarkAsParsed() self._AssertAllValidators() return [program_name] + unparsed_args
def testCrazySuggestion(self): suggestions = _helpers.GetFlagSuggestions('asdfasdgasdfa', self.longopts) self.assertEquals([], suggestions)
def testMisspelledAmbiguousPrefixSuggestion(self): suggestions = _helpers.GetFlagSuggestions('stack', self.longopts) self.assertEquals(['fstack-protector', 'fstack-protector-all'], suggestions)
def testMispelledSuggestions(self): suggestions = _helpers.GetFlagSuggestions('fstack_protector_all', self.longopts) self.assertEquals(['fstack-protector-all'], suggestions)