Skip to content

CommandLineException with both exit code and message #350

Open
@jnm2

Description

@jnm2

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<int> 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<T>, Func<T, int>, Func<T, Task>, Func<T, Task<int>>.

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

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions