@Override public boolean isFlagEnabled(String name) { Option option = cli.getOption(name); if (option == null) { throw new IllegalArgumentException("Cannot find the option '" + name + "'"); } if (option.isFlag()) { return optionsSeenInCommandLine.contains(option); } else { throw new IllegalStateException("Cannot retrieve the flag value on a non-flag option (" + name + ")"); } }
@Test public void testWithDashD() throws CLIException { CLI cli = new DefaultCLI().setName("test"); Option[] options = new Option[]{ new TypedOption<String>().setShortName("D").setLongName("systemProperty") .setMultiValued(true).setType(String.class), new TypedOption<Boolean>().setShortName("F").setLongName("flag") .setFlag(true) .setType(Boolean.class) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Arrays.asList("-Dx=y", "-F")); assertThat(evaluated.cli().getOptions()).hasSize(2); assertThat(evaluated.getRawValueForOption(evaluated.cli().getOption("systemProperty"))) .isEqualTo("x=y"); assertThat((boolean) evaluated.getOptionValue("flag")).isTrue(); }
@Override @SuppressWarnings("unchecked") public <T> T getOptionValue(String name) { Option option = cli.getOption(name); if (option == null) { return null; } if (option instanceof TypedOption) { return getValue((TypedOption<T>) option); } else { return (T) getRawValueForOption(option); } }
@Test public void testWithDashD() throws CLIException { CLI cli = new DefaultCLI().setName("test"); Option[] options = new Option[]{ new TypedOption<String>().setShortName("D").setLongName("systemProperty") .setMultiValued(true).setType(String.class), new TypedOption<Boolean>().setShortName("F").setLongName("flag") .setFlag(true) .setType(Boolean.class) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Arrays.asList("-Dx=y", "-F")); assertThat(evaluated.cli().getOptions()).hasSize(2); assertThat(evaluated.getRawValueForOption(evaluated.cli().getOption("systemProperty"))) .isEqualTo("x=y"); assertThat((boolean) evaluated.getOptionValue("flag")).isTrue(); }
@Override @SuppressWarnings("unchecked") public <T> List<T> getOptionValues(String name) { Option option = cli.getOption(name); if (option == null) { return null; } if (option instanceof TypedOption) { TypedOption<T> typed = (TypedOption<T>) option; if (typed.isParsedAsList()) { return createFromList(getRawValueForOption(option), typed); } else { return getRawValuesForOption(option).stream().map(s -> create(s, typed)) .collect(Collectors.toList()); } } else { return (List<T>) getRawValuesForOption(option); } }
/** * Validates the command line parameters. * * @param context - the execution context * @throws CLIException - validation failed */ @Override public void setUp(ExecutionContext context) throws CLIException { super.setUp(context); CommandLine commandLine = executionContext.commandLine(); if (!isClustered() && ( commandLine.isOptionAssigned(executionContext.cli().getOption("cluster-host")) || commandLine.isOptionAssigned(executionContext.cli().getOption("cluster-port")) || commandLine.isOptionAssigned(executionContext.cli().getOption("cluster-public-host")) || commandLine.isOptionAssigned(executionContext.cli().getOption("cluster-public-port")) )) { throw new CLIException("The -cluster-xxx options require -cluster to be enabled"); } // If quorum and / or ha-group, ha need to have been explicitly set io.vertx.core.cli.Option haGroupOption = executionContext.cli().getOption("hagroup"); io.vertx.core.cli.Option quorumOption = executionContext.cli().getOption("quorum"); if (!ha && (commandLine.isOptionAssigned(haGroupOption) || commandLine.isOptionAssigned(quorumOption))) { throw new CLIException("The option -hagroup and -quorum requires -ha to be enabled"); } }
private static Object getOptionValue(Method method, String name, CommandLine commandLine) { final io.vertx.core.cli.Option option = commandLine.cli().getOption(name); if (option == null) { return null; } boolean multiple = ReflectionUtils.isMultiple(method); if (multiple) { return createMultiValueContainer(method, commandLine.getOptionValues(name)); } return commandLine.getOptionValue(name); }
@Override public boolean isFlagEnabled(String name) { Option option = cli.getOption(name); if (option == null) { throw new IllegalArgumentException("Cannot find the option '" + name + "'"); } if (option.isFlag()) { return optionsSeenInCommandLine.contains(option); } else { throw new IllegalStateException("Cannot retrieve the flag value on a non-flag option (" + name + ")"); } }
@Override @SuppressWarnings("unchecked") public <T> T getOptionValue(String name) { Option option = cli.getOption(name); if (option == null) { return null; } if (option instanceof TypedOption) { return getValue((TypedOption<T>) option); } else { return (T) getRawValueForOption(option); } }
@Override @SuppressWarnings("unchecked") public <T> List<T> getOptionValues(String name) { Option option = cli.getOption(name); if (option == null) { return null; } if (option instanceof TypedOption) { TypedOption<T> typed = (TypedOption<T>) option; if (typed.isParsedAsList()) { return createFromList(getRawValueForOption(option), typed); } else { return getRawValuesForOption(option).stream().map(s -> create(s, typed)) .collect(Collectors.toList()); } } else { return (List<T>) getRawValuesForOption(option); } }
/** * Validates the command line parameters. * * @param context - the execution context * @throws CLIException - validation failed */ @Override public void setUp(ExecutionContext context) throws CLIException { super.setUp(context); CommandLine commandLine = executionContext.commandLine(); if (!isClustered() && ( commandLine.isOptionAssigned(executionContext.cli().getOption("cluster-host")) || commandLine.isOptionAssigned(executionContext.cli().getOption("cluster-port")) || commandLine.isOptionAssigned(executionContext.cli().getOption("cluster-public-host")) || commandLine.isOptionAssigned(executionContext.cli().getOption("cluster-public-port")) )) { throw new CLIException("The -cluster-xxx options require -cluster to be enabled"); } // If quorum and / or ha-group, ha need to have been explicitly set io.vertx.core.cli.Option haGroupOption = executionContext.cli().getOption("hagroup"); io.vertx.core.cli.Option quorumOption = executionContext.cli().getOption("quorum"); if (!ha && (commandLine.isOptionAssigned(haGroupOption) || commandLine.isOptionAssigned(quorumOption))) { throw new CLIException("The option -hagroup and -quorum requires -ha to be enabled"); } }
private static Object getOptionValue(Method method, String name, CommandLine commandLine) { final io.vertx.core.cli.Option option = commandLine.cli().getOption(name); if (option == null) { return null; } boolean multiple = ReflectionUtils.isMultiple(method); if (multiple) { return createMultiValueContainer(method, commandLine.getOptionValues(name)); } return commandLine.getOptionValue(name); }
/** * Gets an {@link io.vertx.core.cli.Option} based on its name (short name, long name or argument name). * @param name the name, must not be <code>null</code> * @return the {@link io.vertx.core.cli.Option}, <code>null</code> if not found */ public Option getOption(String name) { Option ret = delegate.getOption(name); return ret; }
/** * Gets an {@link io.vertx.core.cli.Option} based on its name (short name, long name or argument name). * @param name the name, must not be <code>null</code> * @return the {@link io.vertx.core.cli.Option}, <code>null</code> if not found */ public Option getOption(String name) { Option ret = delegate.getOption(name); return ret; }
public static java.util.Map<String, Object> getOption(io.vertx.core.cli.CLI j_receiver, java.lang.String name) { return j_receiver.getOption(name) != null ? io.vertx.core.impl.ConversionHelper.fromJsonObject(j_receiver.getOption(name).toJson()) : null; } public static java.util.Map<String, Object> getArgument(io.vertx.core.cli.CLI j_receiver, java.lang.String name) {