Skip to content

OptionAttribute IEnumerable<T> possible unintended behaviour #277

Open
@usagirei

Description

@usagirei

Given the class

public class Options{
    private IEnumerable<string> _inputDirs;

    [Option('c', "compress",
        HelpText = "Compress Match Pattern, Pipe Separated (|) ",
        Separator = '|',
        Max = 1,
        Default = new[]
        {
            "*.txt", "*.log", "*.ini"
        })]
    public IEnumerable<string> Compress { get; set; }

    [Value(0,
        HelpText = "Input Directories.",
        Min = 1,
        Required = true)]
    public IEnumerable<string> InputDirs
    {
        get { return _inputDirs; }
        set { _inputDirs = value.SelectMany(Globbing.GlobDirs).ToList(); }
    }

    [Option('n', "name",
        HelpText = "Metadata Name.",
        Default = WILDCARD)]
    public string Name { get; set; }
}

The following happens:

  • If the passed argument is:
    "*" -c "*.*"
    It'll work as intended
  • If the passed argument is:
    "*" -c "*" or a b c -c a or a b c -c a -n b
    Arguments present in the options are removed from the value arguments. However two options can have the same value with no problems.
    In the first case no argument will be passed to the Value.
    In the second, only b and c.
    In the third only c
  • If the passed argument is:
    -c "*" "*.*"
    Compress will receive the two arguments, even if Min and Max are set, it'll consume anything until the next option is found (values get absorbed). IEnumerable is ignoring the Max property, same without Separator property.
  • If the passed argument is:
    -c "*.log|*.txt" "*"
    Compress will receive three arguments, the first one split, plus anything until the next option, meaning you can't have a value after a option IEnumerable is set. Same as above.
  • If the passed argument is:
    -c "*" "*.log|*.txt"
    Compress will receive two arguments, the second one will not get split, meaning the second argument being read is probably a unintended behaviour, shouldn't a Sequence IEnumerable read only one argument?

Tested with 2.0.275-beta

TL;DR: IEnumerable Ignores Max Property, "duplicate" attributes present in a Option Property gets removed from a Value Property

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions