32

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; }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Julien Jacobs
  • 2,561
  • 1
  • 24
  • 34
  • How did it work opposed to what you expected, what was the actual behavior? – Joshua Drake May 17 '12 at 17:02
  • 2
    The library failed to parse the arguments if I only pass "gui" with an error indicating that "opt1" is required. I would expect that it works as "gui" is in a different MutuallyExclusiveSet. – Julien Jacobs May 19 '12 at 17:32

1 Answers1

54

As of version 2.0 of CommandLineParser

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.

From version 2.0.x, where the kernel was completely rewritten, the feature is always active and I'll show a simple example:

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

Invalid:

$ 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

Prior to version 2.0

All the options that belong 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 those 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.)

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
gsscoder
  • 3,088
  • 4
  • 33
  • 49
  • Thank you very much for the detailed answer! I had the feeling that I misunderstood the concept...I ended up adding a Validate function to my options class to check the arguments. That will do the trick until a future feature potentially addresses this case. – Julien Jacobs May 27 '12 at 17:00
  • 4
    This is the exact opposite of how [the wiki](https://github.com/gsscoder/commandline/wiki/Mutually-Exclusive-Options) says it works, but it is how it appears to work. – Rawling Mar 07 '14 at 13:42
  • Is it possible to use commandline parser to require that one and only one of -a or -b is specified. Right now it appears that the behaviour requires at most one of -a or -b. – Michael J Swart Sep 24 '14 at 18:28
  • 1
    Until 2.0.* became `stable` (now it's `beta`) there's a specific section of the **wiki** dedicated to that new release: https://github.com/gsscoder/commandline/wiki/Latest-Version. When it became stable everything will be updated. – gsscoder Aug 30 '15 at 06:50
  • 6
    @gsscoder your post still doesn't explain (at least to me) how to use a combination of `Required` and `SetName`. – lightxx Oct 13 '15 at 07:38
  • @lightxx, from **2.0**: things are more simple. Options with same set name can be specified togheter, if you include another linked to antoher set name parsing will fail. – gsscoder Oct 26 '15 at 07:18
  • 1
    @gsscoder Thanks for your answer. However, even with version 2.x, the problem remains that you can't have a `Required` option together with a SetName. Let's say I have a `FooSet` and a `BarSet`. Now whenever `FooOption` is used, I want to mandatory enforce that `SecondFooOption` is used by decorating it with `Required`. Now I cannot use `BarSet` options any longer (even when no `FooSet` option is being used) because parsing fails because of `SecondFooOption` being decorated as `Required`. – lightxx Oct 27 '15 at 10:59
  • Sticking to your example above, let's say we had boolean `UseWebMode` (`SetName = "web"`) and `UseFtpMode` (`SetName = "ftp"`) parameters. Now IF, and only IF, `UseWebMode` is set, it is mandatory to provide the `WebUrl` parameter. How would I do this? – lightxx Oct 27 '15 at 11:01
  • @lightxx, please be patient, can you reformulate the question? – gsscoder Nov 02 '15 at 06:39
  • @JulienJacobs: It's been a while I know, but do you recall how you implemented that `Validate()` function? And how you got it to pass a failure to the parser? – InteXX Jun 10 '16 at 04:06
  • @lightxx: I'm just getting started with this, but my first guess at solving your `web/ftp` problem would be to use verbs. Like so: https://github.com/gsscoder/commandline/wiki/Verb-Commands – InteXX Jun 10 '16 at 04:11
  • @gsscoder: How can I implement a validation function for the arguments? And how can I get it to pass a failure to the parser? – InteXX Jun 10 '16 at 05:04
  • @InteXX I simply added a public Validate method to my CommandLineOptions (inheriting from CommandLineOptionsBase) in which I was testing the parameter values depending on my needs (i.e. `if "gui" is not set then check that opt1 and opt2 are set`) In the app I was simply calling my Validate function in addition to ParseArguments. Something like `if (parser.ParseArguments(args, options) && options.Validate())` – Julien Jacobs Jun 13 '16 at 14:17
  • @JulienJacobs: `... && options.Validate()` Makes sense, thanks :-) – InteXX Jun 13 '16 at 19:18
  • @lightxx I'm a bit late to the party, but the behaviour you're looking for seems to work if you set `Required=true` for options in both sets. I had a required property in set `A` and an optional property in set `B`. When passing in the optional property, I got an error that the mandatory one from the other set was required. But if the set `B` property was also marked as mandatory, the issue went away. The help text does end up confusing though, as it indicates that both are mandatory. – nullPainter May 17 '21 at 22:50