public void computeUsageLine(StringBuilder buffer, String prefix, CLI cli) { // initialise the string buffer StringBuilder buff; if (prefix == null) { buff = new StringBuilder(getUsagePrefix()); } else { buff = new StringBuilder(getUsagePrefix()).append(prefix); if (!prefix.endsWith(" ")) { buff.append(" "); } } buff.append(cli.getName()).append(" "); if (getOptionComparator() != null) { Collections.sort(cli.getOptions(), getOptionComparator()); } // iterate over the options for (Option option : cli.getOptions()) { appendOption(buff, option); buff.append(" "); } // iterate over the arguments for (Argument arg : cli.getArguments()) { appendArgument(buff, arg, arg.isRequired()); buff.append(" "); } buildWrapped(buffer, buff.toString().indexOf(' ') + 1, buff.toString()); }
/** * Computes the usage of the given {@link CLI}. * * @param builder where the usage is going to be written * @param prefix a prefix to prepend to the usage line. It will be added between 'Usage: ' and the CLI name. * @param cli the cli */ public void usage(StringBuilder builder, String prefix, CLI cli) { computeUsageLine(builder, prefix, cli); if (cli.getSummary() != null && cli.getSummary().trim().length() > 0) { buildWrapped(builder, "\n" + cli.getSummary()); } if (cli.getDescription() != null && cli.getDescription().trim().length() > 0) { buildWrapped(builder, "\n" + cli.getDescription()); } builder.append("\n"); if (cli.getOptions().isEmpty() && cli.getArguments().isEmpty()) { // When we have neither options and arguments, just leave. return; } builder.append("Options and Arguments:\n"); computeOptionsAndArguments(builder, cli.getOptions(), cli.getArguments()); }
for (Argument argument : cli.getArguments()) { if (usedIndexes.contains(argument.getIndex())) { throw new CLIException("Only one argument can use the index " + argument.getIndex()); Iterator<Argument> iterator = cli.getArguments().iterator(); Argument current = null; if (iterator.hasNext()) { for (Argument arg : cli.getArguments()) { if (arg.isRequired() && !commandLine.isArgumentAssigned(arg)) { throw new MissingValueException(arg);
for (Argument argument : cli.getArguments()) { if (argument.getIndex() == -1) { argument.setIndex(current); cli.getArguments().sort((o1, o2) -> { if (o1.getIndex() == o2.getIndex()) { return 1; cli.getArguments().stream().forEach(Argument::ensureValidity);
@Test public void testThatArgumentsAreOrdered() throws CLIException { cli.addArgument(new TypedArgument<String>().setIndex(1).setArgName("1").setType(String.class)); cli.addArgument(new TypedArgument<String>().setIndex(0).setArgName("2").setType(String.class)); cli.addArgument(new TypedArgument<String>().setIndex(2).setArgName("3").setType(String.class)); assertThat(cli.getArguments()).hasSize(3); evaluated = cli.parse(Arrays.asList("a", "b", "c")); Iterator<Argument> iterator = cli.getArguments().iterator(); assertThat(iterator.next().getArgName()).isEqualTo("2"); assertThat(iterator.next().getArgName()).isEqualTo("1"); assertThat(iterator.next().getArgName()).isEqualTo("3"); assertThat((String) evaluated.getArgumentValue("2")).isEqualTo("a"); assertThat((String) evaluated.getArgumentValue("1")).isEqualTo("b"); assertThat((String) evaluated.getArgumentValue("3")).isEqualTo("c"); }
@Test public void testNegativeNumbers() throws CLIException { CLI cli = new DefaultCLI().setName("test"); Option[] options = new Option[]{ new TypedOption<Double>().setLongName("num").setSingleValued(true) .setType(Double.class) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Arrays.asList("--num", "-1.5")); assertThat(evaluated.cli().getOptions()).hasSize(1); assertThat(cli.getArguments()).isEmpty(); assertThat((double) evaluated.getOptionValue("num")).isEqualTo(-1.5d); evaluated = cli.parse(Collections.singletonList("--num=-1.5")); assertThat(evaluated.cli().getOptions()).hasSize(1); assertThat(cli.getArguments()).isEmpty(); assertThat((double) evaluated.getOptionValue("num")).isEqualTo(-1.5d); }
public void computeUsageLine(StringBuilder buffer, String prefix, CLI cli) { // initialise the string buffer StringBuilder buff; if (prefix == null) { buff = new StringBuilder(getUsagePrefix()); } else { buff = new StringBuilder(getUsagePrefix()).append(prefix); if (!prefix.endsWith(" ")) { buff.append(" "); } } buff.append(cli.getName()).append(" "); if (getOptionComparator() != null) { Collections.sort(cli.getOptions(), getOptionComparator()); } // iterate over the options for (Option option : cli.getOptions()) { appendOption(buff, option); buff.append(" "); } // iterate over the arguments for (Argument arg : cli.getArguments()) { appendArgument(buff, arg, arg.isRequired()); buff.append(" "); } buildWrapped(buffer, buff.toString().indexOf(' ') + 1, buff.toString()); }
/** * Computes the usage of the given {@link CLI}. * * @param builder where the usage is going to be written * @param prefix a prefix to prepend to the usage line. It will be added between 'Usage: ' and the CLI name. * @param cli the cli */ public void usage(StringBuilder builder, String prefix, CLI cli) { computeUsageLine(builder, prefix, cli); if (cli.getSummary() != null && cli.getSummary().trim().length() > 0) { buildWrapped(builder, "\n" + cli.getSummary()); } if (cli.getDescription() != null && cli.getDescription().trim().length() > 0) { buildWrapped(builder, "\n" + cli.getDescription()); } builder.append("\n"); if (cli.getOptions().isEmpty() && cli.getArguments().isEmpty()) { // When we have neither options and arguments, just leave. return; } builder.append("Options and Arguments:\n"); computeOptionsAndArguments(builder, cli.getOptions(), cli.getArguments()); }
for (Argument argument : cli.getArguments()) { if (usedIndexes.contains(argument.getIndex())) { throw new CLIException("Only one argument can use the index " + argument.getIndex()); Iterator<Argument> iterator = cli.getArguments().iterator(); Argument current = null; if (iterator.hasNext()) { for (Argument arg : cli.getArguments()) { if (arg.isRequired() && !commandLine.isArgumentAssigned(arg)) { throw new MissingValueException(arg);
for (Argument argument : cli.getArguments()) { if (argument.getIndex() == -1) { argument.setIndex(current); cli.getArguments().sort((o1, o2) -> { if (o1.getIndex() == o2.getIndex()) { return 1; cli.getArguments().stream().forEach(Argument::ensureValidity);
@Test public void testThatArgumentsAreOrdered() throws CLIException { cli.addArgument(new TypedArgument<String>().setIndex(1).setArgName("1").setType(String.class)); cli.addArgument(new TypedArgument<String>().setIndex(0).setArgName("2").setType(String.class)); cli.addArgument(new TypedArgument<String>().setIndex(2).setArgName("3").setType(String.class)); assertThat(cli.getArguments()).hasSize(3); evaluated = cli.parse(Arrays.asList("a", "b", "c")); Iterator<Argument> iterator = cli.getArguments().iterator(); assertThat(iterator.next().getArgName()).isEqualTo("2"); assertThat(iterator.next().getArgName()).isEqualTo("1"); assertThat(iterator.next().getArgName()).isEqualTo("3"); assertThat((String) evaluated.getArgumentValue("2")).isEqualTo("a"); assertThat((String) evaluated.getArgumentValue("1")).isEqualTo("b"); assertThat((String) evaluated.getArgumentValue("3")).isEqualTo("c"); }
/** * Gets the list of defined arguments. * @return the list of argument, empty if none. */ public List<Argument> getArguments() { List<Argument> ret = delegate.getArguments(); return ret; }
/** * Gets the list of defined arguments. * @return the list of argument, empty if none. */ public List<Argument> getArguments() { List<Argument> ret = delegate.getArguments(); return ret; }
@Test public void testNegativeNumbers() throws CLIException { CLI cli = new DefaultCLI().setName("test"); Option[] options = new Option[]{ new TypedOption<Double>().setLongName("num").setSingleValued(true) .setType(Double.class) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Arrays.asList("--num", "-1.5")); assertThat(evaluated.cli().getOptions()).hasSize(1); assertThat(cli.getArguments()).isEmpty(); assertThat((double) evaluated.getOptionValue("num")).isEqualTo(-1.5d); evaluated = cli.parse(Collections.singletonList("--num=-1.5")); assertThat(evaluated.cli().getOptions()).hasSize(1); assertThat(cli.getArguments()).isEmpty(); assertThat((double) evaluated.getOptionValue("num")).isEqualTo(-1.5d); }
public static java.util.List<java.util.Map<String, Object>> getArguments(io.vertx.core.cli.CLI j_receiver) { return j_receiver.getArguments() != null ? j_receiver.getArguments().stream().map(elt -> elt != null ? io.vertx.core.impl.ConversionHelper.fromJsonObject(elt.toJson()) : null).collect(java.util.stream.Collectors.toList()) : null; } public static io.vertx.core.cli.CLI addArgument(io.vertx.core.cli.CLI j_receiver, java.util.Map<String, Object> arg) {