MCPcopy
hub / github.com/google/python-fire / _ParseKeywordArgs

Function _ParseKeywordArgs

fire/core.py:816–933  ·  view source on GitHub ↗

Parses the supplied arguments for keyword arguments. Given a list of arguments, finds occurrences of --name value, and uses 'name' as the keyword and 'value' as the value. Constructs and returns a dictionary of these keyword arguments, and returns a list of the remaining arguments. Only if

(args, fn_spec)

Source from the content-addressed store, hash-verified

814
815
816def _ParseKeywordArgs(args, fn_spec):
817 """Parses the supplied arguments for keyword arguments.
818
819 Given a list of arguments, finds occurrences of --name value, and uses 'name'
820 as the keyword and 'value' as the value. Constructs and returns a dictionary
821 of these keyword arguments, and returns a list of the remaining arguments.
822
823 Only if fn_keywords is None, this only finds argument names used by the
824 function, specified through fn_args.
825
826 This returns the values of the args as strings. They are later processed by
827 _ParseArgs, which converts them to the appropriate type.
828
829 Args:
830 args: A list of arguments.
831 fn_spec: The inspectutils.FullArgSpec describing the given callable.
832 Returns:
833 kwargs: A dictionary mapping keywords to values.
834 remaining_kwargs: A list of the unused kwargs from the original args.
835 remaining_args: A list of the unused arguments from the original args.
836 Raises:
837 FireError: If a single-character flag is passed that could refer to multiple
838 possible args.
839 """
840 kwargs = {}
841 remaining_kwargs = []
842 remaining_args = []
843 fn_keywords = fn_spec.varkw
844 fn_args = fn_spec.args + fn_spec.kwonlyargs
845
846 if not args:
847 return kwargs, remaining_kwargs, remaining_args
848
849 skip_argument = False
850
851 for index, argument in enumerate(args):
852 if skip_argument:
853 skip_argument = False
854 continue
855
856 if _IsFlag(argument):
857 # This is a named argument. We get its value from this arg or the next.
858
859 # Terminology:
860 # argument: A full token from the command line, e.g. '--alpha=10'
861 # stripped_argument: An argument without leading hyphens.
862 # key: The contents of the stripped argument up to the first equal sign.
863 # "shortcut flag": refers to an argument where the key is just the first
864 # letter of a longer keyword.
865 # keyword: The Python function argument being set by this argument.
866 # value: The unparsed value for that Python function argument.
867 contains_equals = '=' in argument
868 stripped_argument = argument.lstrip('-')
869 if contains_equals:
870 key, value = stripped_argument.split('=', 1)
871 else:
872 key = stripped_argument
873 value = None # value will be set later on.

Callers 2

_IsHelpShortcutFunction · 0.85
_ParseFnFunction · 0.85

Calls 2

_IsFlagFunction · 0.85
FireErrorClass · 0.85

Tested by

no test coverage detected