Skip to content

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 #38

Closed

Description

@matt-edmondson

What's wrong

CommandLineParser.TryTakeValue (TrayApp/Cli/CommandLineParser.cs:189-202) takes args[index + 1] as the option's value without checking what it is. TryReadAppOption calls it at lines 171-176. So when the user forgets a value, the next switch on the command line becomes the value instead.

Failure scenario

Both of these were confirmed with a scratch test against CommandLineParser.TryParse:

  • TryParse(["--reason", "--display"]) succeeds with a single match: -r with the value "--display". For NoSleep, nosleep --reason --display records the reason "--display" and doesn't keep the display awake. There is no error.
  • TryParse(["--reason", "--status"]) succeeds with ShowStatus == false. So nosleep -r --status starts a keep-awake run instead of printing the status report.

ReadDuration (line 132) has the same shape. There, though, the duration parse turns the swallowed switch into an error, so only free-text value options are affected silently.

Why it matters

Every other command-line mistake exits 2 with usage. This one makes the run quietly do something other than what was typed. A user who passes --status to check what's happening ends up starting the tool.

Suggested fix / acceptance criteria

  • In TryTakeValue, if the next argument is a standard alias (StandardAliases) or an alias of an application option, fail with "'--reason' needs a value.".
  • Values that merely start with - but aren't a known switch, such as - or -5, are still accepted.
  • Tests:
    • ["--reason", "--display"] and ["--reason", "--status"] fail to parse with "needs a value", and RunAsync exits 2.
    • ["--reason", "-"] and ["--reason", "some text"] still parse.

Activity

  1. added
    bug
    on Oct 7, 2026
  2. matt-edmondson commented on Oct 7, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions