Skip to content

How to correctly implement plugin support where each plugin has its own options? #283

Open
@alexanderfast

Description

@alexanderfast

In my case plugins are dynamically loaded from other assemblies, each plugin supplies its own options instance and the entire commandline is parsed once for each plugin. This requires IgnoreUnknownArguments to be true, since each parsing must be able to handle arguments intended for another plugin.

Because of the way IgnoreUnknownArguments is implemented this becomes a problem. It assumes that every argument has a value, so if an unknown argument is in fact a bool the next key is swallowed offsetting the rest of the parsing by one. In the example below, Bar gets no value and its value is added to the value list instead.

Is there a way around this? Or is there a better way to approach plugin support?

public class OptionsPluginA
{
    [Option("bar")]
    public string Bar { get; set; }

    [ValueList(typeof(List<string>))]
    public IList<string> Values { get; set; }
}

public class OptionsPluginB
{
    [Option("foo")]
    public bool Foo { get; set; }
}

class Program
{
    static void Main()
    {
        var args = "--foo --bar bv v1 v2".Split(' ');
        var parser = new Parser((ParserSettings ps) => { ps.IgnoreUnknownArguments = true; });

        var optionsA = new OptionsPluginA();
        parser.ParseArgumentsStrict(args, optionsA); // Bar = null, Values = bv v1 v2

        var optionsB = new OptionsPluginB();
        parser.ParseArgumentsStrict(args, optionsB); // Foo = true
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions