Repository navigation
fix: stop parsing at unknown flag when non-interspersed - #540
tomasaschan merged 1 commit into
Conversation
When SetInterspersed(false) is combined with PassUnknownFlagToArgs, unknown flags are treated as non-flag arguments and placed into Args(). Previously, both parseShortArg and parseArgs continued parsing after an unknown flag, consuming subsequent known flags instead of passing the remaining arguments unchanged to the wrapped command. Stop shorthand cluster and argument parsing at the first unknown flag when interspersed parsing is disabled, preserving the rest of any shorthand cluster and remaining arguments. Fixes spf13#537 Signed-off-by: duongynhi000005-oss <duongynhi000005@gmail.com>
Ekko82
left a comment
There was a problem hiding this comment.
Verified 9a98e1e on Windows/amd64 with Go 1.27.1 against base f8a99067. I checked 17 cases through both Parse and ParseAll: all 34 checks pass on head, while 22 fail on base.
With SetInterspersed(false) and PassUnknownFlagToArgs, the additional cases confirm that an unknown argument protects the wrapped command's --help, invalid or missing integer values, and literal -- from further parsing. Attached shorthand values and the unprocessed cluster suffix are preserved. A known negative integer before the unknown argument still parses, and the interspersed-mode controls keep their previous behavior.
The added TestPassUnknownFlagToArgsWithNonInterspersed also passes unchanged. Validation covered these targeted probes and the added regression; the full suite, race detector, vet and lint were not run.
Summary
Fixes #537
When
SetInterspersed(false)is configured together withPassUnknownFlagToArgs, unknown flags are treated as non-flag arguments and placed intoArgs(). Previously:parseArgsappended the unknown flag tof.argsand continued parsing, consuming later known flags instead of leaving them for the wrapped command.parseShortArgcontinued scanning remaining characters in a shorthand cluster after encountering an unknown shorthand (e.g.-uk), consuming trailing known shorthands.Changes
parseShortArg, when!f.interspersed, append any remaining characters of the shorthand cluster toUnknownFlagsand stop scanning the cluster.parseArgs, when!f.interspersed, append all remainingargstof.argsand return immediately after recording the unknown flag.TestPassUnknownFlagToArgsWithNonInterspersedcovering long unknown flags (--unknown --known), standalone short unknown flags (-u -k), shorthand clusters (-uk --knownand-ku --after), andParseAll.