dotnet / dotnet/command-line-api

CommandLineException with both exit code and message

Open
#350 8 comments 0 reactions 0 assignees View on GitHub
Area-App Models enhancement
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

Would it potentially make sense for one or all of the app models to be able to throw a `CommandLineException` with a message to be printed in red and an exit code to be returned?

This would differ from throwing any other exception in two ways:

- An exit code can be specified.
- An entire stack trace (`ex.ToString()`) would never be printed like it would for an unhandled exception. Throwing `CommandLineException` means you want only the message to be printed.

Instead of returning `int` or `Task` the user could return `void` or `Task`. Instead of returning an int, the user could use `throw new CommandLineException("Message to explain the exit code", 42);`.

This could halve the number of overloads for things like CommandHandler.Create. It currently has four overloads for each arity; e.g., `Action`, `Func`, `Func`, `Func>`.

```cs
namespace System.CommandLine
{
public sealed class CommandLineException : Exception
{
public int ExitCode { get; }

public CommandLineException(string message, int exitCode = 1) : base(message)
{
ExitCode = exitCode;
}
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.