2

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?

3
  • 3
    Which version you of CommandLine parser nuget you're using? Commented Feb 5, 2016 at 16:29
  • The latest! ...... Commented Feb 5, 2016 at 16:46
  • <package id="CommandLineParser" version="1.9.71" targetFramework="net45" /> Commented Feb 5, 2016 at 17:03

1 Answer 1

1

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;
        });
Sign up to request clarification or add additional context in comments.

1 Comment

Before someone gets crazy about different version code, that works => foreach (var error in errors.OfType<NamedError>()) { Console.WriteLine("The parameter: '{0}' s not correct!", error.NameInfo); } return 1;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.