-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Get a list of all valid commands when writing --help #225
Comments
Hi @bfelbo, Are you suggesting that the help show:
or is there a command that's actually missing from the help, that you'd like it to show? It should be showing all the possible commands, but not listed out as all possible complete commands explicitly simply due to formatting considerations. In particular, all commands share a prefix, and sometimes that prefix is quite long, so we choose not to write out that prefix repeatedly. Always open to suggestions for improvement though. |
Thanks for the response. I'll make it more clear :) See the below example from the docs.
Right now running
I think it'd be great if running help would actually tell you what CLI commands are available, i.e. |
This doesn't seem to be the case? (see my previous comment and example) |
@bfelbo As far as I know, it doesn't generate the options for you. You're saying it would be nice if help also showed something like this?
I don't think that would be too hard to add-in. |
Exactly. That would be amazing! For the class-based interface, I would probably ignore the
|
If the arguments have a default, it'd be great to include them too, e.g.:
I'm not really sure what the |
@bfelbo so I was just playing around. If you return self from your functions you can get a little bit closer to what you're looking for: import fire
class Calculator(object):
"""A simple calculator class."""
def __init__(self, meow=True):
self.meow=meow
self.db = None
def double(self, number):
self.db = 2 * number
return self
def add(self, number):
self.db = 2 * number
return self
if __name__ == '__main__':
fire.Fire(Calculator) Now when I run this I get a help screen with COMMANDS:
|
@bfelbo Ignoring that my example isn't really functional, it does show that COMMANDS are parsed at some point. and we could make use of that to extend the general response from --help This would be a nice upgrade - and I think fits the theme of bootstrapping classes into a more friendly cli. |
Agreed this is an issue. Let me explain why the current behavior is as it is, and how we can probably fix it. Current State"--help" vs "-- --help"--help, --interactive, --verbose, etc are special Fire arguments. Normally they are separated from the standard arguments to a Fire CLI by a separating However, in order to let users get help the way they expect, we make an exception for --help. (Eventually we may want to broaden this exception to all of Fire's args, but for now it's just for --help.) This means that if a user specifies "--help" or "-h" and the CLI isn't expecting an argument named "help" or "h", we'll show the user help for the current component. Help for Objects vs Help for ClassesThe help screen is meant to show all the commands for the current component. If the current component is a class, often the only thing you can do is initialize it. Only after it's initialized does it make sense to access and call its methods. If the current component is an object (which happens for example after a class has been initialized) then the methods on that object are shown in the help as commands. You can use a separator ( Getting Help Today
Next Steps [Edit: never mind, see next comment instead]The solution will be to make References in the CodeHere is where Fire checks to see if the user is requesting help without a separating --. The logic for separators is at Lines 442 to 450 in 1ac5105
and Line 570 in 1ac5105
Basically what we'll have to change is: Instead of immediately |
Actually, perhaps it's even better to just show the methods of a class in that class's help screen, even if the class hasn't been instantiated yet. We'll just need to be careful in the presentation to make clear which flags are required (because the methods can't be called until after the class is initialized). |
@dbieber I think this would work perfectly. Like you previously outlined
Should just go into details on that class, like an improved version of what |
The help text is generated at: Line 47 in 1ac5105
The list of possible actions (including commands) is generated by Line 61 in 1ac5105
The commands are aggregated at: Lines 330 to 331 in 1ac5105
And python-fire/fire/value_types.py Lines 35 to 36 in 1ac5105
The actual string formatting to build the help text starts at Line 65 in 1ac5105
|
Here's a minimal example that would need to be fixed to resolve this:
VisibleMembers is defined here: python-fire/fire/completion.py Line 347 in 1ac5105
|
The code ignoring methods in classes is here: python-fire/fire/completion.py Lines 331 to 334 in 1ac5105
Simply changing that from False to True will result in methods being included in the help. Additional changes will likely also be needed to make sure flags are marked as required when appropriate so we don't falsely give the impression that these methods are callable before the class has been instantiated. |
Thanks for this!
Perhaps wise to commit the current solution and refine per this comment later? Status quo help message is a bit tricky unless user is familiar with fire, and the solution you sketched out is a big improvement! |
This is desired basic functionality for any CLI argument parser. Is there an alternative straightforward tool that does this? |
Would be happy to help and submit a PR along the lines of @dbieber's last comment if a maintainer is open to it. |
Just reviving this old thread to say: showing the methods of a class was my expected default behavior for "--help" and I was very surprised when I discovered it did not work as expected. |
Thanks for creating this amazing library. I'm somewhat confused as to how the
--help
command is implemented. When runningpython example.py --help
Fire only prints the flags, not the actual commands that can be run (see example below):How do I make
--help
show all the possible commands and why is that not the default? :)The text was updated successfully, but these errors were encountered: