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

Command aliases are not tab-completed #1852

Open
Airblader opened this issue Nov 14, 2022 · 2 comments · May be fixed by #1856
Open

Command aliases are not tab-completed #1852

Airblader opened this issue Nov 14, 2022 · 2 comments · May be fixed by #1856
Labels
area/shell-completion All shell completions

Comments

@Airblader
Copy link

Airblader commented Nov 14, 2022

Say you have some command

	cmd := &cobra.Command{
		Use:                   "context",
		Aliases:               []string{"ctx"},
	}

Then completing ct will not work:

prog __complete ct

:4
Completion ended with directive: ShellCompDirectiveNoFileComp

I think the issue is this, though I have not debugged into it, it's just by reading the code:

			// Complete subcommand names, including the help command
			if len(finalArgs) == 0 && !foundLocalNonPersistentFlag {
				// We only complete sub-commands if:
				// - there are no arguments on the command-line and
				// - there are no local, non-persistent flags on the command-line or TraverseChildren is true
				for _, subCmd := range finalCmd.Commands() {
					if subCmd.IsAvailableCommand() || subCmd == finalCmd.helpCommand {
						if strings.HasPrefix(subCmd.Name(), toComplete) {
							completions = append(completions, fmt.Sprintf("%s\t%s", subCmd.Name(), subCmd.Short))
						}
						directive = ShellCompDirectiveNoFileComp
					}
				}
			}

This code path does not take the command's aliases into account.

This appears to be a duplicate of #1296, which has been closed with a reference to some discussion that seems inconclusive to me. I can't think of any reason not to tab-complete aliases, because they are most useful in interactive sessions, i.e., exactly in the situations where a user would want tab-completions to work.

Airblader added a commit to Airblader/cobra that referenced this issue Nov 15, 2022
When completing a subcommand, also take its aliases into consideration
instead of only its name.

fixes spf13#1852
@Airblader Airblader linked a pull request Nov 15, 2022 that will close this issue
@johnSchnake johnSchnake added the area/shell-completion All shell completions label Nov 30, 2022
@github-actions
Copy link

The Cobra project currently lacks enough contributors to adequately respond to all issues. This bot triages issues and PRs according to the following rules:

  • After 60d of inactivity, lifecycle/stale is applied. - After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied and the issue is closed.
    You can:
  • Make a comment to remove the stale label and show your support. The 60 days reset. - If an issue has lifecycle/rotten and is closed, comment and ask maintainers if they'd be interested in reopening

@bartekpacia
Copy link

I can't think of any reason not to tab-complete aliases, because they are most useful in interactive sessions, i.e., exactly in the situations where a user would want tab-completions to work.

I strongly disagree with this. I don't want completion to highlight aliases and shortcuts.

If this change is ever considered, please make it configurable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/shell-completion All shell completions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants