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

Strange interaction with Count and cobra #112

Closed
ncw opened this issue Feb 9, 2017 · 6 comments
Closed

Strange interaction with Count and cobra #112

ncw opened this issue Feb 9, 2017 · 6 comments

Comments

@ncw
Copy link

ncw commented Feb 9, 2017

Compile this code.

Here is the guts of it

var (
	verbose = pflag.CountP("verbose", "v", "Print more stuff")

	RootCmd = &cobra.Command{
		Use: "test",
		Run: func(cmd *cobra.Command, args []string) {
			fmt.Printf("ROOT verbose = %d, args = %v\n", *verbose, args)
		},
	}

	testCmd = &cobra.Command{
		Use: "test",
		Run: func(cmd *cobra.Command, args []string) {
			fmt.Printf("TEST verbose = %d, args = %v\n", *verbose, args)
		},
	}
)

func init() {
	RootCmd.AddCommand(testCmd)
}

func main() {
	if err := RootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(-1)
	}
}

Now run with various arguments

Note this has run the ROOT command not the TEST command

$ ./flags -v test
ROOT verbose = 1, args = [test]

This is OK

$ ./flags test potato
TEST verbose = 0, args = [potato]

Why does this produce an error?

$ ./flags -v test potato
Error: unknown command "potato" for "test"
Run 'test --help' for usage.
unknown command "potato" for "test"

This seems to work though

$ ./flags -vv test potato
TEST verbose = 2, args = [potato]

It is as if the -v on it's own eats an argument, but it isn't as simple as that as this works fine

 ./flags -v potato
ROOT verbose = 1, args = [potato]
@moorereason
Copy link

Don't know the cause, but you should differentiate the Use field in the root command to avoid confusion.

RootCmd = &cobra.Command{
	Use: "root", // instead of "test"
$ ./flags -v test potato
Error: unknown command "potato" for "root"
Run 'root --help' for usage.
unknown command "potato" for "root"

More interesting behavior:

$ ./flags -v potato test
TEST verbose = 1, args = [potato]

@eparis
Copy link
Collaborator

eparis commented Feb 9, 2017

I'm kind of poking at this, but ./flags test -v potato works just fine (I moved the -v)

@eparis
Copy link
Collaborator

eparis commented Feb 9, 2017

The bug appears to be that stripflags() is considering test to be an argument to -v.

I think the test for isBooleanFlag() is incorrect...

@eparis
Copy link
Collaborator

eparis commented Feb 9, 2017

give me a couple minutes to send a potential patch.

@eparis
Copy link
Collaborator

eparis commented Feb 9, 2017

I believe this would be fixed by spf13/cobra#391

@ncw
Copy link
Author

ncw commented Feb 9, 2017

@eparis I tried the patch with my original problem and it fixes it - thank you :-)

ncw added a commit to rclone/rclone that referenced this issue Feb 11, 2017
This is a temporary fix until this pull request gets merged

spf13/cobra#391

See original ticket

spf13/pflag#112
@n10v n10v closed this as completed Apr 18, 2017
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

No branches or pull requests

4 participants