-
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
Add support for --version flag #584
Conversation
Implementation of proposal at #282 (comment). Looking for feedback on over-all approach. If it looks reasonable to be accepted, then I can update PR to address any implementation issue and add documentation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution! It's really good!
Just fix the two things I wrote below.
I'm also interested in opinion of @eparis about this PR.
command.go
Outdated
if c.HasParent() { | ||
return c.parent.VersionTemplate() | ||
} | ||
return `{{with .Use}}{{printf "%s " .}}{{end}}{{printf "version %s" .Version}}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need here .Use
?
What do you think about printing only a version? Just {{.Version}}
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can certainly do that as well, but for many applications the version output takes the form {{appName}} version {{version}}
, so thought that it would be a sensible default. Examples:
➜ go version
go version go1.9.2 darwin/amd64
➜ git --version
git version 2.6.3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. But then use .Name
instead of .Use
, because .Use
can be in format command [path] [flags]
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
command.go
Outdated
@@ -848,6 +893,22 @@ func (c *Command) InitDefaultHelpFlag() { | |||
} | |||
} | |||
|
|||
// InitDefaultHelpFlag adds default version flag to c. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct to // InitDefaultVersionFlag ...
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
5f60cea
to
32dfe8d
Compare
One more thing I want to discuss is, if it's better to add |
A default version subcommand would be difficult because it would break any applications that accept only positional args and no subcommands. |
Agree that
@dnephin agree with the back-compat concern in the general case, but this can be mitigated by only adding the command if the root command has no subcommands -- this is the same behavior used for the I've updated the PR to add the subcommand behavior as well. |
997ef50
to
84e1b22
Compare
Added support for
The one point of customization that I think might be nice to expose is the ability to remove the |
@BoGeM what are the next steps here for discussion/deciding a path forward? |
The question is, should we add both subcommand and version. There is a lot of question to implemented version subcommand (e.g. is there a possibility to use only flag or only subcommand?), so IMHO it's better to leave only version flag for now. |
Makes sense -- also agree given that users can always add a |
84e1b22
to
aaf72ac
Compare
Actually, that is also not so hard to implement it by yourself, but having already integrated |
Updated PR to be flag implementation only |
Okay. The code and tests are great, so I would like to merge it. |
Thank you for your contribution! |
The current implementation does not work with the default run method (like usage).
|
@benkeil I believe this is expected behavior. Per https://github.com/spf13/cobra/blob/master/command.go#L679, if a command isn't runnable, the expected behavior is to run the "help" command. In your example above, If you make the root command runnable by providing a
Then the |
That’s right, but then the —version flag makes no sense, because I must always implement it by myself. |
@benkeil No, you must not. The version flag works only if you have Run function in your command. In Run function normally you have logic of your command/application, not implementation of version flag. |
Actually, after some further testing locally, I think I understand the concern -- if an application has a root command that has no |
PR is at #595. This fix should work for the standard case. I'm not sure if there are any concerns for cases where a non-root (intermediate) command has a non-blank version, but the linked PR properly encapsulates the logic that I meant to implement for this flag. |
Thank you. |
Use a tagged release of Cobra Relevant changes: - spf13/cobra#567 Add `CalledAs` method to cobra.Command - spf13/cobra#580 Update error message for missing required flags - spf13/cobra#584 Add support for --version flag - spf13/cobra#614 If user has a project in symlink, just use its destination folder and work there - spf13/cobra#649 terminates the flags when -- is found in commandline - spf13/cobra#662 Add support for ignoring parse errors - spf13/cobra#686 doc: hide hidden parent flags Also various improvements were added for generating Bash completion scripts (currently not used by us) Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Use a tagged release of Cobra Relevant changes: - spf13/cobra#567 Add `CalledAs` method to cobra.Command - spf13/cobra#580 Update error message for missing required flags - spf13/cobra#584 Add support for --version flag - spf13/cobra#614 If user has a project in symlink, just use its destination folder and work there - spf13/cobra#649 terminates the flags when -- is found in commandline - spf13/cobra#662 Add support for ignoring parse errors - spf13/cobra#686 doc: hide hidden parent flags Also various improvements were added for generating Bash completion scripts (currently not used by us) Bump spf13/pflag to v1.0.1 Relevant changes: - spf13/pflag#122 DurationSlice: implementation and tests - spf13/pflag#115 Implement BytesHex type of argument - spf13/pflag#150 Add uintSlice and boolSlice to name prettifier - spf13/pflag#155 Add multiline wrapping support - spf13/pflag#158 doc: clarify difference between string slice vs. array - spf13/pflag#160 add ability to ignore unknown flags - spf13/pflag#163 Allow Users To Show Deprecated Flags Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Use a tagged release of Cobra. All relevant PR's were merged, so the fork is no longer needed. Relevant changes: - spf13/cobra#552 Add a field to disable [flags] in UseLine() - spf13/cobra#567 Add `CalledAs` method to cobra.Command - spf13/cobra#580 Update error message for missing required flags - spf13/cobra#584 Add support for --version flag - spf13/cobra#614 If user has a project in symlink, just use its destination folder and work there - spf13/cobra#649 terminates the flags when -- is found in commandline - spf13/cobra#662 Add support for ignoring parse errors - spf13/cobra#686 doc: hide hidden parent flags Also various improvements were added for generating Bash completion scripts (currently not used by us) Fixes usage output for dockerd; Before this update: dockerd --help Usage: dockerd COMMAND A self-sufficient runtime for containers. After this update: dockerd --help Usage: dockerd [OPTIONS] [flags] A self-sufficient runtime for containers. Bump spf13/pflag to v1.0.1 Relevant changes: - spf13/pflag#106 allow lookup by shorthand - spf13/pflag#113 Add SortFlags option - spf13/pflag#138 Generate flag error output for errors returned from the parseFunc - spf13/pflag#141 Fixing Count flag usage string - spf13/pflag#143 add int16 flag - spf13/pflag#122 DurationSlice: implementation and tests - spf13/pflag#115 Implement BytesHex type of argument - spf13/pflag#150 Add uintSlice and boolSlice to name prettifier - spf13/pflag#155 Add multiline wrapping support - spf13/pflag#158 doc: clarify difference between string slice vs. array - spf13/pflag#160 add ability to ignore unknown flags - spf13/pflag#163 Allow Users To Show Deprecated Flags Hide [flags] in usage output Hides the [flags] in the usage output of commands (present in newer versions of Cobra), using the `.DisableFlagsInUseLine` option. Before this change: dockerd --help Usage: dockerd [OPTIONS] [flags] A self-sufficient runtime for containers. After this change: dockerd --help Usage: dockerd [OPTIONS] A self-sufficient runtime for containers. Signed-off-by: Sebastiaan van Stijn <github@gone.nl> Â# modified: vendor/github.com/spf13/pflag/string_array.go § Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Use a tagged release of Cobra Relevant changes: - spf13/cobra#567 Add `CalledAs` method to cobra.Command - spf13/cobra#580 Update error message for missing required flags - spf13/cobra#584 Add support for --version flag - spf13/cobra#614 If user has a project in symlink, just use its destination folder and work there - spf13/cobra#649 terminates the flags when -- is found in commandline - spf13/cobra#662 Add support for ignoring parse errors - spf13/cobra#686 doc: hide hidden parent flags Also various improvements were added for generating Bash completion scripts (currently not used by us) Bump spf13/pflag to v1.0.1 Relevant changes: - spf13/pflag#122 DurationSlice: implementation and tests - spf13/pflag#115 Implement BytesHex type of argument - spf13/pflag#150 Add uintSlice and boolSlice to name prettifier - spf13/pflag#155 Add multiline wrapping support - spf13/pflag#158 doc: clarify difference between string slice vs. array - spf13/pflag#160 add ability to ignore unknown flags - spf13/pflag#163 Allow Users To Show Deprecated Flags Signed-off-by: Sebastiaan van Stijn <github@gone.nl> Upstream-commit: 0acb2522bd17ec6f1eb4ecb0960c792a71f08f69 Component: cli
Use a tagged release of Cobra. All relevant PR's were merged, so the fork is no longer needed. Relevant changes: - spf13/cobra#552 Add a field to disable [flags] in UseLine() - spf13/cobra#567 Add `CalledAs` method to cobra.Command - spf13/cobra#580 Update error message for missing required flags - spf13/cobra#584 Add support for --version flag - spf13/cobra#614 If user has a project in symlink, just use its destination folder and work there - spf13/cobra#649 terminates the flags when -- is found in commandline - spf13/cobra#662 Add support for ignoring parse errors - spf13/cobra#686 doc: hide hidden parent flags Also various improvements were added for generating Bash completion scripts (currently not used by us) Fixes usage output for dockerd; Before this update: dockerd --help Usage: dockerd COMMAND A self-sufficient runtime for containers. After this update: dockerd --help Usage: dockerd [OPTIONS] [flags] A self-sufficient runtime for containers. Bump spf13/pflag to v1.0.1 Relevant changes: - spf13/pflag#106 allow lookup by shorthand - spf13/pflag#113 Add SortFlags option - spf13/pflag#138 Generate flag error output for errors returned from the parseFunc - spf13/pflag#141 Fixing Count flag usage string - spf13/pflag#143 add int16 flag - spf13/pflag#122 DurationSlice: implementation and tests - spf13/pflag#115 Implement BytesHex type of argument - spf13/pflag#150 Add uintSlice and boolSlice to name prettifier - spf13/pflag#155 Add multiline wrapping support - spf13/pflag#158 doc: clarify difference between string slice vs. array - spf13/pflag#160 add ability to ignore unknown flags - spf13/pflag#163 Allow Users To Show Deprecated Flags Hide [flags] in usage output Hides the [flags] in the usage output of commands (present in newer versions of Cobra), using the `.DisableFlagsInUseLine` option. Before this change: dockerd --help Usage: dockerd [OPTIONS] [flags] A self-sufficient runtime for containers. After this change: dockerd --help Usage: dockerd [OPTIONS] A self-sufficient runtime for containers. Signed-off-by: Sebastiaan van Stijn <github@gone.nl> Â# modified: vendor/github.com/spf13/pflag/string_array.go § Signed-off-by: Sebastiaan van Stijn <github@gone.nl> Upstream-commit: ed75c7727bf3e454a5faa6baf686de12152d911c Component: engine
Fixes #282