1
Vote

Problem with IgnoreUnknownArguments

description

I found a bug in the implementation of the parser classes for IgnoreUnknownArguments. If the unrecognized argument is a single bad command line argument followed by a good command line argument, the good command line argument will be skipped. Example (assume all command line arguments are binary:

MyApp.exe --Good1 --Bad1 --Good2 --Good3

If --Bad1 is not recognized, then --Good2 two will be skipped, but --Good3 will be read.

Solution:
The reason this happens is that parser implementations assume there will not be any bad binary command line parameters so if a bad command line parameter is encountered, parser implementations return a status of PresentParserState.MoveOnNextElement. I tested the following solution in LongOptionParser and it worked:

Change:
            var option = map[optionGroup.Current];
            if (option == null)
            {
                return _ignoreUnkwnownArguments ? PresentParserState.MoveOnNextElement : PresentParserState.Failure;
            }
To:
            var option = map[optionGroup.Current];
            if (option == null)
            {
                return _ignoreUnkwnownArguments ? PresentParserState.Undefined : PresentParserState.Failure;
            }
Also, rather than using the Undefined state, I'd recommend implementing a completely new state for Ignored arguments by adding an Ignore state as follows:
internal enum PresentParserState : ushort
{
    Undefined = 0x00,
    Success = 0x01,
    Failure = 0x02,
    Ignore = 0x03,
    MoveOnNextElement = 0x04
}

comments