Commandline: Mutually Exclusive Options (SetName) not working

Created on 25 Feb 2018  路  2Comments  路  Source: commandlineparser/commandline

2.2.1

Using the default parser, this command line,

command.exe config --url --process

and these options, both the Url and Process properties are set to true and there is no error.

    [Verb("config")]
    public class ConfigOptions : BaseOptions
    {
        [Usage]
        public static IEnumerable<Example> Examples
        {
            get
            {
                yield return new Example("get url", new ConfigOptions { Url = true });
                yield return new Example("set url", new ConfigOptions { Url = true, Value = "https://xxx" });
            }
        }

        [Option("url", SetName = "setting", HelpText = "URL to the account.")]
        public bool Url { get; set; }

        [Option("process", SetName = "setting", HelpText = "Custom project process.")]
        public bool Process { get; set; }

        [Option("field", SetName = "setting", HelpText = "Custom field.")]
        public bool Field { get; set; }

        [Value(0, HelpText = "Value to set.")]
        public string Value { get; set; }
    }

Most helpful comment

It used to work this way prior to version 2.x, so all options sharing the same SetName would be mutually exclusive. From version 2.x SetName defines (sets of) options that belong together, so mutually exclusive options now need to have a different SetName.

Example:

internal class Options
{
    [Option("username", SetName = "auth")]
    public string Username { get; set; }

    [Option("password", SetName = "auth")]
    public string Password { get; set; }

    [Option("guestaccess", SetName = "guest")]
    public bool GuestAccess { get; set; }
}

Now the 'username' and 'password' options with SetName 'auth' can be used together, but are mutually exclusive with the 'guestaccess' option with SetName 'guest'.

All 2 comments

It used to work this way prior to version 2.x, so all options sharing the same SetName would be mutually exclusive. From version 2.x SetName defines (sets of) options that belong together, so mutually exclusive options now need to have a different SetName.

Example:

internal class Options
{
    [Option("username", SetName = "auth")]
    public string Username { get; set; }

    [Option("password", SetName = "auth")]
    public string Password { get; set; }

    [Option("guestaccess", SetName = "guest")]
    public bool GuestAccess { get; set; }
}

Now the 'username' and 'password' options with SetName 'auth' can be used together, but are mutually exclusive with the 'guestaccess' option with SetName 'guest'.

This is great news! I like this much better! Thank you.

Was this page helpful?
0 / 5 - 0 ratings