Search in sources :

Example 36 with OptionException

use of joptsimple.OptionException in project JavaForFun by gumartinm.

the class OffsetManagement method parseArguments.

private void parseArguments(final String[] args) throws IOException {
    final OptionParser optionParser = new OptionParser();
    groupIdOption = optionParser.accepts("group-id", "Consumers group ID").withRequiredArg().ofType(String.class).describedAs("consumers group 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").required();
    zookeeperOption = optionParser.accepts("zookeeper", "Format: HOST:POST").withRequiredArg().ofType(String.class).defaultsTo("localhost:2181").describedAs("url").required();
    inputTopicOption = optionParser.accepts("input-topic", "Topic name").withRequiredArg().ofType(String.class).withValuesSeparatedBy(',').required().describedAs("topic name");
    offsetOption = optionParser.accepts("offset", "The new offset value, default to -2 which means from beginning; while value -1 means from end").withRequiredArg().describedAs("new offset").ofType(Long.class).defaultsTo(OffsetRequest.EarliestTime());
    partitionOption = optionParser.accepts("partition", "The partition number. All partitions by default.").withRequiredArg().describedAs("partition number").ofType(Integer.class).defaultsTo(Integer.MIN_VALUE);
    try {
        options = optionParser.parse(args);
    } catch (final OptionException ex) {
        optionParser.printHelpOn(System.err);
        throw ex;
    }
}
Also used : OptionException(joptsimple.OptionException) OptionParser(joptsimple.OptionParser)

Example 37 with OptionException

use of joptsimple.OptionException 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 38 with OptionException

use of joptsimple.OptionException in project wildfly-swarm by wildfly-swarm.

the class Main method generateSwarmJar.

