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

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

Closed
Luap99 opened this issue Oct 15, 2020 · 6 comments · Fixed by #1258
Closed

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

Luap99 opened this issue Oct 15, 2020 · 6 comments · Fixed by #1258

Comments

@Luap99
Copy link
Contributor

Luap99 commented Oct 15, 2020

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
@marckhouzam
Copy link
Collaborator

I completely missed this scenario! Thanks @Luap99 for noticing. Will you be working on it?

@Luap99
Copy link
Contributor Author

Luap99 commented Oct 15, 2020

Yes I just looked into it. Only a few lines to change.

Luap99 added a commit to Luap99/cobra that referenced this issue Oct 15, 2020
Flag definitions like `-asd` are not handled correctly by
the custom completion logic. They should be treated as
multiple flags. For details refer to spf13#1257.

Fixes spf13#1257

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
Luap99 added a commit to Luap99/cobra that referenced this issue Oct 15, 2020
Flag definitions like `-asd` are not handled correctly by
the custom completion logic. They should be treated as
multiple flags. For details refer to spf13#1257.

Fixes spf13#1257

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
Luap99 added a commit to Luap99/cobra that referenced this issue Oct 15, 2020
Flag definitions like `-asd` are not handled correctly by
the custom completion logic. They should be treated as
multiple flags. For details refer to spf13#1257.

Fixes spf13#1257

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
@Luap99
Copy link
Contributor Author

Luap99 commented Oct 15, 2020

I'm astonished that I didn't catch this earlier I use this syntax all the time.
PR #1258 to fix

Luap99 added a commit to Luap99/cobra that referenced this issue Oct 15, 2020
Flag definitions like `-asd` are not handled correctly by
the custom completion logic. They should be treated as
multiple flags. For details refer to spf13#1257.

Fixes spf13#1257

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
Luap99 added a commit to Luap99/cobra that referenced this issue Oct 15, 2020
Flag definitions like `-asd` are not handled correctly by
the custom completion logic. They should be treated as
multiple flags. For details refer to spf13#1257.

Fixes spf13#1257

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
Luap99 added a commit to Luap99/cobra that referenced this issue Oct 15, 2020
Flag definitions like `-asd` are not handled correctly by
the custom completion logic. They should be treated as
multiple flags. For details refer to spf13#1257.

Fixes spf13#1257

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
@github-actions
Copy link

This issue is being marked as stale due to a long period of inactivity

@marckhouzam
Copy link
Collaborator

Waiting for review on #1258

@github-actions
Copy link

This issue is being marked as stale due to a long period of inactivity

Luap99 added a commit to Luap99/cobra that referenced this issue Feb 18, 2021
Flag definitions like `-asd` are not handled correctly by
the custom completion logic. They should be treated as
multiple flags. For details refer to spf13#1257.

Fixes spf13#1257

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
jpmcb pushed a commit that referenced this issue May 3, 2021
Flag definitions like `-asd` are not handled correctly by
the custom completion logic. They should be treated as
multiple flags. For details refer to #1257.

Fixes #1257

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
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.

3 participants