3

Using CommandLineParser I can define a set of mutually exclusive options like this (taken from the wiki):

class OptionsMutuallyExclusive
{
  //define commands in set(group) named web
  [Option(SetName = "web")]
  public string DocumentRoot { get; set; }
  [Option(SetName  = "web")]
  public bool EnableJavaScript { get; set; }

  //define other commands in set(group) named ftp
  [Option(SetName  = "ftp")]
  public string FtpDirectory { get; set; }
  [Option(SetName  = "ftp")]
  public bool AnonymousLogin { get; set; }

  //Common: the next option can be used with any set because it's not included in a set
  [Option('r')]
  public string UrlAddress { get; set; }
}

Now I can parse this my app like this (.Dump() coming from LinqPad)

var result = parser.ParseArguments<OptionsMutuallyExclusive>(args);
result.WithParsed(options => options.Dump()).WithNotParsed(errs => errs.Dump());

And will get a positive result (WithParsed) using the following args:

--enablejavascript --documentroot ~/var/local/website -r http://localhost
--ftpdirectory ~/var/local/ftpsite --anonymouslogin -r ftp://myftp.server

And will get an error (WithNotParsed) on this:

--enablejavascript --anonymouslogin

But: Is there any way to tell, which set was used?

So getting the value web on the first call and the value ftp on the second one? Or by using an interface and/or derived classes getting an typed result like WebOptions and FtpOptions?

Using different classes I can call ParseArguments multiple times until it succeeds but this isn't really nice.

All examples I could find are always trying to determine the current set by testing which values are defined and which are not.

Is there any thing I'm missing?

Any alternative?

sc911
  • 1,130
  • 10
  • 18

0 Answers0