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

Make bash completion provide sub-command names even if ValidArgs present #1088

Merged
merged 1 commit into from
May 7, 2020

Conversation

marckhouzam
Copy link
Collaborator

Fixes #1076

This PR makes bash completion also provide sub-command names when ValidArgs or ValidArgsFunction are specified.

Here are details about the completions given:

Cobra's Bash completion:
Original (before custom Go completions of PR #1035):

  • ValidArgs XOR sub-commands (which means it gives ValidArgs and if there are none, then it gives sub-commands)

Current:

  • ValidArgs XOR ValidArgsFunction XOR sub-commands

With this PR:

  • (ValidArgs AND sub-commands) XOR (ValidArgsFunction AND sub-commands)

For info, Cobra's Zsh completion does it yet another way:

  • sub-commands XOR ValidArgs

Which which should align eventually.

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
@jharshman
Copy link
Collaborator

+label:kind/enhancement

@jharshman jharshman added the kind/feature A feature request for cobra; new or enhanced behavior label May 7, 2020
@jharshman jharshman merged commit f8fdd17 into spf13:master May 7, 2020
@marckhouzam marckhouzam deleted the feat/compCmdsAndValidArgs branch May 8, 2020 02:22
@Fanduzi
Copy link

Fanduzi commented Feb 1, 2023

I want to use ValidArgs and Args to validate args, but I don't want them to appear in the completion information

for example: show command have two subcommad [binlog xtrabackup]

# my-recover show --help
Show mysql backup information

Usage:
  my-recover show [flags]
  my-recover show [command]

Available Commands:
  binlog      show backed up binlog information
  xtrabackup  show xtrabackups information

Flags:
  -h, --help   help for show

I hope to verify the args passed by the user and only receive binlog/xtrabackup

# my-recover show hh
Error: invalid argument "hh" for "my-recover show"

therefore i use [Args and ValidArgs]

	Args:      cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
	ValidArgs: []string{"xtrabackup", "binlog"},

But when completing, ValidArgs and subcommand are displayed at the same time

# my-recover show 
binlog                                           binlog      (show backed up binlog information)  xtrabackup                                       xtrabackup  (show xtrabackups information)

Is there any other validation method, or is there any switch to prevent ValidArgs from being displayed in the completion?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A feature request for cobra; new or enhanced behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Completions do not suggest sub-commands if there are ValidArgs
3 participants