Skip to content

Fail a value option followed by a switch instead of taking the switch as its value [patch] - #43

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/38-value-option-swallows-switch
Oct 9, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/38-value-option-swallows-switch

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #38

What changed

CommandLineParser.TryTakeValue took args[index + 1] as the option's value without checking it. A forgotten value therefore swallowed the next switch, with no error:

  • --reason --display recorded the reason "--display" and dropped the flag.
  • -r --status started a keep-awake run instead of printing the status report.

Now, if the next argument is a standard alias (StandardAliases) or an alias of a registered application option, parsing fails with '--reason' needs a value. and RunAsync exits 2, the same as every other command-line mistake. Values that only start with - but aren't a known switch, such as - or -5, are still accepted.

--for goes through the same TryTakeValue, so --for --status now reports the missing value. Before, it gave the misleading "'--status' is not a duration."

Tests

  • TryParse_WithAnApplicationValueOptionFollowedByASwitch_Fails… covers --display, -d, --status and -t.
  • TryParse_WithADurationFollowedByASwitch_SaysTheValueIsMissing.
  • TryParse_WithAValueThatIsNotAKnownSwitch_TakesIt covers -, -5 and some text.
  • RunAsync_WithAValueOptionMissingItsValueBeforeStatus_ExitsTwo checks that the run exits 2 and OnStart never runs. It passes --no-tray --for 0.05s so that, if the bug comes back, the test fails quickly instead of hanging in a run that never ends.
  • With the parser fix reverted, all of these fail. With it in place, the full suite passes (148 passed, 1 skipped as before, net10.0 on Linux), and the library builds for net10.0 and net9.0.

🤖 Generated with Claude Code

https://claude.ai/code/session_011eecgtNA1yFm2UXzPrxvEg


Generated by Claude Code

… as its value [patch]

TryTakeValue took the next argument as the option's value without looking
at it, so a forgotten value swallowed the next switch: "--reason --display"
recorded the reason "--display" and dropped the flag, and "-r --status"
started a run instead of reporting status. When the next argument is a
standard alias or an application option's alias, parsing now fails with
"'--reason' needs a value." and the run exits 2. Values that only start
with '-', such as "-" or "-5", are still taken. --for gets the same check.

Fixes #38

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011eecgtNA1yFm2UXzPrxvEg
@sonarqubecloud

sonarqubecloud Bot commented Oct 9, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit f07439b into main Oct 9, 2026
14 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.

A value option takes the next argument even when it's a switch: "--reason --display" sets the reason to "--display" and silently drops the flag

2 participants