Command Line Parser with mutually exclusive required parameters

17,597

All the options that belongs to a mutually exclusive set are mutually exclusive between them. Follow this example:

class Options {
  [Option("a", null, MutuallyExclusiveSet="zero")] 
  public string OptionA { get; set; }
  [Option("b", null, MutuallyExclusiveSet="zero")] 
  public string OptionB { get; set; }
  [Option("c", null, MutuallyExclusiveSet="one")] 
  public string OptionC { get; set; }
  [Option("d", null, MutuallyExclusiveSet="one")] 
  public string OptionD { get; set; }
}

With these rules following command lines are valid:

$ app -a foo -c bar
$ app -a foo -d bar
$ app -b foo -c bar
$ app -b foo -d bar

and these aren't:

$ app -a foo -b bar
$ app -c foo -d bar
$ app -a foo -b bar -c foo1 -d foo2

As you can see you can't specify options together that belong to the same set. Remember also that prebuilt singleton (CommandLineParser.Default) don't work with MutualliyExclusiveSet attribute. You need to dress up a parser by your own:

if (new CommandLineParser(new CommandLineParserSettings {
                            MutuallyExclusive = true,
                            CaseSensitive = true,
                            HelpWriter = Console.Error}).ParseArguments(args, opts) {
  // consume values here
  Console.WriteLine(opts.OptionA);
}

This is the way mutually exclusive options work in Command Line Parser Library. Anyway to solve your specific problem, I suggest you to define all the options as you would do in a normal console application. Then add the Gui boolean switch. If this option is specified ignore others. If not behave as a normal console app.

(Another thing: in a subsequent version will be a feature called "subcommands" that will let you manage multiple Options types; this maybe the right case for this upcoming feature.)

EDIT 2015-08-30:

The feature as implemented in 1.9.x stable always created confusion, was disabled by default and required the developer to activate it via settings instance.

Version 2.0.x, where the kernel was completely rewritten, the feature is always active and I'll try to show a simple example (remember that public API is changed since 2.0.x is a major release update).


class Options {
  [Option(SetName = "web")]
  public string WebUrl { get; set; }
  [Option(SetName = "web")]
  public int MaxLinks { get; set; }

  [Option(SetName = "ftp")]
  public string FtpUrl { get; set; }
  [Option(SetName = "ftp")]
  public int MaxFiles { get; set; }

  [Option]
  public bool Verbose { get; set; }
}

Set from ftp set are not compatible with the ones from web, --verbose (which doesn't belong to a set, or better belongs to the default one "" is neutral and can be intermixed at will). Valid:


$ app --weburl http://stackoverflow.com --maxlinks 99
$ app --ftpurl ftp://ftp.myoffice.files.com --maxfiles 1234
$ app --verbose --weburl http://twitter.com --maxlinks 777
$ app --ftpurl ftp://ftp.xyz.org --maxfiles 44 --verbose
$ app --verbose

Not valid:


$ app --weburl http://stackoverflow.com --maxlinks 99 --ftpurl ftp://ftp.xyz.org
$ app --ftpurl ftp://ftp.myoffice.files.com --maxfiles 1234 --maxlinks 777
$ app --verbose --weburl http://twitter.com --maxfiles 44
$ app --maxfiles 44 --maxlinks 99
Share:
17,597
Julien Jacobs
Author by

Julien Jacobs

.Net Architect and Team Manager. Architecture and development of enterprise systems with Microsoft technologies, along with team and project management. .Net Core, C#, SQL Server, Azure, Angular, Azure DevOps, Git

Updated on June 03, 2022

Comments

  • Julien Jacobs
    Julien Jacobs almost 2 years

    I started to use the CommandLine Parser Library for a tool that will have both a GUI and a command line execution. Launching the GUI is done via a command line option.

    I would therefore like to have required options in case the program is executing in command line mode. Basically, I would want Option 1 and Option 2 to be required if the option "Gui" is not set.

    I tried to combine the MutuallyExclusiveSet and Required attributes as shown below, but it does not work as I thought. Did I misunderstand the concept of "MutuallyExclusiveSet" or simply misusing it? Or is it something that the library is not yet supporting?

    public class CommandLineOptions : CommandLineOptionsBase
    {
        [Option(null, "gui", Required = false, HelpText = "Launch the GUI", MutuallyExclusiveSet = "Gui")]
        public bool Gui { get; set; }
    
        [Option(null, "opt1", HelpText = "Option 1", MutuallyExclusiveSet = "CommandLine", Required = true)]
        public string Option1 { get; set; }
    
        [Option(null, "opt2", HelpText = "Option 2", MutuallyExclusiveSet = "CommandLine", Required = true)]
        public string Option2 { get; set; }
    }