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

Duplicate Results in Slice flags when SubCommand Used #65

Open
bensallen opened this issue Mar 7, 2020 · 1 comment
Open

Duplicate Results in Slice flags when SubCommand Used #65

bensallen opened this issue Mar 7, 2020 · 1 comment

Comments

@bensallen
Copy link

Modifying the sliceFlag example to include a subcommand like:

package main

import "github.com/integrii/flaggy"

func main() {
	// Declare variables and their defaults
	var stringSliceFlag []string
	var boolSliceFlag []bool

	// Add a slice flag
	flaggy.DefaultParser.AdditionalHelpAppend = "Example: ./sliceFlag -b -b -s one -s two -b=false"
	flaggy.StringSlice(&stringSliceFlag, "s", "string", "A test string slice flag")
	flaggy.BoolSlice(&boolSliceFlag, "b", "bool", "A test bool slice flag")

	testSubcommand := flaggy.NewSubcommand("test")
	testSubcommand.Description = "Testing"
	testSubcommand.ShortName = "test"

	flaggy.AttachSubcommand(testSubcommand, 1)

	// Parse the flag
	flaggy.Parse()

	// output the flag contents
	for i := range stringSliceFlag {
		println(stringSliceFlag[i])
	}

	for i := range boolSliceFlag {
		println(boolSliceFlag[i])
	}
}

Results in:

$ ./main -b -b -s one -s two -b=false
one
two
true
true
false
$ ./main -b -b -s one -s two -b=false test
one
two
one
two
true
true
false
true
true
false

It appears all args are passed to the recursed subcommand here: https://github.com/integrii/flaggy/blob/master/subCommand.go#L280. Perhaps only the arguments to the right of the subcommand in the args should be passed?

Otherwise, perhaps a more thorough approach is to consider the depth of the flag being parsed. If the flag doesn't belong to the current depth then ignore it.

@richie-tt
Copy link

I have the same issue:

github.com/integrii/flaggy v1.5.2
go 1.21

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

2 participants