Skip to content

How to get Error message? #513

Closed
Closed
@IB38

Description

@IB38

My snippet:

    class Program
    {
        
        public class Args
        {
            [Option('n', "name", Required = true)]
            public string Name { get; set; }

        }
        
        static void Main(string[] args)
        {

            Args parsedArgs;
            Parser.Default.ParseArguments<Args>(args).WithParsed(result => parsedArgs = result).WithNotParsed(ThrowOnParseError);
        }

        private static void ThrowOnParseError(IEnumerable<Error> errors)
        {
            var excList = new List<Exception>();
            foreach (var err in errors)
            {
                excList.Add(new ArgumentException(err.ToString()));
            }
            if (excList.Any())
                throw new AggregateException(excList);
        }
    }

I would like to throw errors as ArgumentException with message being the same message that gets printed when error occurs. For example, when option -n is missing, this text is printed:

CmdLineParserTest 1.0.0.0
Copyright c 2019

ERROR(S):
Required option 'n, name' is missing.

-n, --name Required.

--help Display this help screen.

--version Display version information.

I would like to throw ArgumentException with "Required option 'n, name' is missing." being its message.
I looked into HelpText, but im not sure if it's what i need.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions