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?

Elisabeth
  • 20,496
  • 52
  • 200
  • 321

1 Answers1

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;
        });
vendettamit
  • 14,315
  • 2
  • 32
  • 54
  • 2
    Before someone gets crazy about different version code, that works => foreach (var error in errors.OfType()) { Console.WriteLine("The parameter: '{0}' s not correct!", error.NameInfo); } return 1; – Elisabeth Feb 05 '16 at 21:13