integrii / flaggy

Idiomatic Go input parsing with subcommands, positional values, and flags at any position. No required project or package layout and no external dependencies.
The Unlicense
855 stars 30 forks source link

Duplicate Results in Slice flags when SubCommand Used #65

Open bensallen opened 4 years ago

bensallen commented 4 years ago

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 commented 9 months ago

I have the same issue:

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