Search in sources :

Example 91 with Option

use of org.apache.commons.cli.Option in project moco by dreamhead.

the class StartArgsParser method envOption.

protected Option envOption() {
    Option opt = new Option("e", true, "environment");
    opt.setType(String.class);
    opt.setRequired(false);
    return opt;
}
Also used : Option(org.apache.commons.cli.Option)

Example 92 with Option

use of org.apache.commons.cli.Option in project moco by dreamhead.

the class StartArgsParser method portOption.

protected Option portOption() {
    Option opt = new Option("p", true, "port");
    opt.setType(Number.class);
    opt.setRequired(false);
    return opt;
}
Also used : Option(org.apache.commons.cli.Option)

Example 93 with Option

use of org.apache.commons.cli.Option in project jmxtrans by jmxtrans.

the class CommonsCliArgumentParser method parseOptions.

/** Parse the options given on the command line. */
@Nonnull
@Override
public JmxTransConfiguration parseOptions(@Nonnull String[] args) throws OptionsException, org.apache.commons.cli.ParseException {
    CommandLineParser parser = new GnuParser();
    CommandLine cl = parser.parse(getOptions(), args);
    Option[] options = cl.getOptions();
    JmxTransConfiguration configuration = new JmxTransConfiguration();
    for (Option option : options) {
        if (option.getOpt().equals("c")) {
            configuration.setContinueOnJsonError(Boolean.parseBoolean(option.getValue()));
        } else if (option.getOpt().equals("j")) {
            File jsonDir = new File(option.getValue());
            if (jsonDir.exists() && jsonDir.isDirectory()) {
                configuration.setJsonDir(jsonDir);
            } else {
                throw new OptionsException("Path to json directory is invalid: " + jsonDir);
            }
        } else if (option.getOpt().equals("f")) {
            File jsonFile = new File(option.getValue());
            if (jsonFile.exists() && jsonFile.isFile()) {
                configuration.setJsonFile(jsonFile);
            } else {
                throw new OptionsException("Path to json file is invalid: " + jsonFile);
            }
        } else if (option.getOpt().equals("e")) {
            configuration.setRunEndlessly(true);
        } else if (option.getOpt().equals("q")) {
            File quartzConfigFile = new File(option.getValue());
            if (quartzConfigFile.exists() && quartzConfigFile.isFile()) {
                configuration.setQuartzPropertiesFile(quartzConfigFile);
            } else {
                throw new OptionsException("Could not find path to the quartz properties file: " + quartzConfigFile.getAbsolutePath());
            }
        } else if (option.getOpt().equals("s")) {
            try {
                configuration.setRunPeriod(Integer.parseInt(option.getValue()));
            } catch (NumberFormatException nfe) {
                throw new OptionsException("Seconds between server job runs must be an integer");
            }
        } else if (option.getOpt().equals("a")) {
            configuration.setAdditionalJars(option.getValuesList());
        } else if (option.getOpt().equals("h")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("java -jar jmxtrans-all.jar", getOptions());
            configuration.setHelp(true);
        }
    }
    if ((!configuration.isHelp()) && (configuration.getJsonDirOrFile() == null)) {
        throw new OptionsException("Please specify either the -f or -j option.");
    }
    return configuration;
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) File(java.io.File) Nonnull(javax.annotation.Nonnull)

Example 94 with Option

use of org.apache.commons.cli.Option in project languagetool by languagetool-org.

the class BuilderOptions method addOption.

public void addOption(String opt, boolean hasArg, String description, boolean isRequired) {
    Option option = new Option(opt, hasArg, description);
    option.setRequired(isRequired);
    options.addOption(option);
}
Also used : Option(org.apache.commons.cli.Option)

Example 95 with Option

use of org.apache.commons.cli.Option in project databus by linkedin.

the class BootstrapDropSource method parseArgs.

