flaggy icon indicating copy to clipboard operation
flaggy copied to clipboard

Duplicate Results in Slice flags when SubCommand Used

Open bensallen opened this issue 4 years ago • 1 comments

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.

bensallen avatar Mar 07 '20 21:03 bensallen

I have the same issue:

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

richie-tt avatar Nov 28 '23 12:11 richie-tt