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

Combining shorthand flags before command names does not work #2188

Open
nicula-stripe opened this issue Sep 11, 2024 · 4 comments · May be fixed by #2189
Open

Combining shorthand flags before command names does not work #2188

nicula-stripe opened this issue Sep 11, 2024 · 4 comments · May be fixed by #2189

Comments

@nicula-stripe
Copy link

How to reproduce:

  • Compile the following source code which defines some commands & flags (go build ./mycli.go):
package main

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

var (
	aFlag bool
	bFlag bool
	cFlag string
)

func main() {
	// Define commands
	var rootCmd = &cobra.Command{
		Use: "mycli",
	}
	var firstCmd = &cobra.Command{
		Use: "first",
	}
	var secondCmd = &cobra.Command{
		Use: "second",
		Run: func(cmd *cobra.Command, args []string) {
			if aFlag {
				fmt.Println("flag A is set")
			}
			if bFlag {
				fmt.Println("flag B is set")
			}
			if cFlag != "" {
				fmt.Printf("flag C is set with value: %s\n", cFlag)
			}
		},
	}

	// Define flags
	rootCmd.PersistentFlags().BoolVarP(&aFlag, "set-A", "A", false, "Set the A flag")
	rootCmd.PersistentFlags().BoolVarP(&bFlag, "set-B", "B", false, "Set the B flag")
	rootCmd.PersistentFlags().StringVarP(&cFlag, "set-C", "C", "", "Set the C flag with a value")

	firstCmd.AddCommand(secondCmd)
	rootCmd.AddCommand(firstCmd)

	if err := rootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
  • Notice how this command does not work:
$ ./mycli -ABC elephant first second
Error: unknown command "elephant" for "mycli"
Run 'mycli --help' for usage.
unknown command "elephant" for "mycli"
  • However, those commands work:
$ ./mycli -ABC=elephant first second
flag A is set
flag B is set
flag C is set with value: elephant
$ ./mycli first second -ABC elephant
flag A is set
flag B is set
flag C is set with value: elephant

So why does ./mycli -ABC elephant first second not work? It seems like it should have no problem noticing that the last flag in the bundle, -C, takes an argument, and therefore not consider elephant as a command name, but as an argument to -C.

Is this a bug, a missing feature, or intended behavior?

Thanks!

@marckhouzam
Copy link
Collaborator

This sounds familiar. You may want to look at past issues.

Does ./mycli -C elephant first second work?

@nicula-stripe
Copy link
Author

@marckhouzam

Does ./mycli -C elephant first second work?

Yes

@marckhouzam
Copy link
Collaborator

Sounds like a bug then

inicula added a commit to inicula/cobra that referenced this issue Sep 14, 2024
inicula added a commit to inicula/cobra that referenced this issue Sep 14, 2024
inicula added a commit to inicula/cobra that referenced this issue Sep 14, 2024
inicula added a commit to inicula/cobra that referenced this issue Sep 14, 2024
inicula added a commit to inicula/cobra that referenced this issue Sep 14, 2024
@inicula inicula linked a pull request Sep 14, 2024 that will close this issue
@inicula
Copy link

inicula commented Sep 14, 2024

I gave it a go myself in #2189.

inicula added a commit to inicula/cobra that referenced this issue Sep 14, 2024
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