Skip to content

fix: stop parsing at unknown flag when non-interspersed - #540

Merged
tomasaschan merged 1 commit into
spf13:masterfrom
duongynhi000005-oss:fix/pass-unknown-flag-non-interspersed
Nov 7, 2026
Merged

tomasaschan merged 1 commit into
spf13:masterfrom
duongynhi000005-oss:fix/pass-unknown-flag-non-interspersed

Conversation

@duongynhi000005-oss

Copy link
Copy Markdown

Summary

Fixes #537

When SetInterspersed(false) is configured together with PassUnknownFlagToArgs, unknown flags are treated as non-flag arguments and placed into Args(). Previously:

  • parseArgs appended the unknown flag to f.args and continued parsing, consuming later known flags instead of leaving them for the wrapped command.
  • parseShortArg continued scanning remaining characters in a shorthand cluster after encountering an unknown shorthand (e.g. -uk), consuming trailing known shorthands.

Changes

  • In parseShortArg, when !f.interspersed, append any remaining characters of the shorthand cluster to UnknownFlags and stop scanning the cluster.
  • In parseArgs, when !f.interspersed, append all remaining args to f.args and return immediately after recording the unknown flag.
  • Added TestPassUnknownFlagToArgsWithNonInterspersed covering long unknown flags (--unknown --known), standalone short unknown flags (-u -k), shorthand clusters (-uk --known and -ku --after), and ParseAll.

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 Ekko82 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@tomasaschan
tomasaschan merged commit 046b0d8 into spf13:master Nov 7, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PassUnknownFlagToArgs continues parsing after an unknown flag with SetInterspersed(false)

3 participants