This function, Fire, is the main entrypoint for Python Fire. Executes a command either from the `command` argument or from sys.argv by recursively traversing the target object `component`'s members consuming arguments, evaluating functions, and instantiating classes as it goes. When buildi
(component=None, command=None, name=None, serialize=None)
| 71 | |
| 72 | |
| 73 | def Fire(component=None, command=None, name=None, serialize=None): |
| 74 | """This function, Fire, is the main entrypoint for Python Fire. |
| 75 | |
| 76 | Executes a command either from the `command` argument or from sys.argv by |
| 77 | recursively traversing the target object `component`'s members consuming |
| 78 | arguments, evaluating functions, and instantiating classes as it goes. |
| 79 | |
| 80 | When building a CLI with Fire, your main method should call this function. |
| 81 | |
| 82 | Args: |
| 83 | component: The initial target component. |
| 84 | command: Optional. If supplied, this is the command executed. If not |
| 85 | supplied, then the command is taken from sys.argv instead. This can be |
| 86 | a string or a list of strings; a list of strings is preferred. |
| 87 | name: Optional. The name of the command as entered at the command line. |
| 88 | Used in interactive mode and for generating the completion script. |
| 89 | serialize: Optional. If supplied, all objects are serialized to text via |
| 90 | the provided callable. |
| 91 | Returns: |
| 92 | The result of executing the Fire command. Execution begins with the initial |
| 93 | target component. The component is updated by using the command arguments |
| 94 | to either access a member of the current component, call the current |
| 95 | component (if it's a function), or instantiate the current component (if |
| 96 | it's a class). When all arguments are consumed and there's no function left |
| 97 | to call or class left to instantiate, the resulting current component is |
| 98 | the final result. |
| 99 | Raises: |
| 100 | ValueError: If the command argument is supplied, but not a string or a |
| 101 | sequence of arguments. |
| 102 | FireExit: When Fire encounters a FireError, Fire will raise a FireExit with |
| 103 | code 2. When used with the help or trace flags, Fire will raise a |
| 104 | FireExit with code 0 if successful. |
| 105 | """ |
| 106 | name = name or os.path.basename(sys.argv[0]) |
| 107 | |
| 108 | # Get args as a list. |
| 109 | if isinstance(command, str): |
| 110 | args = shlex.split(command) |
| 111 | elif isinstance(command, (list, tuple)): |
| 112 | args = command |
| 113 | elif command is None: |
| 114 | # Use the command line args by default if no command is specified. |
| 115 | args = sys.argv[1:] |
| 116 | else: |
| 117 | raise ValueError('The command argument must be a string or a sequence of ' |
| 118 | 'arguments.') |
| 119 | |
| 120 | args, flag_args = parser.SeparateFlagArgs(args) |
| 121 | |
| 122 | argparser = parser.CreateParser() |
| 123 | parsed_flag_args, unused_args = argparser.parse_known_args(flag_args) |
| 124 | |
| 125 | context = {} |
| 126 | if parsed_flag_args.interactive or component is None: |
| 127 | # Determine the calling context. |
| 128 | caller = inspect.stack()[1] |
| 129 | caller_frame = caller[0] |
| 130 | caller_globals = caller_frame.f_globals |
nothing calls this directly
no test coverage detected