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

Add support for --version flag #584

Merged
merged 1 commit into from
Dec 1, 2017
Merged

Conversation

nmiyake
Copy link
Contributor

@nmiyake nmiyake commented Nov 22, 2017

Fixes #282

@nmiyake
Copy link
Contributor Author

nmiyake commented Nov 22, 2017

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.

Copy link
Collaborator

@n10v n10v left a 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}}`
Copy link
Collaborator

@n10v n10v Nov 23, 2017

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

Copy link
Contributor Author

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

Copy link
Collaborator

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

Copy link
Contributor Author

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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct to // InitDefaultVersionFlag ....

Copy link
Contributor Author

@nmiyake nmiyake Nov 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@n10v
Copy link
Collaborator

n10v commented Nov 23, 2017

One more thing I want to discuss is, if it's better to add version subcommand instead of flag. Many cobra applications (at least hugo, docker, gopherjs, rclone) use namely subcommand.

@dnephin
Copy link
Contributor

dnephin commented Nov 23, 2017

docker has both, --version is for a short single line version (as it is in this PR), and the version subcommand has a multi-line output with a bunch of versions for both client and server.

A default version subcommand would be difficult because it would break any applications that accept only positional args and no subcommands.

@nmiyake
Copy link
Contributor Author

nmiyake commented Nov 23, 2017

Agree that version subcommand makes sense as well -- common applications like docker and git support both:

➜ git version
git version 2.6.3
➜ git --version
git version 2.6.3
➜ docker version
Client:
 Version:      17.11.0-ce
 API version:  1.34
 Go version:   go1.8.4
 Git commit:   1caf76c
 Built:        Mon Nov 20 18:30:18 2017
 OS/Arch:      darwin/amd64

Server:
 Version:      17.11.0-ce
 API version:  1.34 (minimum version 1.12)
 Go version:   go1.8.5
➜ docker --version
Docker version 17.11.0-ce, build 1caf76c

@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 help command.

I've updated the PR to add the subcommand behavior as well.

@nmiyake nmiyake force-pushed the addVersionFlag branch 4 times, most recently from 997ef50 to 84e1b22 Compare November 24, 2017 18:17
@nmiyake
Copy link
Contributor Author

nmiyake commented Nov 24, 2017

Added support for version subcommand. Behavior is as follows:

  • Like help, if the root command has no subcommands, this subcommand is not added
  • For backcompat, the subcommand is only added if the Version property is non-empty
  • For backcompat, if a version subcommand has already been added, the default one will not be added

The one point of customization that I think might be nice to expose is the ability to remove the version subcommand entirely if it is not wanted (for example, if the author only wants the flag but not the subcommand). Right now, I don't believe this is possible for help either. Will file a separate issue to discuss that.

@nmiyake
Copy link
Contributor Author

nmiyake commented Nov 30, 2017

@BoGeM what are the next steps here for discussion/deciding a path forward?

@n10v
Copy link
Collaborator

n10v commented Dec 1, 2017

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.

@nmiyake
Copy link
Contributor Author

nmiyake commented Dec 1, 2017

Makes sense -- also agree given that users can always add a version command themselves, whereas the --version flag implementation requires library-level support to be done properly. Will update PR to be only flag again.

@n10v
Copy link
Collaborator

n10v commented Dec 1, 2017

--version flag implementation requires library-level support to be done properly

Actually, that is also not so hard to implement it by yourself, but having already integrated --version flag is a nice and convenient feature IMHO.

@nmiyake
Copy link
Contributor Author

nmiyake commented Dec 1, 2017

Updated PR to be flag implementation only

@n10v
Copy link
Collaborator

n10v commented Dec 1, 2017

Okay. The code and tests are great, so I would like to merge it.

@n10v n10v merged commit b1ec2ce into spf13:master Dec 1, 2017
@n10v
Copy link
Collaborator

n10v commented Dec 1, 2017

Thank you for your contribution!

@benkeil
Copy link

benkeil commented Dec 6, 2017

The current implementation does not work with the default run method (like usage).

rootCmd := &cobra.Command{Use: "root", Version: "1.0.0"}
fmt.Println(executeCommand(rootCmd, "--version"))

@nmiyake
Copy link
Contributor Author

nmiyake commented Dec 7, 2017

@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, rootCmd is not runnable, so attempting to run the command will always execute the "help" behavior.

If you make the root command runnable by providing a Run or RunE function:

rootCmd := &Command{Use: "root", Version: "1.0.0", Run: func(cmd *Command, args []string){}}

Then the --version flag will work as expected. The same is true if you add any subcommands to rootCmd.

@benkeil
Copy link

benkeil commented Dec 7, 2017

That’s right, but then the —version flag makes no sense, because I must always implement it by myself.

@n10v
Copy link
Collaborator

n10v commented Dec 7, 2017

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

@nmiyake
Copy link
Contributor Author

nmiyake commented Dec 7, 2017

Actually, after some further testing locally, I think I understand the concern -- if an application has a root command that has no Run defined but does have subcommands, the --version flag does not work, when I would expect it to actually work in that case. Fix is fairly trivial -- will put up PR with fix and updated test for review/comment.

@nmiyake
Copy link
Contributor Author

nmiyake commented Dec 7, 2017

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.

@benkeil
Copy link

benkeil commented Dec 7, 2017

Thank you.

thaJeztah added a commit to thaJeztah/cli that referenced this pull request May 18, 2018
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>
thaJeztah added a commit to thaJeztah/cli that referenced this pull request May 19, 2018
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>
thaJeztah added a commit to thaJeztah/docker that referenced this pull request May 19, 2018
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>
docker-jenkins pushed a commit to docker-archive/docker-ce that referenced this pull request May 22, 2018
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
docker-jenkins pushed a commit to docker-archive/docker-ce that referenced this pull request May 22, 2018
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
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

Successfully merging this pull request may close these issues.

4 participants