dotnet / dotnet/command-line-api
CommandLineException with both exit code and message
- 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
Assessment
This issue has not been assessed yet.