@Command public class ExitCodeDemo implements Runnable { public void run() { throw new ParameterException(new CommandLine(this), "exit code demo"); } public static void main(String... args) { CommandLine cmd = new CommandLine(new ExitCodeDemo()); cmd.parseWithHandlers( new CommandLine.RunLast().andExit(123), CommandLine.defaultExceptionHandler().andExit(456), args); } }
@Command public void doit(@Mixin CommonOption commonOptionParam, @Option(names = "-z") int z, @Parameters String arg0, String arg1) {}
@Command(name = "language", description = "Resolve ISO language code (ISO 639-1 or -2, two/three letters)") static class Subcommand2 implements Runnable { @Parameters(arity = "1..*", paramLabel = "<code> <code2>", description = "language code(s) to be resolved") private String[] languageCodes; @Override public void run() { for (String code : languageCodes) { System.out.println(String.format("%s: %s", code.toLowerCase(), new Locale(code).getDisplayLanguage())); } } }
@Command static class MixeeInstantiated { @Mixin ValidMixin mixin = new ValidMixin(); } @Command
@Command(footer = "Common footer") public class CommonOption { @Option(names = "-x", description = "reusable option you want in many commands") int x; }
@Command(name = "top", subcommands = Sub.class) static class Top implements Runnable { @Option(names = {"-d", "--directory"}, description = "this option applies to all subcommands") private File baseDirectory; int result; public void run() { } }
@Command(name = "ISOCodeResolve", subcommands = { Subcommand1.class, Subcommand2.class, CommandLine.HelpCommand.class }, description = "Resolve ISO country codes (ISO-3166-1) or language codes (ISO 639-1 or -2)") static class ParentCommand implements Runnable { @Override public void run() { } }
@Command(description = "First level subcommand 1") public static class Sub1 { @Option(names = "--num", description = "a number") double number; @Option(names = "--str", description = "a String") String str; @Option(names = "--candidates", completionCandidates = Candidates.class, description = "with candidates") String str2; } @Command(description = "First level subcommand 2")
@Command class MyUnmatched { @Unmatched List raw; } @Test(expected = InitializationException.class)
@Command(name = "top", subcommands = {Sub.class}) static class Top { @Option(names = "-o", required = true) String mandatory; @Option(names = "-h", usageHelp = true) boolean isUsageHelpRequested; } @Command(name = "sub", description = "This is a subcommand") static class Sub {}
@Command(name = "base", abbreviateSynopsis = true, commandListHeading = "c o m m a n d s", customSynopsis = "cust", description = "base description", descriptionHeading = "base descr heading", footer = "base footer", footerHeading = "base footer heading", header = "base header", headerHeading = "base header heading", optionListHeading = "base option heading", parameterListHeading = "base param heading", requiredOptionMarker = '&', separator = ";", showDefaultValues = true, sortOptions = false, synopsisHeading = "abcd") class Base { }
@Command(name = "mycmd", mixinStandardHelpOptions = true, version = "MyCallable-1.0") static class MyCallable implements Callable<Object> { @Option(names = "-x", description = "this is an option") String option; public Object call() { throw new IllegalStateException("this is a test"); } }
@Command void clone(@Option(names = {"-l", "--local"}) boolean local, @Option(names = "-q") boolean quiet, @Option(names = "-v") boolean verbose, @Option(names = {"-b", "--branch"}) String branch, @Parameters(paramLabel = "<repository>") String repo) { // ... implement business logic }
/** * Helper class encapsulating the command line options and positional parameters. */ @Command(name = "java com.puppycrawl.tools.checkstyle.JavadocPropertiesGenerator", mixinStandardHelpOptions = true) private static class CliOptions { /** * The command line option to specify the output file. */ @Option(names = "--destfile", required = true, description = "The output file.") private File outputFile; /** * The command line positional parameter to specify the input file. */ @Parameters(index = "0", description = "The input file.") private File inputFile; } }
@Command(description = "Second level sub-subcommand 2") public static class Sub2Child2 { @Option(names = {"-u", "--timeUnit"}) private TimeUnit timeUnit; @Option(names = {"-t", "--timeout"}) private long timeout; @Parameters(completionCandidates = Candidates.class, description = "with candidates") String str2; }
@Command(description = "Second level sub-subcommand 1") public static class Sub2Child1 { @Option(names = {"-h", "--host"}, description = "a host") InetAddress host; } @Command(description = "Second level sub-subcommand 2")
/** * Command that clears the screen. */ @Command(name = "cls", aliases = "clear", mixinStandardHelpOptions = true, description = "Clears the screen", version = "1.0") static class ClearScreen implements Callable<Void> { @ParentCommand CliCommands parent; public Void call() throws IOException { parent.reader.clearScreen(); return null; } }
@Command(description = "description from mixinSub", descriptionHeading = "MixinSub Description Heading%n", header = "MixinSub Header", headerHeading = "MixinSub Header Heading%n", //footer = "222", //footerHeading = "222 222", //optionListHeading = "222 222 222", parameterListHeading = "2 22", commandListHeading = "222 2", requiredOptionMarker = '2', synopsisHeading = "22222") static class MixMeInSub extends MixMeInSuper {}
@Command(name = "sub-2", mixinStandardHelpOptions = true) public static class Sub2Command implements Runnable{ @Option(names = "option-2") int option2; @Option(names = "flag-2") boolean flag2; public void run() { System.out.println(String.format("option-2=%d, flag-2=%s", option2, flag2)); } } }
@Command(name = "mycmd", mixinStandardHelpOptions = true, version = "MyRunnable-1.0") static class MyRunnable implements Runnable { @Option(names = "-x", description = "this is an option") String option; public void run() { throw new IllegalStateException("this is a test"); } } private static final String MYCALLABLE_USAGE = format("" +