5

Is there any way to make the Command Line Parser library report unknown arguments?

Given the following options class:

public class Options
{
    [Option('i', "int-option", DefaultValue = 10, HelpText = "Set the int")]
    public int IntOption { get; set; }

    [ParserState]
    public IParserState LastParserState { get; set; }

    [HelpOption]
    public string GetUsage()
    {
        return HelpText.AutoBuild(this, 
            HelpText.DefaultParsingErrorsHandler(this, current));
    }
}

And the following program:

var options = new Options();
var parser = new Parser(settings =>
{
    settings.HelpWriter = Console.Error;
    settings.IgnoreUnknownArguments = false;
});

if (parser.ParseArgumentsStrict(args, options))
{
    Console.WriteLine("Int value set: {0}", options.IntOption);
}

When calling the program with "MyProgram.exe --unknown" I just get the default usage information, but no mention of what error made the parsing fail. I'd like to get some kind of indication to the user what went wrong.

PHeiberg
  • 29,411
  • 6
  • 59
  • 81
  • 1
    For reference, issue is **fixed** in ``develop`` branch. Instructions here: https://github.com/gsscoder/commandline/wiki/Latest-Beta – jay Mar 04 '13 at 15:28

1 Answers1

3

Long story short: with the current implementation you can't get any info about the unknown options.

The long story:

If you put a brakepoint into your GetUsage method you will see that the LastParserState is not null but contains 0 element.

LastParserState is basically filled from the ArgumentParser.PostParsingState but the the LongOptionParser (which in your case is involved because of the -- double dash) is not adding anything to the PostParsingState collection inside its parse method:

Source from Github:

var parts = argumentEnumerator.Current.Substring(2).Split(new[] { '=' }, 2);
var option = map[parts[0]];

if (option == null)
{
    return _ignoreUnkwnownArguments ? PresentParserState.MoveOnNextElement : 
                                      PresentParserState.Failure;
}

So internally the parser doesn't store any info about what went wrong just record that fact.

nemesv
  • 138,284
  • 16
  • 416
  • 359
  • 1
    I think it would be an useful feature so I suggest that you should create a feature request because on this question. – nemesv Mar 03 '13 at 20:19
  • Thanks! Good analysis. I checked the LastParserState and came to the conclusion that it wasn't there. Didn't take the time to debug through the source though. – PHeiberg Mar 03 '13 at 20:29
  • See the originating [issue](https://github.com/gsscoder/commandline/issues/64); it may be fixed the current development branch. – jay Mar 04 '13 at 06:36
  • Links are broken. – Ivan Kochurkin Mar 30 '18 at 16:47