Skip to content

Commit

Permalink
[#1673] add example workaround for inherited options DuplicateOptionA…
Browse files Browse the repository at this point in the history
…nnotationException
  • Loading branch information
remkop committed May 11, 2022
1 parent 86e5da8 commit 2ca718c
Showing 1 changed file with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package picocli.examples.inheritedoptions;

import picocli.AutoComplete;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Option;

/**
* Demonstrates how to avoid DuplicateOptionAnnotationsException
* (inspired by https://github.com/remkop/picocli/issues/1673)
* in this case, an inherited --help option clashes with a
* subcommand that already has a --help option.
*/
@Command(name = "top" /*, subcommands = AutoComplete.GenerateCompletion.class*/)
public class DuplicateOptionsWorkaround implements Runnable {

@Option(names = {"-h", "--help"}, scope = CommandLine.ScopeType.INHERIT, description = "Display usage help and exit.")
boolean usageHelpRequested;

public void run() {
System.out.println("Hello, world!");
}

public static void main(String[] args) {
CommandLine top = new CommandLine(new DuplicateOptionsWorkaround());

// create a CommandSpec for the subcommand
CommandSpec sub = CommandSpec.forAnnotatedObject(new AutoComplete.GenerateCompletion());

// remove the duplicate option from the subcommand
sub.remove(sub.findOption("--help"));

// add the subcommand manually
top.addSubcommand(sub);

// and run the application
top.execute(args);
}
}

0 comments on commit 2ca718c

Please sign in to comment.