@Parameters(index = "1..*") public void setOtherFiles(List<File> otherFiles) { for (File f : otherFiles) { if (!f.exists()) { throw new ParameterException(spec.commandLine(), "File " + f.getAbsolutePath() + " must exist"); } } this.otherFiles = otherFiles; }
@Command int multiply(@Option(names = "--count") int count, @Parameters int multiplier) { System.out.println("Result is " + count * multiplier); return count * multiplier; }
@Command(name = "i18n-sub", subcommands = CommandLine.HelpCommand.class, mixinStandardHelpOptions = true, description = {"subcmd desc 1", "subcmd desc 2", "subcmd desc 3"}, descriptionHeading = "subcmd desc heading%n", header = {"subcmd header 1", "subcmd header 2", "subcmd header 3"}, headerHeading = "subcmd header heading%n", footer = {"subcmd footer 1", "subcmd footer 2", "subcmd footer 3"}, footerHeading = "subcmd footer heading%n", commandListHeading = "subcmd command list heading%n", optionListHeading = "subcmd option list heading%n", parameterListHeading = "subcmd param list heading%n") public class I18nSubcommand { @Option(names = {"-x", "--xxx"}) String x; @Option(names = {"-y", "--yyy"}, description = {"subcmd yyy description 1", "subcmd yyy description 2"}) String y; @Option(names = {"-z", "--zzz"}, description = "subcmd zzz description") String z; @Parameters(index = "0") String param0; @Parameters(index = "1", description = "subcmd param1 description") String param1; }
@Command(name = "i18n", resourceBundle = "picocli.I18nSuperclass_Messages", subcommands = CommandLine.HelpCommand.class, mixinStandardHelpOptions = true, description = {"super desc 1", "super desc 2", "super desc 3"}, descriptionHeading = "super desc heading%n", header = {"super header 1", "super header 2", "super header 3"}, headerHeading = "super header heading%n", footer = {"super footer 1", "super footer 2", "super footer 3"}, footerHeading = "super footer heading%n", commandListHeading = "super command list heading%n", optionListHeading = "super option list heading%n", parameterListHeading = "super param list heading%n") public class I18nSuperclass { @Option(names = {"-x", "--xxx"}) String x; @Option(names = {"-y", "--yyy"}, description = {"super yyy description 1", "super yyy description 2"}) String y; @Option(names = {"-z", "--zzz"}, description = "super zzz description") String z; @Parameters(index = "0") String param0; @Parameters(index = "1", description = "super param1 description") String param1; }
@Command(defaultValueProvider = TestDefaultProvider.class, abbreviateSynopsis = true) static class App { @Option(names = "-a", description = "Default: ${DEFAULT-VALUE}") private String optionStringFieldWithoutDefaultNorInitialValue; @Option(names = "-b", description = "Default: ${DEFAULT-VALUE}", defaultValue = "Annotated default value") private String optionStringFieldWithAnnotatedDefault; @Option(names = "-c", description = "Default: ${DEFAULT-VALUE}", showDefaultValue = CommandLine.Help.Visibility.ALWAYS) private String optionStringFieldWithInitDefault = "Initial default value"; @Parameters(arity = "0..1", description = "Default: ${DEFAULT-VALUE}", showDefaultValue = CommandLine.Help.Visibility.ALWAYS) private String paramStringFieldWithoutDefaultNorInitialValue; @Parameters(arity = "0..1", description = "Default: ${DEFAULT-VALUE}", defaultValue = "Annotated default value") private String paramStringFieldWithAnnotatedDefault; @Parameters(arity = "0..1", description = "Default: ${DEFAULT-VALUE}") private String paramStringFieldWithInitDefault = "Initial default value"; private String stringForSetterDefault; @Option(names = "-d", description = "Default: ${DEFAULT-VALUE}", defaultValue = "Annotated setter default value") void setString(String val) { stringForSetterDefault = val; } }
@Command(name = "sub") static class Sub { @Option(names = "-a") private String optionStringFieldWithoutDefaultNorInitialValue; @Option(names = "-b", defaultValue = "Annotated default value") private String optionStringFieldWithAnnotatedDefault; @Option(names = "-c") private String optionStringFieldWithInitDefault = "Initial default value"; @Parameters(arity = "0..1") private String paramStringFieldWithoutDefaultNorInitialValue; @Parameters(arity = "0..1", defaultValue = "Annotated default value") private String paramStringFieldWithAnnotatedDefault; @Parameters(arity = "0..1") private String paramStringFieldWithInitDefault = "Initial default value"; private String stringForSetterDefault; @Option(names = "-d", defaultValue = "Annotated setter default value") void setString(String val) { stringForSetterDefault = val; } }
@Command(name = "i18n-sub2", resourceBundle = "picocli.I18nSubclass2_Messages", description = {"orig sub2 desc 1", "orig sub2 desc 2"}, descriptionHeading = "orig sub2 desc heading%n", headerHeading = "orig sub2 header heading%n") public class I18nSubclass2 extends I18nSuperclass { @Option(names = {"-a", "--aaa"}) String a; @Option(names = {"-b", "--bbb"}, description = {"orig sub2 bbb description 1", "orig sub2 bbb description 2"}) String b; @Option(names = {"-c", "--ccc"}, description = "orig sub2 ccc description") String c; @Parameters(index = "2", description = "sub2", descriptionKey = "SUB2PARAMDESC") String param2; @Parameters(index = "3", description = "orig sub2 param1 description") String param3; }
/** * This is the main container which will be populated by picocli with values * from the arguments. */ private static class Options { @Option(names = { "-n", "--name" }) private String name = "default"; @Parameters(paramLabel = "PROPERTIES", split = ",") private String[] properties = null; public String getName() { return name; } public String[] getProperties() { return properties; } } }
@Command(name = "i18n-sub", description = {"orig sub desc 1", "orig sub desc 2"}, descriptionHeading = "orig sub desc heading:%n", headerHeading = "orig sub header heading%n") public class I18nSubclass extends I18nSuperclass { @Option(names = {"-a", "--aaa"}) String a; @Option(names = {"-b", "--bbb"}, description = {"orig sub bbb description 1", "orig sub bbb description 2"}) String b; @Option(names = {"-c", "--ccc"}, description = "orig sub ccc description") String c; @Parameters(index = "2", description = "sub") String param2; @Parameters(index = "3", description = "orig sub param1 description") String param3; }
@Command(name = "CommandSpecYamlPrinter", mixinStandardHelpOptions = true, description = "Prints details of a CommandSpec") private static class App implements Runnable { @Parameters(arity = "1..*") Class<?>[] classes = new Class[0]; // @Override (requires Java 6) public void run() { for (Class<?> cls : classes) { StringWriter sw = new StringWriter(); new CommandSpecYamlPrinter().print(CommandSpec.forAnnotatedObject(cls), new PrintWriter(sw)); System.out.println(sw); } } } }
@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(name = "country", description = "Resolve ISO country code (ISO-3166-1, Alpha-2 code)") static class Subcommand1 implements Runnable { @Parameters(arity = "1..*", paramLabel = "<country1> <country2>", description = "country code(s) to be resolved") private String[] countryCodes; @Override public void run() { for (String code : countryCodes) { System.out.println(String.format("%s: %s", code.toUpperCase(), new Locale("", code).getDisplayCountry())); } } }
@Command(description = "First level subcommand 2") public static class Sub2 { @Option(names = "--num2", description = "another number") int number2; @Option(names = {"--directory", "-d"}, description = "a directory") File directory; @Parameters(arity = "0..1") Possibilities possibilities; } @Command(description = "Second level sub-subcommand 1")
@Command(name = "sub") static class Sub implements Runnable { @ParentCommand private Top parent; @Parameters private int count; private int result; public void run() { int multiplier = parent == null ? 0 : parent.baseDirectory.toString().length(); result = multiplier * count; parent.result = result; } } @Test
@Command void push(@Option(names = {"-f", "--force"}) boolean force, @Option(names = "--tags") boolean tags, @Parameters(paramLabel = "<repository>") String repo) { // ... implement business logic } }
@Command void commit(@Option(names = {"-m", "--message"}) String commitMessage, @Option(names = "--squash", paramLabel = "<commit>") String squash, @Parameters(paramLabel = "<file>") File[] files) { // ... implement business logic }
@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; }