-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Getting --help to print descriptions for arguments #571
Comments
Hi @reallyroy I think that this is not supported yet. In #395 it was suggested to edit the
which will produce: Usage:
build ['arg1'] ['arg2'] ['arg3'] [flags] However, I think that this should be better tackled together with #838, to produce the following output: #NoArgs
Usage:
build [flags] #ArbitraryArgs
Usage:
build [flags] [args] #MinimumNArgs(x)
Usage (NArgs>x):
build [flags] args #MaximumNArgs(x)
Usage (NArgs<x):
build [flags] [args] #ExactArgs(x)
Usage (NArgs=x):
build [flags] args
In any of the cases above, if
|
This issue is being marked as stale due to a long period of inactivity |
AFAIK, this is not supported yet. |
For now, the best set of workarounds I found: (building on kofalt's example) &cobra.Command{
Use: "clone [flags] URL [DIRECTORY]",
Long: "Clone a git repo from given URL into a new directory.\n" +
"\n" +
"URL can use git://, https://, ssh:// schemes.\n"
"Directory defaults to last component of URL under current directory.",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("Repository URL is required")
}
if len(args) > 2 {
return fmt.Errorf("Unexpected positional arguments after URL DIRECTORY: %s", )
}
return nil
},
I think cobra could do better on last part in future; it's pretty standard to at least print a "Usage: ..." line by default on wrong invocations. |
Chosen approach explained in spf13/cobra#571 (comment) Didn't touch the generic REST commands such as `get`, `delete` etc as I'm confused by them both using `urls.Expand()` to support `get URL` & `get KIND ID` forms, as well as having some Cobra sub-commands.
This is such a basic behavior, it exists in every other arg parser in any language that I know of. It is baffling that args are treated as a second class citizens compared to flags. If anything it should be the other way around! |
Just wanted to check if there is a way to get Cobra to print help text for ARGS.
Say I have a CLI whose usage is:
do [SOMETHING]
I was wondering if it was possible to get
do --help
to print:The readme doesn't mention anything like this, and there are no examples on SetHelpTemplate, so I'm unsure if that is what needs to be used here. Would someone be able clarify if this is possible? And if not, are there any other options?
Thanks!
The text was updated successfully, but these errors were encountered: