Repository navigation
Don't treat unknown short option packs as help requests - #1000
Open
ravindudanthanarayana wants to merge 1 commit into
Open
ravindudanthanarayana wants to merge 1 commit into
ravindudanthanarayana wants to merge 1 commit into
Conversation
Any single-dash input is split into a pack of short names, and the built-in flag check matched `-h` against each member. As a result, an input like `-path` printed help and exited successfully instead of reporting an unknown option, so typos went unnoticed by scripts. Only count a help name from a pack when the other members are help names or are valid for the current command. `-vh` and `-help` still show help.
ravindudanthanarayana
marked this pull request as ready for review
November 1, 2026 11:15
ravindudanthanarayana
requested review from
natecook1000 and
rauhul
as code owners
November 1, 2026 11:15
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Single-dash input that isn't a valid option can print the help screen and exit 0 instead of failing, if it happens to contain an
h. Related to #325, where a quoted argument starting with-and containing a URL printed help.Original behavior
Using the
mathexample:The input isn't reported, and a script sees success. The same happens for
-hey,-xzh,"- [a](https://b)", and at any level of the command tree (math -path,math stats average -path).-pathdoesn't request help;-patcorrectly fails withUnknown option.Minimal reproduction
Root cause
parseIndividualArgsplits any single-dash input into a complete element plus one element per character, since it could be a pack of short names.checkForBuiltInFlagsthen usesSplitArguments.contains(anyOf:), which looks at every element without distinguishing pack members, so thehin-pathmatches the-hhelp name.Implementation
contains(anyOf:isRecognized:)still matches complete elements as before. A match that comes from a pack now only counts if one of these is true:-helpworking, which the comment incheckForBuiltInFlagsalready calls out.The second check needs the current command's
ArgumentSetbecausecheckForBuiltInFlagsalso runs after a decoding failure, when flags haven't been removed from the input yet. Without it,-vhon a command missing a required argument would report the missing argument instead of showing help. The set is only built when a pack contains a help name, so ordinary parsing doesn't pay for it.Behavior changes to review
-path,-heyUnknown option '-path', exit 64-vzh(-vvalid,-znot)Unknown option '-z', exit 64-h,--help,-help,-vh,-hv,-hh-vhwith-von the default subcommand-helpwhenhelpNamesis.shortonlyUnknown option '-help'-help-hiddenUnknown option '-help-hidden'(--help-hiddenunchanged)Scripts that relied on a malformed pack exiting 0 would now fail, though that was never a documented way to request help. This is a behavior change only; no API changes, and
contains(anyOf:)is internal.Limitations
"- [a](https://b)"parse as a value, which is what Prints help message when I pass a dash symbol even quoted #325 ultimately asks for. It now gets an error rather than a misleading help screen;--is still needed to pass it as a value. I've left Prints help message when I pass a dash symbol even quoted #325 open for that reason.-vhwhere-vbelongs to a non-default subcommand now reports that failure rather than help.Tests
Added to
ShortNameEndToEndTests.swift, covering unknown packs (including custom help names), default subcommands, help names alone, packs of known names through both the normal path and the decode and validation failure paths, and help-like text passed as a value.Validation
macOS, Swift 6.4 (
swiftlang-6.4.0.34.1). Linux and Windows were not run locally.swift test --filter ShortNameEndToEndTestsfails, 7 issues across the 2 new unknown-pack tests. The tests pinning existing help behavior pass.swift testpasses, 580 Swift Testing tests across 7 test runs.swift format lint --strict --recursive Sources Testsreports nothing.This change was developed with AI assistance (Claude Code).
Checklist