protected static File generateSwarmJar(final String[] args) throws Exception {
    OptionSet foundOptions = null;
    try {
        foundOptions = OPT_PARSER.parse(args);
    } catch (OptionException e) {
        exit(e.getMessage(), true);
    }
    if (foundOptions.has(HELP_OPT)) {
        exit(null, 0, true);
    }
    if (foundOptions.has(VERSION_OPT)) {
        exit("swarmtool v" + VERSION, 0);
    }
    final List<File> nonOptArgs = foundOptions.valuesOf(SOURCE_OPT);
    if (nonOptArgs.isEmpty()) {
        exit("No source artifact specified.", true);
    }
    if (nonOptArgs.size() > 1) {
        exit("Too many source artifacts provided (" + nonOptArgs + ")", true);
    }
    final File source = nonOptArgs.get(0);
    if (!source.exists()) {
        exit("File " + source.getAbsolutePath() + " does not exist.");
    }
    final Properties properties = new Properties();
    if (foundOptions.has(SYSPROPS_FILE_OPT)) {
        try (InputStream in = new FileInputStream(foundOptions.valueOf(SYSPROPS_FILE_OPT))) {
            properties.load(in);
        }
    }
    foundOptions.valuesOf(SYSPROPS_OPT).forEach(prop -> {
        final String[] parts = prop.split("=");
        properties.put(parts[0], parts[1]);
    });
    final DeclaredDependencies dependencies = new DeclaredDependencies();
    foundOptions.valuesOf(DEPENDENCIES_OPT).stream().map(DeclaredDependencies::createSpec).forEach(dependencies::add);
    final String[] parts = source.getName().split("\\.(?=[^\\.]+$)");
    final String baseName = parts[0];
    final String type = parts[1] == null ? "jar" : parts[1];
    final String jarName = foundOptions.has(NAME_OPT) ? foundOptions.valueOf(NAME_OPT) : baseName;
    final String outDir = new File(foundOptions.valueOf(OUTPUT_DIR_OPT)).getCanonicalPath();
    final String suffix = foundOptions.has(HOLLOW_OPT) ? "-hollow-swarm" : "-swarm";
    final BuildTool tool = new BuildTool(getResolvingHelper(foundOptions.valuesOf(REPOS_OPT))).projectArtifact("", baseName, "", type, source).declaredDependencies(dependencies).fractionDetectionMode(foundOptions.has(DISABLE_AUTO_DETECT_OPT) ? BuildTool.FractionDetectionMode.never : BuildTool.FractionDetectionMode.force).bundleDependencies(!foundOptions.has(DISABLE_BUNDLE_DEPS_OPT)).executable(foundOptions.has(EXECUTABLE_OPT)).properties(properties).hollow(foundOptions.has(HOLLOW_OPT));
    if (foundOptions.has(MAIN_OPT)) {
        tool.mainClass(foundOptions.valueOf(MAIN_OPT));
    }
    if (foundOptions.has(MODULES_OPT)) {
        tool.additionalModules(foundOptions.valuesOf(MODULES_OPT));
    }
    if (foundOptions.has(DEBUG_LOGGING)) {
        tool.logger(BuildTool.STD_LOGGER_WITH_DEBUG);
    }
    addSwarmFractions(tool, foundOptions.valuesOf(FRACTIONS_OPT));
    System.err.println(String.format("Building %s/%s%s.jar", outDir, jarName, suffix));
    return tool.build(jarName, Paths.get(outDir));
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DeclaredDependencies(org.wildfly.swarm.tools.DeclaredDependencies) BuildTool(org.wildfly.swarm.tools.BuildTool) OptionException(joptsimple.OptionException) OptionSet(joptsimple.OptionSet) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 39 with OptionException

use of joptsimple.OptionException 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 40 with OptionException

use of joptsimple.OptionException in project crate by crate.

the class Command method main.

/**
 * Parses options for this command from args and executes it.
 */
public final int main(String[] args, Terminal terminal) throws Exception {
    if (addShutdownHook()) {
        shutdownHookThread = new Thread(() -> {
            try {
                this.close();
            } catch (final IOException e) {
                try (StringWriter sw = new StringWriter();
                    PrintWriter pw = new PrintWriter(sw)) {
                    e.printStackTrace(pw);
                    terminal.println(sw.toString());
                } catch (final IOException impossible) {
                    // say that an exception here is impossible
                    throw new AssertionError(impossible);
                }
            }
        });
        Runtime.getRuntime().addShutdownHook(shutdownHookThread);
    }
    beforeMain.run();
    try {
        mainWithoutErrorHandling(args, terminal);
    } catch (OptionException e) {
        printHelp(terminal);
        terminal.println(Terminal.Verbosity.SILENT, "ERROR: " + e.getMessage());
        return ExitCodes.USAGE;
    } catch (UserException e) {
        if (e.exitCode == ExitCodes.USAGE) {
            printHelp(terminal);
        }
        terminal.println(Terminal.Verbosity.SILENT, "ERROR: " + e.getMessage());
        return e.exitCode;
    }
    return ExitCodes.OK;
}
Also used : StringWriter(java.io.StringWriter) OptionException(joptsimple.OptionException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

OptionException (joptsimple.OptionException)41 OptionSet (joptsimple.OptionSet)32 OptionParser (joptsimple.OptionParser)26 File (java.io.File)7 IOException (java.io.IOException)7 List (java.util.List)5 PrintWriter (java.io.PrintWriter)4 StringWriter (java.io.StringWriter)4 ArrayList (java.util.ArrayList)4 BuiltinHelpFormatter (joptsimple.BuiltinHelpFormatter)4 OptionSpec (joptsimple.OptionSpec)4 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)3 ThreadFactory (java.util.concurrent.ThreadFactory)3 BisqEnvironment (io.bisq.core.app.BisqEnvironment)2 BitsquareEnvironment (io.bitsquare.app.BitsquareEnvironment)2 FileInputStream (java.io.FileInputStream)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Properties (java.util.Properties)2 Logger (org.slf4j.Logger)2