google / python-fire

Python Fire is a library for automatically generating command line interfaces (CLIs) from absolutely any Python object.
Other
26.95k stars 1.44k forks source link

cli for function created on the fly #471

Closed tschm closed 10 months ago

tschm commented 11 months ago

I have a function

def outer(strategy, default):
    """outer cli interface"""

    def inner(filename=default, output=Path.home() / "data"):
          # Do something with strategy

    expose inner as cli
    return fire.Fire(inner)

I was hoping fire works for this situation, too. Fire seems to try to execute the inner function.

dbieber commented 10 months ago

The fire.Fire function always "fires off" / executes the command passed to it. If no command is passed, then sys.argv is used as the command (that's what lets it produce CLIs so easily). That's what's happening here.

If you want to get a new input from the user, you could do that before calling Fire, e.g.:

def outer(strategy, default):
    """outer cli interface"""

    def inner(filename=default, output=Path.home() / "data"):
          # Do something with strategy

    # expose inner as cli
    command = input()  # Get a new input from the user
    return fire.Fire(inner, command=command)