-
Notifications
You must be signed in to change notification settings - Fork 3k
When no subcommands are registered, omit command help output #32
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
When no subcommands are registered, omit command help output #32
Conversation
For a single root command with a Run method, the help output still contains 'help [command]' as a subcommand (because Help is always added). Since the only subcommand would be 'help', the help is better off omitted. This change allows a command to be used both as a subcommand or a root command without having to define a custom help that elides the help command when no subcommands are added. Instead, the default help command is only added when subcommands are present.
…oot_only_command When no subcommands are registered, omit command help output
|
Apart from resulting in #46 (omitting command description), this change conflicts with cobra doc:
There are multiple issues:
|
|
I am trying to fix this, but feel that this commit should be reverted and rethought about. @smarterclayton ideas? |
|
So a command that has no children by choice (because Run does the thing you want) doesn't seem to need a help command. As soon as you add your own subcommands, then help becomes a reasonable subcommand. It's the difference between the 'git' command and 'git-filter-branch'. Since someone who wants to use commander is trying to group a lot of related commands together, there are occasionally cases where you want to be able to expose a single command. With the previous behavior, you were always left with a bizarre suncommand that did nothing useful. When I made the change, I did not suggest a change to the readme or example, but that would clarify things. The example should demonstrate subcommands, but I don't think its the case that cobra shouldn't support more sophisticated use. |
|
The run 'help' for usage message should definitely be fixed in the context of my intended change and was not intentional. |
|
Thanks for the clarification. Agree with the rational. Can someone update the readme? Steve Francia
|
|
Will do
|
|
Great, editing README is also an option. But in context of this PR, I feel This is also necessary because as I documented in #46, now there is no way to see the description of the root command with the default |
|
You definitely have a good point about the root help being essential. I Perhaps the logic should be.. If the only subcommand is help then don't display "Available Commands" at If there is only a root command with no subcommands then help by itself Does this make sense? |
|
Let me illustrate with an example: Old behavior: Suggested new behavior with implicit help: If About the flag, I am not sure if I understand why there were two different outputs for the
Sounds logical? I validated that this is consistent with |
|
What if I want to reserve the first argument for myself (in the above example of reason commander for commands that have subcommands and others that don't? If say, you have a subcommand that creates a directory, and so you want the first argument to be a directory named "help". The behavior now is that the root command gets all arguments. If you force every use of command to go through command processing, and there is no way to disable this behavior, then you can no longer make a "subcommandless command".
|
|
I am not sure if I understood what you meant by:
About the directory named help, I don't see how that could be a problem. The following should work as expected with newdir subcommand as you describe above: From what I understand with experimentation and reading the code, However, if you want such behavior when then cobra is probably an overkill. Also, this could be worked around by having a flag for the root command to keep the semantics correct. |
|
I see cobra as a general command framework for building cli commands in go. Subcommands are hard, so that's where the effort is. But cobra provides value even for commands without subcommands, especially in large projects which want to tweak cobra in the same way across multiple commands. The behavior we're discussing in the absence of subcommands is essentially the behavior of every major Unix command line utility (--help). It seems strange to me to argue that every cobra command should take a magic "help" subcommand in the absence of subcommands, and behave differently (in an unexpected fashion) to users familiar with other CLIs. I'm not opposed to having to invoke a flag, it just doesn't seem to be the "right" behavior (I can register a single command and execute it without needing subcommands).
|
|
You raise a good point about having cobra as a generic CLI framework. In that case, one easy solution I can think of is to provide a toggle to switch default Having default help is important as cobra supports @spf13 opinion? Design philosophy? |
|
I'll stand by what I said earlier.... If the only subcommand is help then don't display "Available Commands" at all. Otherwise it should be included in the list. This seems like the ideal behavior for Cobra and fits exactly with the unix tool philosophy as well as the subcommand philosophy. @smarterclayton I think this agrees with your point, but perhaps I misunderstood. Please correct me if I did. |
|
I agree with you - a more succinct way of saying what I was trying to say. I'll update the example and README. ----- Original Message -----
|
|
Updated in #50 ----- Original Message -----
|
For a single root command with a Run method, the help output still
contains 'help [command]' as a subcommand (because Help is always
added). Since the only subcommand would be 'help', the help is better
off omitted.
This change allows a command to be used both as a subcommand
or a root command without having to define a custom help that elides
the help command when no subcommands are added. Instead, the default
help command is only added when subcommands are present.
Example:
If you specify a custom help command, your help command will be used
regardless of subcommand presence.