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

Completion fails after the first argument for programs without sub-commands #1562

Closed
marckhouzam opened this issue Dec 14, 2021 · 0 comments · Fixed by #1563
Closed

Completion fails after the first argument for programs without sub-commands #1562

marckhouzam opened this issue Dec 14, 2021 · 0 comments · Fixed by #1563

Comments

@marckhouzam
Copy link
Collaborator

marckhouzam commented Dec 14, 2021

Programs that don't have sub-commands can accept any number of arguments.
However, when doing shell completion for such programs, within the __complete code this very __complete command makes it that the program suddenly has a sub-command, and the call to Find() -> legacyArgs() will then return an error if there are more than one argument on the command-line being completed.

This only happens when the program does not specify any value for its Args field, case which leads to legacyArgs() being called.

Here is an illustration. The program used follows.

# prog has no sub-commands
bash-5.1$ ./prog -h
Usage:
  prog [flags]

Flags:
  -h, --help   help for prog

# prog has a ValidArgsFunction to complete arguments
bash-5.1$ ./prog __complete ''
arg1
arg2
:4
Completion ended with directive: ShellCompDirectiveNoFileComp

# Completion fails to continue completing arguments
bash-5.1$ ./prog __complete arg1 ''
[Debug] [Error] Unable to find a command for arguments: [arg1]
:0
Completion ended with directive: ShellCompDirectiveDefault
Test program
package main

import (
	"fmt"

	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Use: "prog",
	ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
		return []string{"arg1", "arg2"}, cobra.ShellCompDirectiveNoFileComp
	},
        Args: nil,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println(fmt.Sprintf("rootCmd called with args %v", args))
	},
}
func main() {
	if err := rootCmd.Execute(); err != nil {
		fmt.Println("Got error", err.Error())
	}
}
marckhouzam added a commit to VilledeMontreal/cobra that referenced this issue Dec 14, 2021
Fixes spf13#1562

Programs that don't have sub-commands can accept any number of args.
However, when doing shell completion for such programs, within the
__complete code this very __complete command makes it that the program
suddenly has a sub-command, and the call to Find() -> legacyArgs() will
then return an error if there are more than one argument on the
command-line being completed.

To avoid this, we first remove the __complete command in such a case so
as to get back to having no sub-commands.

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
jpmcb pushed a commit that referenced this issue Dec 14, 2021
Fixes #1562

Programs that don't have sub-commands can accept any number of args.
However, when doing shell completion for such programs, within the
__complete code this very __complete command makes it that the program
suddenly has a sub-command, and the call to Find() -> legacyArgs() will
then return an error if there are more than one argument on the
command-line being completed.

To avoid this, we first remove the __complete command in such a case so
as to get back to having no sub-commands.

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
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 a pull request may close this issue.

1 participant