Skip to content

Commit

Permalink
Added a "top-level" annotation to hide persistent flags in all sub-co…
Browse files Browse the repository at this point in the history
…mmands, excepting some specific commands, while printing help

Fixes issue #1099

Signed-off-by: Silvin Lubecki <[email protected]>
  • Loading branch information
silvin-lubecki committed Jun 1, 2018
1 parent a8ee42a commit c003ed1
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command {
// Install persistent flags
persistentFlags := cmd.PersistentFlags()
persistentFlags.StringVar(&opts.Common.Orchestrator, "orchestrator", "", "Orchestrator to use (swarm|kubernetes|all)")
persistentFlags.SetAnnotation("orchestrator", "top-level", []string{"version", "stack"})

setFlagErrorFunc(dockerCli, cmd, flags, opts)

Expand Down Expand Up @@ -245,6 +246,12 @@ func hideUnsupportedFeatures(cmd *cobra.Command, details versionDetails) {
if !isOSTypeSupported(f, osType) || !isVersionSupported(f, clientVersion) {
f.Hidden = true
}
// root command shows all top-level flags
if cmd.Parent() != nil {
if commands, ok := f.Annotations["top-level"]; ok {
f.Hidden = !findCommand(cmd, commands)
}
}
})

for _, subcmd := range cmd.Commands() {
Expand All @@ -259,6 +266,19 @@ func hideUnsupportedFeatures(cmd *cobra.Command, details versionDetails) {
}
}

// Checks if a command or one of its ancestors is in the list
func findCommand(cmd *cobra.Command, commands []string) bool {
if cmd == nil {
return false
}
for _, c := range commands {
if c == cmd.Name() {
return true
}
}
return findCommand(cmd.Parent(), commands)
}

func isSupported(cmd *cobra.Command, details versionDetails) error {
if err := areSubcommandsSupported(cmd, details); err != nil {
return err
Expand Down

0 comments on commit c003ed1

Please sign in to comment.