jessevdk / go-flags

go command line option parser
http://godoc.org/github.com/jessevdk/go-flags
BSD 3-Clause "New" or "Revised" License
2.56k stars 305 forks source link

Support per-command PassAfterNonOption #393

Closed woky closed 3 months ago

woky commented 1 year ago

Currently, PassAfterNonOption is a Parser flag. This means that it applies to all commands. Sometimes it's desirable have this behavior on only a subset of commands. For instance, for restart subcommand, we would like the default behavior of parsing both -v and --wait flags:

mycmd restart -v foo bar --wait

But for exec subcommand, we want only the -v flag to be parsed. The -l flag should be treated as positional argument.

mycmd exec -v ls -l /

Introduce PassAfterNonOption boolean field in Command structure that has the same meaning as the flag in Parser but applies only to the currently active command. The field can be set by adding pass-after-non-option tag to data structure declaration.

This only allows to selectively enable the behavior in commands when PassAfterNonOption Parser flag is unset. When the flag is set, the behavior is enabled for all commands and cannot be disabled by false value of PassAfterNonOption field. This is to keep the code simple and backwards compatible (since PassAfterNonOption field defaults to false).