Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
doronbehar opened this issue Aug 2, 2024 · 2 comments

Comments

@doronbehar
Copy link

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...

@dbieber
Copy link
Member

dbieber commented Sep 19, 2024

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.

@doronbehar
Copy link
Author

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:

python-fire/README.md

Lines 53 to 66 in efcf60f

Here's an example of calling Fire on a class.
```python
import fire
class Calculator(object):
"""A simple calculator class."""
def double(self, number):
return 2 * number
if __name__ == '__main__':
fire.Fire(Calculator)
```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants