Search in sources :

Example 21 with OptionSpec

use of joptsimple.OptionSpec in project hazelcast-simulator by hazelcast.

the class CliUtils method initOptionsOnlyWithHelp.

public static OptionSet initOptionsOnlyWithHelp(OptionParser parser, String content, String[] args) {
    try {
        OptionSpec helpSpec = parser.accepts("help", "Shows the help.").forHelp();
        OptionSet options = parser.parse(args);
        if (options.has(helpSpec)) {
            printHelp(content);
            printHelpAndExit(parser, System.out);
        }
        if (options.nonOptionArguments().size() > 0) {
            printHelp(content);
            printHelpAndExit(parser, System.out);
        }
        return options;
    } catch (OptionException e) {
        throw new CommandLineExitException(e.getMessage() + ". Use --help to get overview of the help options.");
    }
}
Also used : OptionSpec(joptsimple.OptionSpec) OptionException(joptsimple.OptionException) OptionSet(joptsimple.OptionSet)

Example 22 with OptionSpec

use of joptsimple.OptionSpec in project apache-kafka-on-k8s by banzaicloud.

the class StreamsResetter method parseArguments.

private void parseArguments(final String[] args) throws IOException {
    final OptionParser optionParser = new OptionParser(false);
    applicationIdOption = optionParser.accepts("application-id", "The Kafka Streams application ID (application.id).").withRequiredArg().ofType(String.class).describedAs("id").required();
    bootstrapServerOption = optionParser.accepts("bootstrap-servers", "Comma-separated list of broker urls with format: HOST1:PORT1,HOST2:PORT2").withRequiredArg().ofType(String.class).defaultsTo("localhost:9092").describedAs("urls");
    inputTopicsOption = optionParser.accepts("input-topics", "Comma-separated list of user input topics. For these topics, the tool will reset the offset to the earliest available offset.").withRequiredArg().ofType(String.class).withValuesSeparatedBy(',').describedAs("list");
    intermediateTopicsOption = optionParser.accepts("intermediate-topics", "Comma-separated list of intermediate user topics (topics used in the through() method). For these topics, the tool will skip to the end.").withRequiredArg().ofType(String.class).withValuesSeparatedBy(',').describedAs("list");
    toOffsetOption = optionParser.accepts("to-offset", "Reset offsets to a specific offset.").withRequiredArg().ofType(Long.class);
    toDatetimeOption = optionParser.accepts("to-datetime", "Reset offsets to offset from datetime. Format: 'YYYY-MM-DDTHH:mm:SS.sss'").withRequiredArg().ofType(String.class);
    byDurationOption = optionParser.accepts("by-duration", "Reset offsets to offset by duration from current timestamp. Format: 'PnDTnHnMnS'").withRequiredArg().ofType(String.class);
    toEarliestOption = optionParser.accepts("to-earliest", "Reset offsets to earliest offset.");
    toLatestOption = optionParser.accepts("to-latest", "Reset offsets to latest offset.");
    fromFileOption = optionParser.accepts("from-file", "Reset offsets to values defined in CSV file.").withRequiredArg().ofType(String.class);
    shiftByOption = optionParser.accepts("shift-by", "Reset offsets shifting current offset by 'n', where 'n' can be positive or negative").withRequiredArg().describedAs("number-of-offsets").ofType(Long.class);
    commandConfigOption = optionParser.accepts("config-file", "Property file containing configs to be passed to admin clients and embedded consumer.").withRequiredArg().ofType(String.class).describedAs("file name");
    executeOption = optionParser.accepts("execute", "Execute the command.");
    dryRunOption = optionParser.accepts("dry-run", "Display the actions that would be performed without executing the reset commands.");
    // TODO: deprecated in 1.0; can be removed eventually
    optionParser.accepts("zookeeper", "Zookeeper option is deprecated by bootstrap.servers, as the reset tool would no longer access Zookeeper directly.");
    try {
        options = optionParser.parse(args);
    } catch (final OptionException e) {
        printHelp(optionParser);
        throw e;
    }
    if (options.has(executeOption) && options.has(dryRunOption)) {
        CommandLineUtils.printUsageAndDie(optionParser, "Only one of --dry-run and --execute can be specified");
    }
    scala.collection.immutable.HashSet<OptionSpec<?>> allScenarioOptions = new scala.collection.immutable.HashSet<>();
    allScenarioOptions.$plus(toOffsetOption);
    allScenarioOptions.$plus(toDatetimeOption);
    allScenarioOptions.$plus(byDurationOption);
    allScenarioOptions.$plus(toEarliestOption);
    allScenarioOptions.$plus(toLatestOption);
    allScenarioOptions.$plus(fromFileOption);
    allScenarioOptions.$plus(shiftByOption);
    CommandLineUtils.checkInvalidArgs(optionParser, options, toOffsetOption, allScenarioOptions.$minus(toOffsetOption));
    CommandLineUtils.checkInvalidArgs(optionParser, options, toDatetimeOption, allScenarioOptions.$minus(toDatetimeOption));
    CommandLineUtils.checkInvalidArgs(optionParser, options, byDurationOption, allScenarioOptions.$minus(byDurationOption));
    CommandLineUtils.checkInvalidArgs(optionParser, options, toEarliestOption, allScenarioOptions.$minus(toEarliestOption));
    CommandLineUtils.checkInvalidArgs(optionParser, options, toLatestOption, allScenarioOptions.$minus(toLatestOption));
    CommandLineUtils.checkInvalidArgs(optionParser, options, fromFileOption, allScenarioOptions.$minus(fromFileOption));
    CommandLineUtils.checkInvalidArgs(optionParser, options, shiftByOption, allScenarioOptions.$minus(shiftByOption));
}
Also used : OptionSpec(joptsimple.OptionSpec) OptionException(joptsimple.OptionException) OptionParser(joptsimple.OptionParser) HashSet(java.util.HashSet)

Example 23 with OptionSpec

use of joptsimple.OptionSpec in project LanternServer by LanternPowered.

the class OptionObjectProvider method get0.

@Nullable
protected Object get0() {
    final Option option = this.injectionPoint.getAnnotation(Option.class);
    if (option == null) {
        throw new IllegalStateException("Missing @Option annotation.");
    }
    final Flag flag = this.injectionPoint.getAnnotation(Flag.class);
    if (flag != null && !this.type.isSupertypeOf(Boolean.class)) {
        throw new IllegalStateException("The @Flag annotation can only be used for boolean options.");
    }
    final List<String> arguments = Arrays.asList(option.value());
    final OptionSpecBuilder builder = this.optionParser.acceptsAll(arguments, option.description());
    final OptionSpec optionSpec;
    if (flag != null) {
        optionSpec = builder;
    } else {
        optionSpec = builder.withRequiredArg().withValuesConvertedBy(this.typeConverter());
    }
    final OptionSet optionSet = this.optionParser.parse(this.arguments);
    if (flag != null) {
        return optionSet.has(optionSpec);
    } else {
        final Object object = optionSet.valueOf(optionSpec);
        if (this.injectionPoint.getType().getRawType() == boolean.class && object == null) {
            return false;
        } else {
            return object;
        }
    }
}
Also used : OptionSpec(joptsimple.OptionSpec) OptionSpecBuilder(joptsimple.OptionSpecBuilder) Option(org.lanternpowered.server.inject.Option) OptionSet(joptsimple.OptionSet) Flag(org.lanternpowered.server.inject.Flag) Nullable(javax.annotation.Nullable)

Example 24 with OptionSpec

use of joptsimple.OptionSpec in project whirr by apache.

the class AbstractClusterCommand method getClusterSpec.

/**
 * Load the cluster spec by parsing the command line option set
 */
protected ClusterSpec getClusterSpec(OptionSet optionSet) throws ConfigurationException {
    Configuration optionsConfig = new PropertiesConfiguration();
    for (Map.Entry<Property, OptionSpec<?>> entry : optionSpecs.entrySet()) {
        Property property = entry.getKey();
        OptionSpec<?> option = entry.getValue();
        Object value;
        if (property.hasMultipleArguments()) {
            value = optionSet.valuesOf(option);
        } else {
            value = optionSet.valueOf(option);
        }
        if (value == null && property.getType().equals(Boolean.class) && optionSet.has(property.getSimpleName())) {
            value = Boolean.TRUE.toString();
        }
        if (value != null) {
            optionsConfig.setProperty(property.getConfigName(), value);
        }
    }
    CompositeConfiguration config = new CompositeConfiguration();
    config.addConfiguration(optionsConfig);
    if (optionSet.has(configOption)) {
        Configuration defaults = new PropertiesConfiguration(optionSet.valueOf(configOption));
        config.addConfiguration(defaults);
    }
    ClusterSpec clusterSpec = new ClusterSpec(config);
    for (Property required : EnumSet.of(CLUSTER_NAME, PROVIDER, IDENTITY, CREDENTIAL, INSTANCE_TEMPLATES, PRIVATE_KEY_FILE)) {
        if (clusterSpec.getConfiguration().getString(required.getConfigName()) == null) {
            throw new IllegalArgumentException(String.format("Option '%s' not set.", required.getSimpleName()));
        }
    }
    return clusterSpec;
}
Also used : OptionSpec(joptsimple.OptionSpec) ArgumentAcceptingOptionSpec(joptsimple.ArgumentAcceptingOptionSpec) Configuration(org.apache.commons.configuration.Configuration) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) ClusterSpec(org.apache.whirr.ClusterSpec) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) Map(java.util.Map) Property(org.apache.whirr.ClusterSpec.Property)

Aggregations

OptionSpec (joptsimple.OptionSpec)24 OptionSet (joptsimple.OptionSet)15 ArgumentAcceptingOptionSpec (joptsimple.ArgumentAcceptingOptionSpec)14 OptionParser (joptsimple.OptionParser)14 ArrayList (java.util.ArrayList)10 Properties (java.util.Properties)8 ClusterMapConfig (com.github.ambry.config.ClusterMapConfig)7 VerifiableProperties (com.github.ambry.config.VerifiableProperties)7 ClusterAgentsFactory (com.github.ambry.clustermap.ClusterAgentsFactory)6 ClusterMap (com.github.ambry.clustermap.ClusterMap)6 File (java.io.File)5 MetricRegistry (com.codahale.metrics.MetricRegistry)4 BlobProperties (com.github.ambry.messageformat.BlobProperties)4 Throttler (com.github.ambry.utils.Throttler)4 FileWriter (java.io.FileWriter)4 HashSet (java.util.HashSet)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 OptionException (joptsimple.OptionException)4 IOException (java.io.IOException)3