Elisabeth
Elisabeth

Reputation: 21206

Get invalid parameters from commandline parser

I use the commandline parser nuget.

var options = new Options();
bool isInputValid = CommandLine.Parser.Default.ParseArguments(args, options);

How do I get the parameters which are invalid?

Upvotes: 2

Views: 2162

Answers (1)

vendettamit
vendettamit

Reputation: 14677

In 1.9.71 I dont' see any option where you can fetch the invalid tokens from arguments after parsing. But if you upgrade to -pre release version i.e.

<package id="CommandLineParser" version="2.0.275-beta" targetFramework="net45" />

This version gives flexibility to do more with parsed results. So you can easily find the invalid token like below:

 var result = CommandLine.Parser.Default.ParseArguments<Options>(args);

 result.MapResult(
        options =>
        {
            // Do something with optios
            return 0;
        },
        errors =>
        {
            var invalidTokens = errors.Where(x => x is TokenError).ToList();
            if(invalidTokens != null)
            {
                invalidTokens.ForEach(token => Console.WriteLine(((TokenError)token).Token));
            }

            return 1;
        });

Upvotes: 1

Related Questions