Skip to content

Custom completion logic doesn't support multiple shorthand flags together #1257

@Luap99

Description

@Luap99

Unlike the flag package, a single dash before an option means something different than a double dash. Single dashes signify a series of shorthand letters for flags. All but the last shorthand letter must be boolean flags or a flag with a default value

from: https://github.com/spf13/pflag#command-line-flag-syntax

The custom completion logic does not respect this and will error in that case. (no completion)

Consider the following simple example:

package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Use:              filepath.Base(os.Args[0]),
	TraverseChildren: false,
	ValidArgs:        []string{"foo", "bar"},
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("rootCmd called")
	},
}

func main() {
	f := rootCmd.Flags()
	f.BoolP("short", "s", false, "short flag 1")
	f.BoolP("short2", "d", false, "short flag 2")
	rootCmd.Execute()
}
$ ./testprog -sd
rootCmd called
$ ./testprog __complete -sd ""
[Debug] [Error] Subcommand 'testprog' does not support flag 'sd'
:0
Completion ended with directive: ShellCompDirectiveDefault
$ ./testprog __complete -s ""
foo
bar
:4
Completion ended with directive: ShellCompDirectiveNoFileComp

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions