alecthomas / kong

Kong is a command-line parser for Go
MIT License
2.15k stars 140 forks source link

fix: When a Grammar combines flags with passthrough args, see if an unrecognized flag may be treated as a positional argument #435

Closed boblail closed 4 months ago

boblail commented 4 months ago

Issue

Given a grammar like this:

var cli struct {
        Args []string `arg:"" optional:"" passthrough:""`
}

The first positional argument implies that it was preceded by --, so subsequent flags are not parsed.

If Kong parses cli 1 --unknown 3, it will populate Args with []string{"1", "--unknown", "3"}. However, if Kong parses cli --unknown 2 3, it will fail saying that --unknown is an unrecognized flag.

Proposal

This commit changes the parser so that if an unknown flag could be treated as the first passthrough argument, it is.

After this change, if Kong parses cli --unknown 2 3, it will populate Args with []string{"--unknown", "2", "3"}.

alecthomas commented 4 months ago

Thanks!