@SuppressWarnings("static-access")
public static int parseArgs(String[] args) throws IOException {
    CommandLineParser cliParser = new GnuParser();
    Option helpOption = OptionBuilder.withLongOpt(HELP_OPT_LONG_NAME).withDescription("Help screen").create(HELP_OPT_CHAR);
    Option sourceIdOption = OptionBuilder.withLongOpt(SOURCE_ID_OPT_LONG_NAME).withDescription("Source ID for which tables need to be dropped").hasArg().withArgName("Source ID").create(SOURCE_ID_OPT_CHAR);
    Option dbOption = OptionBuilder.withLongOpt(BOOTSTRAP_DB_PROPS_OPT_LONG_NAME).withDescription("Bootstrap producer properties to use").hasArg().withArgName("property_file").create(BOOTSTRAP_DB_PROP_OPT_CHAR);
    Option cmdLinePropsOption = OptionBuilder.withLongOpt(CMD_LINE_PROPS_OPT_LONG_NAME).withDescription("Cmd line override of config properties. Semicolon separated.").hasArg().withArgName("Semicolon_separated_properties").create(CMD_LINE_PROPS_OPT_CHAR);
    Option log4jPropsOption = OptionBuilder.withLongOpt(LOG4J_PROPS_OPT_LONG_NAME).withDescription("Log4j properties to use").hasArg().withArgName("property_file").create(LOG4J_PROPS_OPT_CHAR);
    Options options = new Options();
    options.addOption(helpOption);
    options.addOption(sourceIdOption);
    options.addOption(dbOption);
    options.addOption(cmdLinePropsOption);
    options.addOption(log4jPropsOption);
    CommandLine cmd = null;
    try {
        cmd = cliParser.parse(options, args);
    } catch (ParseException pe) {
        LOG.error("Bootstrap Physical Config: failed to parse command-line options.", pe);
        throw new RuntimeException("Bootstrap Physical Config: failed to parse command-line options.", pe);
    }
    if (cmd.hasOption(LOG4J_PROPS_OPT_CHAR)) {
        String log4jPropFile = cmd.getOptionValue(LOG4J_PROPS_OPT_CHAR);
        PropertyConfigurator.configure(log4jPropFile);
        LOG.info("Using custom logging settings from file " + log4jPropFile);
    } else {
        PatternLayout defaultLayout = new PatternLayout("%d{ISO8601} +%r [%t] (%p) {%c} %m%n");
        ConsoleAppender defaultAppender = new ConsoleAppender(defaultLayout);
        Logger.getRootLogger().removeAllAppenders();
        Logger.getRootLogger().addAppender(defaultAppender);
        LOG.info("Using default logging settings");
    }
    if (cmd.hasOption(HELP_OPT_CHAR)) {
        printCliHelp(options);
        System.exit(0);
    }
    if (!cmd.hasOption(SOURCE_ID_OPT_CHAR))
        throw new RuntimeException("Source ID is not provided");
    if (!cmd.hasOption(BOOTSTRAP_DB_PROP_OPT_CHAR))
        throw new RuntimeException("Bootstrap config is not provided");
    String propFile = cmd.getOptionValue(BOOTSTRAP_DB_PROP_OPT_CHAR);
    LOG.info("Loading bootstrap DB config from properties file " + propFile);
    _sBootstrapConfigProps = new Properties();
    FileInputStream f = new FileInputStream(propFile);
    try {
        _sBootstrapConfigProps.load(f);
    } finally {
        f.close();
    }
    if (cmd.hasOption(CMD_LINE_PROPS_OPT_CHAR)) {
        String cmdLinePropString = cmd.getOptionValue(CMD_LINE_PROPS_OPT_CHAR);
        updatePropsFromCmdLine(_sBootstrapConfigProps, cmdLinePropString);
    }
    return Integer.parseInt(cmd.getOptionValue(SOURCE_ID_OPT_CHAR));
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PatternLayout(org.apache.log4j.PatternLayout) GnuParser(org.apache.commons.cli.GnuParser) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Aggregations

Option (org.apache.commons.cli.Option)152 Options (org.apache.commons.cli.Options)105 CommandLine (org.apache.commons.cli.CommandLine)53 CommandLineParser (org.apache.commons.cli.CommandLineParser)52 ParseException (org.apache.commons.cli.ParseException)41 GnuParser (org.apache.commons.cli.GnuParser)39 HelpFormatter (org.apache.commons.cli.HelpFormatter)30 File (java.io.File)13 OptionGroup (org.apache.commons.cli.OptionGroup)13 FileInputStream (java.io.FileInputStream)10 IOException (java.io.IOException)10 HashMap (java.util.HashMap)9 DefaultParser (org.apache.commons.cli.DefaultParser)9 Properties (java.util.Properties)8 BasicParser (org.apache.commons.cli.BasicParser)6 ConsoleAppender (org.apache.log4j.ConsoleAppender)6 PatternLayout (org.apache.log4j.PatternLayout)6 ArrayList (java.util.ArrayList)5 PosixParser (org.apache.commons.cli.PosixParser)5 List (java.util.List)3