Skip to content

Document exit codes in CLI help output and return exit code 2 for input errors#1546

Merged
garrytrinder merged 5 commits intomainfrom
copilot/fix-exit-codes-validation-errors
Feb 26, 2026
Merged

Document exit codes in CLI help output and return exit code 2 for input errors#1546
garrytrinder merged 5 commits intomainfrom
copilot/fix-exit-codes-validation-errors

Conversation

Copy link
Contributor

Copilot AI commented Feb 25, 2026

Validation errors (bad IP, invalid log level, etc.) were reported as returning exit code 0. Verified that with current System.CommandLine 2.0.0-beta5, all validators using input.AddError() already return exit code 1 correctly. The remaining gaps were that exit codes were undocumented in --help, and input validation errors were not distinguished from runtime errors.

  • ExitCodeHelpAction: Wraps the default HelpAction to append an exit codes section to all help output (root + subcommands):
    Exit codes:
      0  Success
      1  Runtime error
      2  Invalid input or usage
    
  • DevProxyCommand.ConfigureCommand(): Replaces the HelpOption's action with the wrapper after all options/commands are registered
  • DevProxyCommand.InvokeAsync(): Maps parse/validation errors to exit code 2, distinguishing them from runtime errors (exit code 1), following conventions from curl, git, and others

Verified behavior:

  • devproxy --help → exit code 0
  • devproxy --ip-address "not-an-ip" → exit code 2
  • devproxy --bogus → exit code 2
  • devproxy --log-level bogus → exit code 2
  • devproxy --timeout abc → exit code 2
Original prompt

This section details on the original issue you should resolve

<issue_title>Fix exit codes: validation errors return 0 instead of non-zero</issue_title>
<issue_description>Parent: #1534

Problem

Option validation errors return exit code 0 instead of a non-zero value. For example:

devproxy --ip-address "not-an-ip"
# prints: "not-an-ip is not a valid IP address"
# prints full help text
# exits with code 0

An unknown option (--bogus) correctly returns exit code 1, but option validation failures (bad IP, bad log level, bad timeout value) return 0. This is inconsistent and dangerous for automation.

AI agents branch on exit codes to determine success/failure. When a validation error returns 0, the agent proceeds as if the command succeeded, leading to cascading failures.

Rationale

Exit codes are the most basic contract between a CLI and its callers. Every CI pipeline, shell script, and AI agent relies on non-zero exit codes to detect failure. The current behavior violates this universal expectation.

Proposed solution

  1. Validation errors should return exit code 1 (or consider exit code 2 to distinguish input errors from runtime errors, following conventions from curl, git, and others)
  2. Ensure all validators in DevProxyCommand.ConfigureCommand() produce non-zero exits when input.AddError() is called — this may be a System.CommandLine configuration issue rather than a per-validator fix
  3. Document exit codes in root --help:
    Exit codes:
      0  Success
      1  Runtime error
      2  Invalid input or usage
    
  4. Verify all subcommands follow the same convention

Affected code

  • DevProxy/Commands/DevProxyCommand.cs — all validators using input.AddError()
  • Potentially a System.CommandLine middleware/configuration issue
    </issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…rs return non-zero exit codes

Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix exit codes for validation errors returning 0 Document exit codes in CLI help output Feb 25, 2026
Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 25, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • aka.ms
    • Triggering command: ./devproxy ./devproxy --help (dns block)
    • Triggering command: ./devproxy ./devproxy --ip-address not-an-ip (dns block)
    • Triggering command: ./devproxy ./devproxy --bogus (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title Document exit codes in CLI help output Document exit codes in CLI help output and return exit code 2 for input errors Feb 25, 2026
@waldekmastykarz waldekmastykarz marked this pull request as ready for review February 25, 2026 13:21
@waldekmastykarz waldekmastykarz requested a review from a team as a code owner February 25, 2026 13:21
Copilot AI review requested due to automatic review settings February 25, 2026 13:21
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves Dev Proxy’s CLI contract by documenting exit codes in --help output and consistently distinguishing invalid input/usage from runtime failures via exit code mapping.

Changes:

  • Add a help-action wrapper that appends an “Exit codes” section to CLI help output.
  • Map parse/validation errors (parseResult.Errors) to exit code 2 (invalid input/usage), keeping runtime errors as 1.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
DevProxy/Commands/ExitCodeHelpAction.cs Adds a HelpAction wrapper that appends the standardized exit-codes section to help output.
DevProxy/Commands/DevProxyCommand.cs Wraps the help option’s action with ExitCodeHelpAction and maps parse/validation errors to exit code 2 after invocation.

Copy link
Contributor

@garrytrinder garrytrinder left a comment

Choose a reason for hiding this comment

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

Tested: all exit codes correct (0 for --help, 2 for validation errors). ExitCodeHelpAction properly wraps help output with exit code documentation. Code review clean — no regressions.

@garrytrinder garrytrinder merged commit 9635ab1 into main Feb 26, 2026
4 checks passed
@garrytrinder garrytrinder deleted the copilot/fix-exit-codes-validation-errors branch February 26, 2026 11:01
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.

Fix exit codes: validation errors return 0 instead of non-zero

4 participants