Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command Line Parser with mutually exclusive required parameters

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; } } 
like image 870
Julien Jacobs Avatar asked May 17 '12 16:05

Julien Jacobs


1 Answers

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 
like image 162
gsscoder Avatar answered Sep 22 '22 10:09

gsscoder