ActiveHelp for cmds that don't take any more args

The result is that when doing shell completion (bash and zsh only),
instead of getting no suggestions for commands that take no more
arguments, the user will instead be shown an informative message:

$ helm list <TAB>
This command does not take any more arguments (but may accept flags).

Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
pull/13261/head
Marc Khouzam 1 year ago
parent 109dbe7c18
commit c92cc07c1b

@ -102,7 +102,7 @@ func newCompletionCmd(out io.Writer) *cobra.Command {
Short: "generate autocompletion script for bash",
Long: bashCompDesc,
Args: require.NoArgs,
ValidArgsFunction: noCompletions,
ValidArgsFunction: noMoreArgsCompFunc,
RunE: func(cmd *cobra.Command, _ []string) error {
return runCompletionBash(out, cmd)
},
@ -114,7 +114,7 @@ func newCompletionCmd(out io.Writer) *cobra.Command {
Short: "generate autocompletion script for zsh",
Long: zshCompDesc,
Args: require.NoArgs,
ValidArgsFunction: noCompletions,
ValidArgsFunction: noMoreArgsCompFunc,
RunE: func(cmd *cobra.Command, _ []string) error {
return runCompletionZsh(out, cmd)
},
@ -126,7 +126,7 @@ func newCompletionCmd(out io.Writer) *cobra.Command {
Short: "generate autocompletion script for fish",
Long: fishCompDesc,
Args: require.NoArgs,
ValidArgsFunction: noCompletions,
ValidArgsFunction: noMoreArgsCompFunc,
RunE: func(cmd *cobra.Command, _ []string) error {
return runCompletionFish(out, cmd)
},
@ -138,7 +138,7 @@ func newCompletionCmd(out io.Writer) *cobra.Command {
Short: "generate autocompletion script for powershell",
Long: powershellCompDesc,
Args: require.NoArgs,
ValidArgsFunction: noCompletions,
ValidArgsFunction: noMoreArgsCompFunc,
RunE: func(cmd *cobra.Command, _ []string) error {
return runCompletionPowershell(out, cmd)
},
@ -209,7 +209,15 @@ func runCompletionPowershell(out io.Writer, cmd *cobra.Command) error {
return cmd.Root().GenPowerShellCompletionWithDesc(out)
}
// Function to disable file completion
func noCompletions(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveNoFileComp
// noMoreArgsCompFunc deactivates file completion when doing argument shell completion.
// It also provides some ActiveHelp to indicate no more arguments are accepted.
func noMoreArgsCompFunc(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return noMoreArgsComp()
}
// noMoreArgsComp deactivates file completion when doing argument shell completion.
// It also provides some ActiveHelp to indicate no more arguments are accepted.
func noMoreArgsComp() ([]string, cobra.ShellCompDirective) {
activeHelpMsg := "This command does not take any more arguments (but may accept flags)."
return cobra.AppendActiveHelp(nil, activeHelpMsg), cobra.ShellCompDirectiveNoFileComp
}

@ -71,7 +71,7 @@ func newCreateCmd(out io.Writer) *cobra.Command {
return nil, cobra.ShellCompDirectiveDefault
}
// No more completions, so disable file completion
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
},
RunE: func(_ *cobra.Command, args []string) error {
o.name = args[0]

@ -58,7 +58,7 @@ func newDocsCmd(out io.Writer) *cobra.Command {
Long: docsDesc,
Hidden: true,
Args: require.NoArgs,
ValidArgsFunction: noCompletions,
ValidArgsFunction: noMoreArgsCompFunc,
RunE: func(cmd *cobra.Command, _ []string) error {
o.topCmd = cmd.Root()
return o.run(out)

@ -42,7 +42,7 @@ func newEnvCmd(out io.Writer) *cobra.Command {
return keys, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
},
Run: func(_ *cobra.Command, args []string) {
envVars := settings.EnvVars()

@ -43,7 +43,7 @@ func newGetAllCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListReleases(toComplete, args, cfg)
},

@ -43,7 +43,7 @@ func newGetHooksCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListReleases(toComplete, args, cfg)
},

@ -45,7 +45,7 @@ func newGetManifestCmd(cfg *action.Configuration, out io.Writer) *cobra.Command
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListReleases(toComplete, args, cfg)
},

@ -42,7 +42,7 @@ func newGetMetadataCmd(cfg *action.Configuration, out io.Writer) *cobra.Command
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListReleases(toComplete, args, cfg)
},

@ -41,7 +41,7 @@ func newGetNotesCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListReleases(toComplete, args, cfg)
},

@ -48,7 +48,7 @@ func newGetValuesCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListReleases(toComplete, args, cfg)
},

@ -62,7 +62,7 @@ func newHistoryCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListReleases(toComplete, args, cfg)
},

@ -68,7 +68,7 @@ func newListCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
Long: listHelp,
Aliases: []string{"ls"},
Args: require.NoArgs,
ValidArgsFunction: noCompletions,
ValidArgsFunction: noMoreArgsCompFunc,
RunE: func(cmd *cobra.Command, _ []string) error {
if client.AllNamespaces {
if err := cfg.Init(settings.RESTClientGetter(), "", os.Getenv("HELM_DRIVER"), debug); err != nil {

@ -50,7 +50,7 @@ func newPluginInstallCmd(out io.Writer) *cobra.Command {
return nil, cobra.ShellCompDirectiveDefault
}
// No more completion once the plugin path has been specified
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
},
PreRunE: func(_ *cobra.Command, args []string) error {
return o.complete(args)

@ -30,7 +30,7 @@ func newPluginListCmd(out io.Writer) *cobra.Command {
Use: "list",
Aliases: []string{"ls"},
Short: "list installed Helm plugins",
ValidArgsFunction: noCompletions,
ValidArgsFunction: noMoreArgsCompFunc,
RunE: func(_ *cobra.Command, _ []string) error {
debug("pluginDirs: %s", settings.PluginsDirectory)
plugins, err := plugin.FindPlugins(settings.PluginsDirectory)

@ -65,7 +65,7 @@ func newPushCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
}
return comps, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace
}
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
},
RunE: func(_ *cobra.Command, args []string) error {
registryClient, err := newRegistryClient(o.certFile, o.keyFile, o.caFile, o.insecureSkipTLSverify, o.plainHTTP)

@ -53,7 +53,7 @@ func newRegistryLoginCmd(cfg *action.Configuration, out io.Writer) *cobra.Comman
Short: "login to a registry",
Long: registryLoginDesc,
Args: require.MinimumNArgs(1),
ValidArgsFunction: noCompletions,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(_ *cobra.Command, args []string) error {
hostname := args[0]

@ -35,7 +35,7 @@ func newRegistryLogoutCmd(cfg *action.Configuration, out io.Writer) *cobra.Comma
Short: "logout from a registry",
Long: registryLogoutDesc,
Args: require.MinimumNArgs(1),
ValidArgsFunction: noCompletions,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(_ *cobra.Command, args []string) error {
hostname := args[0]
return action.NewRegistryLogout(cfg).Run(out, hostname)

@ -50,7 +50,7 @@ func newReleaseTestCmd(cfg *action.Configuration, out io.Writer) *cobra.Command
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListReleases(toComplete, args, cfg)
},

@ -71,7 +71,12 @@ func newRepoAddCmd(out io.Writer) *cobra.Command {
Use: "add [NAME] [URL]",
Short: "add a chart repository",
Args: require.ExactArgs(2),
ValidArgsFunction: noCompletions,
ValidArgsFunction: func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
if len(args) > 1 {
return noMoreArgsComp()
}
return nil, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(_ *cobra.Command, args []string) error {
o.name = args[0]
o.url = args[1]

@ -60,7 +60,7 @@ func newRepoIndexCmd(out io.Writer) *cobra.Command {
return nil, cobra.ShellCompDirectiveDefault
}
// No more completions, so disable file completion
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
},
RunE: func(_ *cobra.Command, args []string) error {
o.dir = args[0]

@ -36,7 +36,7 @@ func newRepoListCmd(out io.Writer) *cobra.Command {
Aliases: []string{"ls"},
Short: "list chart repositories",
Args: require.NoArgs,
ValidArgsFunction: noCompletions,
ValidArgsFunction: noMoreArgsCompFunc,
RunE: func(_ *cobra.Command, _ []string) error {
f, _ := repo.LoadFile(settings.RepositoryConfig)
if len(f.Repositories) == 0 && !(outfmt == output.JSON || outfmt == output.YAML) {

@ -55,7 +55,7 @@ func newRollbackCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
return compListRevisions(toComplete, cfg, args[0])
}
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
},
RunE: func(_ *cobra.Command, args []string) error {
if len(args) > 1 {

@ -65,13 +65,12 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
Aliases: []string{"inspect"},
Long: showDesc,
Args: require.NoArgs,
ValidArgsFunction: noCompletions, // Disable file completion
}
// Function providing dynamic auto-completion
validArgsFunc := func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListCharts(toComplete, true)
}

@ -60,7 +60,7 @@ func newStatusCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListReleases(toComplete, args, cfg)
},

@ -1,2 +1,3 @@
_activeHelp_ This command does not take any more arguments (but may accept flags).
:4
Completion ended with directive: ShellCompDirectiveNoFileComp

@ -1,2 +1,3 @@
_activeHelp_ This command does not take any more arguments (but may accept flags).
:4
Completion ended with directive: ShellCompDirectiveNoFileComp

@ -1,2 +1,3 @@
_activeHelp_ This command does not take any more arguments (but may accept flags).
:4
Completion ended with directive: ShellCompDirectiveNoFileComp

@ -97,7 +97,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
if len(args) == 1 {
return compListCharts(toComplete, true)
}
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
},
RunE: func(_ *cobra.Command, args []string) error {
client.Namespace = settings.Namespace()

@ -50,7 +50,7 @@ func newVerifyCmd(out io.Writer) *cobra.Command {
return nil, cobra.ShellCompDirectiveDefault
}
// No more completions, so disable file completion
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
},
RunE: func(_ *cobra.Command, args []string) error {
err := client.Run(args[0])

@ -65,7 +65,7 @@ func newVersionCmd(out io.Writer) *cobra.Command {
Short: "print the client version information",
Long: versionDesc,
Args: require.NoArgs,
ValidArgsFunction: noCompletions,
ValidArgsFunction: noMoreArgsCompFunc,
RunE: func(_ *cobra.Command, _ []string) error {
return o.run(out)
},

Loading…
Cancel
Save