question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for mutual exclusive options

See original GitHub issue

Something like this:

@Command(autoHelp = true, sortOptions = false, groups = {
@OptionGroup(name = "dest", required = true, exclusive = true,
           heading = "Destination: The data can be sent either to a file or to a socket"),
@OptionGroup(name = "other",
           heading = "Other:%n  Some other options")})
class ExclusiveOptionsExample {

  @Option(names = "-f", description = "Target file", group = "dest")
  File file;
  
  @Option(names = "-s", description = "Target socket", group = "dest")
  InetAddress adr;
  
  @Option(names = "-d", description = "Data file", group = "other")
  File data;
}

Usage help:

Usage: <main class> [-hV] [-d=<data>] (-f=<file> | -s=<adr>)
Destination: The data can be sent either to a file or to a socket
  -f= <file>                  Target file
  -s= <adr>                   Target socket
Other:
  Some other options
  -d= <data>                  Data file
  -h, --help                  Show this help message and exit.
  -V, --version               Print version information and exit.
Commands:
  help  Displays help information about the specified command

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:10
  • Comments:16 (11 by maintainers)

github_iconTop GitHub Comments

5reactions
remkopcommented, Feb 13, 2019

A first cut of support for ArgGroups has landed in master. This adds support for:

  • mutually exclusive options and positional parameters (exclusive = true)
  • co-occurring options and positional parameters (exclusive = false: require all elements in the group, or none) as requested in #295
  • composite groups
  • groups can be required or optional

Example:

 @Command(argGroups = {
         @ArgGroup(name = "EXCL",      exclusive = true,  required = true),
         @ArgGroup(name = "ALL",       exclusive = false, required = true),
         @ArgGroup(name = "COMPOSITE", exclusive = false, required = false,
                   subgroups = {"ALL", "EXCL"}),
 })
 class App {
     @Option(names = "-x", groups = "EXCL") boolean x;
     @Option(names = "-y", groups = "EXCL") boolean y;
     @Option(names = "-a", groups = "ALL") boolean a;
     @Option(names = "-b", groups = "ALL") boolean b;
 }

This defines a composite group with the following synopsis:

 [(-x | -y) (-a -b)]

The composite group consists of the required exclusive group (-x | -y) and the required co-occurring group (-a -b).

Valid user input for this group would be:

  • no options at all (because the composite group itself is non-required)
  • -x -a -b (in any order)
  • -y -a -b (in any order)

Any other combination of -x, -y, -a and -b would give a validation error.


Still TODO:

  • show group synopsis in usage help message
  • show group headings in options list (as requested in #450)
  • resource bundle support for group headings in options list (related to #450)
  • update annotation processor in picocli-codegen to build a model from the new annotations at compile time
  • documentation in user manual and release notes

That said, the majority of the functionality is there. I would appreciate it if people would try this to see if it meets their requirements and to find any places where the API, error messages or anything else should be improved.

1reaction
lenkitecommented, Apr 28, 2018

Also: Sometimes its common to have a single option as an alternative with a group of options. Like in the below -u, -p, -c are only required if -P (profile) is not present.

list-nodes -c <controlHost> -u <user> -p <pass>  OR list-nodes -P <profileName>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mutually exclusive command line options - python
I want to achieve something like if -i option and "ifile" is present or -p and fcsname is present..at input, then it is...
Read more >
Mutually Exclusive: What It Means, With Examples
If considering mutually exclusive options, a company must weigh the opportunity cost, or what it would be giving up by choosing each option....
Read more >
mutuallyExclusiveOptions
Declare a set of two or more mutually exclusive options. If none of the options are given on the command line, the value...
Read more >
Mutually Exclusive Campaigns
Click the gear icon ⚙ on the top right, and go to CAMPAIGN SETTINGS. Under the Mutually Exclusive Groups section, click Create mutually...
Read more >
Mutually Exclusive Events - Math is Fun
Let's look at the probabilities of Mutually Exclusive events. But first, a definition: Probability of an event happening = Number of ways it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found