python-fire icon indicating copy to clipboard operation
python-fire copied to clipboard

Docs don't mention when a class method is considered private v.s public / --help behavior is confusing

Open doronbehar opened this issue 1 year ago • 2 comments

Here's a MWE:

#!/usr/bin/env python

from fire import Fire

class Example:
    def version(self):
        return "bla"
    def start(self):
        print("starting")
    def stop(self):
        print("stopping")

Fire(Example)

Running this script without arguments shows the available commands - OK:

NAME
    fire-bug.py

SYNOPSIS
    fire-bug.py COMMAND

COMMANDS
    COMMAND is one of the following:

     start

     stop

     version

But the more common (and usually safe) thing to do when a user is being introduced a new command, is to run it with a --help. However, Fire doesn't behave the same for this argument:

INFO: Showing help with the command 'fire-bug.py -- --help'.

NAME
    fire-bug.py

SYNOPSIS
    fire-bug.py -

Unless you use --verbose as well. Why is that? This is not trivial for a user to guess, and only reading the docs here led me to this conclusion. Plus, the docs don't mention how to make a class method a "public" method...

doronbehar avatar Aug 02 '24 10:08 doronbehar

Quick solution: If you call Fire(Example()) instead of Fire(Example) I think you'll get the behavior you want.

The reason this happens is that when you call Fire(Example) and run --help it's giving you help for the class Example, not for an instance of the class. By contrast, when you run without arguments the first thing that happens is the class gets instantiated, and so the final component is an instance of Example(), and then help is shown for that.

I agree this isn't so intuitive and can be improved.

dbieber avatar Sep 19 '24 23:09 dbieber

Quick solution: If you call Fire(Example()) instead of Fire(Example) I think you'll get the behavior you want.

@dbieber thanks for replying. Indeed that works, but you should be aware that your README states exactly the opposite:

https://github.com/google/python-fire/blob/efcf60f7f8202d9887b9da16e4ff81a554b9d023/README.md?plain=1#L53-L66

doronbehar avatar Sep 22 '24 09:09 doronbehar