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

Function _ParseArgs

fire/core.py:757–813  ·  view source on GitHub ↗

Parses the positional and named arguments from the available supplied args. Modifies kwargs, removing args as they are used. Args: fn_args: A list of argument names that the target function accepts, including positional and named arguments, but not the varargs or kwargs nam

(fn_args, fn_defaults, num_required_args, kwargs,
               remaining_args, metadata)

Source from the content-addressed store, hash-verified

755
756
757def _ParseArgs(fn_args, fn_defaults, num_required_args, kwargs,
758 remaining_args, metadata):
759 """Parses the positional and named arguments from the available supplied args.
760
761 Modifies kwargs, removing args as they are used.
762
763 Args:
764 fn_args: A list of argument names that the target function accepts,
765 including positional and named arguments, but not the varargs or kwargs
766 names.
767 fn_defaults: A list of the default values in the function argspec.
768 num_required_args: The number of required arguments from the function's
769 argspec. This is the number of arguments without a default value.
770 kwargs: Dict with named command line arguments and their values.
771 remaining_args: The remaining command line arguments, which may still be
772 used as positional arguments.
773 metadata: Metadata about the function, typically from Fire decorators.
774 Returns:
775 parsed_args: A list of values to be used as positional arguments for calling
776 the target function.
777 kwargs: The input dict kwargs modified with the used kwargs removed.
778 remaining_args: A list of the supplied args that have not been used yet.
779 capacity: Whether the call could have taken args in place of defaults.
780 Raises:
781 FireError: If additional positional arguments are expected, but none are
782 available.
783 """
784 accepts_positional_args = metadata.get(decorators.ACCEPTS_POSITIONAL_ARGS)
785 capacity = False # If we see a default get used, we'll set capacity to True
786
787 # Select unnamed args.
788 parsed_args = []
789 for index, arg in enumerate(fn_args):
790 value = kwargs.pop(arg, None)
791 if value is not None: # A value is specified at the command line.
792 value = _ParseValue(value, index, arg, metadata)
793 parsed_args.append(value)
794 else: # No value has been explicitly specified.
795 if remaining_args and accepts_positional_args:
796 # Use a positional arg.
797 value = remaining_args.pop(0)
798 value = _ParseValue(value, index, arg, metadata)
799 parsed_args.append(value)
800 elif index < num_required_args:
801 raise FireError(
802 'The function received no value for the required argument:', arg)
803 else:
804 # We're past the args for which there's no default value.
805 # There's a default value for this arg.
806 capacity = True
807 default_index = index - num_required_args # index into the defaults.
808 parsed_args.append(fn_defaults[default_index])
809
810 for key, value in kwargs.items():
811 kwargs[key] = _ParseValue(value, None, key, metadata)
812
813 return parsed_args, kwargs, remaining_args, capacity
814

Callers 1

_ParseFnFunction · 0.85

Calls 2

_ParseValueFunction · 0.85
FireErrorClass · 0.85

Tested by

no test coverage detected