Skip to content

Mutually Exclusive Options

jsgbrl edited this page Jan 24, 2018 · 17 revisions

IMPORTANT NOTE: This wiki refers to latest stables, if a beta version is available in github master branch please refer to Latest Version.

You can define options as belonging to a set that is mutually exclusive. Consider an imaginary application that can act as either a web or ftp server, but not both at the same time.

Defining the options class as follows:

class OptionsWithDefaultSet
{
  [Option(MutuallyExclusiveSet = "web")]
  public string DocumentRoot { get; set; }

  [Option(MutuallyExclusiveSet = "web")]
  public bool EnableJavaScript { get; set; }

  [Option(MutuallyExclusiveSet = "ftp")]
  public string FtpDirectory { get; set; }

  [Option(MutuallyExclusiveSet = "ftp")]
  public bool AnonymousLogin { get; set; }
}

and group options in different set. In this way if you combine an ftp option with a web one, parsing will fail. Options in the SAME set set can be combined together, but options cannot be combined across sets. For example, options in the ftp set can be combined, but when options from the web and ftp sets are used together, an incompatible options error is produced.

Please remember to activate the feature as shown in the following snippet.

var options = new OptionsWithDefaultSet();
bool isOk = new Parser(s =>
            {
                s.MutuallyExclusive = true;
            }).ParseArguments(args, options);

Follows behaviour in the terminal.

;; permitted
$ app --enablejavascript --documentroot ~/var/local/website

;; denied
$ app --anonymouslogin --enablejavascript

If you have various sets and each set contains a good number of options, It's better to structure your application with Verb Commands.