Example usage for org.apache.commons.cli DefaultParser DefaultParser

List of usage examples for org.apache.commons.cli DefaultParser DefaultParser

Introduction

In this page you can find the example usage for org.apache.commons.cli DefaultParser DefaultParser.

Prototype

DefaultParser

Source Link

Usage

From source file:de.topobyte.utilities.apache.commons.cli.TestMulti.java

License:asdf

@Test
public void testInteger() throws ParseException, ArgumentParseException {
    Options options = new Options();
    OptionHelper.addS(options, "foo", true, true, "an option");
    OptionHelper.addS(options, "bar", true, true, "another option");

    String[] arguments = new String[] { "-foo", "10", "-bar", "20", "-bar", "30" };
    CommandLine line = new DefaultParser().parse(options, arguments);

    IntegerOption foo = ArgumentHelper.getInteger(line, "foo");
    IntegerOption bar = ArgumentHelper.getInteger(line, "bar");

    assertTrue(foo.hasValue());/*from  w w w  .  j a v a 2s.c  o m*/
    assertTrue(bar.hasValue());
    assertEquals(10, foo.getValue());
    assertEquals(20, bar.getValue());

    List<IntegerOption> bars = ArgumentHelper.getIntegers(line, "bar");
    assertEquals(2, bars.size());
    assertEquals(20, bars.get(0).getValue());
    assertEquals(30, bars.get(1).getValue());
}

From source file:io.github.collaboratory.LauncherCWL.java

/**
 * Constructor for shell-based launch/*w  ww .  j av  a 2  s  .  c o m*/
 * @param args raw arguments from the command-line
 */
public LauncherCWL(String[] args) {
    // create the command line parser
    CommandLineParser parser = new DefaultParser();
    // parse command line
    CommandLine line = parseCommandLine(parser, args);
    configFilePath = line.getOptionValue("config");
    imageDescriptorPath = line.getOptionValue("descriptor");
    runtimeDescriptorPath = line.getOptionValue("job");

    gson = CWL.getTypeSafeCWLToolDocument();
    fileProvisioning = new FileProvisioning(configFilePath);
}

From source file:com.ericsson.eiffel.remrem.publish.cli.CliOptions.java

/**
 * Parse the given arguments and act on them
 * @param args command line arguments/*from ww  w  .j  a v  a  2s  .  c  om*/
 * @return if the service should start or not
 */
public static void parse(String[] args) {
    createCLIOptions();
    CommandLineParser parser = new DefaultParser();
    try {
        commandLine = parser.parse(options, args);
        afterParseChecks();
        handleMessageBusOptions();
        handleDebugOptions();
    } catch (Exception e) {
        System.out.println(e.getMessage());
        help(CLIExitCodes.CLI_MISSING_OPTION_EXCEPTION);
    }
}

From source file:act.installer.GenbankInstaller.java

public static void main(String[] args) throws Exception {
    Options opts = new Options();
    for (Option.Builder b : OPTION_BUILDERS) {
        opts.addOption(b.build());/*w w  w  .  j  av a 2 s.  co  m*/
    }

    CommandLine cl = null;
    try {
        CommandLineParser parser = new DefaultParser();
        cl = parser.parse(opts, args);
    } catch (ParseException e) {
        LOGGER.error("Argument parsing failed: %s", e.getMessage());
        HELP_FORMATTER.printHelp(GenbankInstaller.class.getCanonicalName(), HELP_MESSAGE, opts, null, true);
        System.exit(1);
    }

    if (cl.hasOption("help")) {
        HELP_FORMATTER.printHelp(GenbankInstaller.class.getCanonicalName(), HELP_MESSAGE, opts, null, true);
        System.exit(1);
    }

    File genbankFile = new File(cl.getOptionValue(OPTION_GENBANK_PATH));
    String dbName = cl.getOptionValue(OPTION_DB_NAME);
    String seqType = cl.getOptionValue(OPTION_SEQ_TYPE);

    if (!genbankFile.exists()) {
        String msg = String.format("Genbank file path is null");
        LOGGER.error(msg);
        throw new RuntimeException(msg);
    } else {
        MongoDB db = new MongoDB("localhost", 27017, dbName);

        DBIterator iter = db.getDbIteratorOverOrgs();

        Iterator<Organism> orgIterator = new Iterator<Organism>() {
            @Override
            public boolean hasNext() {
                boolean hasNext = iter.hasNext();
                if (!hasNext)
                    iter.close();
                return hasNext;
            }

            @Override
            public Organism next() {
                DBObject o = iter.next();
                return db.convertDBObjectToOrg(o);
            }

        };

        OrgMinimalPrefixGenerator prefixGenerator = new OrgMinimalPrefixGenerator(orgIterator);
        Map<String, String> minimalPrefixMapping = prefixGenerator.getMinimalPrefixMapping();

        GenbankInstaller installer = new GenbankInstaller(genbankFile, seqType, db, minimalPrefixMapping);
        installer.init();
    }

}

From source file:de.upb.wdqa.wdvd.FeatureExtractorConfiguration.java

private void readArgs(String[] args) {
    CommandLineParser parser = new DefaultParser();

    try {//  ww  w.  j av a2  s.  co m
        CommandLine cmd = parser.parse(options, args);

        labelFile = getFileFromOption(cmd, OPTION_LABEL_FILE);
        geolocationDbFile = getFileFromOption(cmd, OPTION_GEOLOCATION_DB_FILE);
        geolocationFeatureFile = getFileFromOption(cmd, OPTION_GEOLOCATION_FEATURE_FILE);
        revisionTagFile = getFileFromOption(cmd, OPTION_REVISION_TAG_FILE);

        List<String> argList = cmd.getArgList();

        if (argList.size() != 2) {
            printHelp();
        } else {
            revisionFile = new File(argList.get(0));
            featureFile = new File(argList.get(1));
        }
    } catch (ParseException exp) {
        System.err.print(exp);
    }
}

From source file:io.github.alechenninger.monarch.CliInputs.java

public static CliInputs parse(String[] args) throws ParseException {
    return parse(args, new DefaultParser());
}

From source file:com.github.braully.graph.BatchExecuteOperation.java

void processMain(String... args) {
    GraphCaratheodoryHeuristic.verbose = false;

    Options options = new Options();

    OptionGroup exec = new OptionGroup();
    exec.setRequired(false);//from  w ww. j av  a  2s  .  co m
    IGraphOperation[] opers = getOperations();
    Option[] execs = new Option[opers.length];
    for (int i = 0; i < opers.length; i++) {
        IGraphOperation oper = opers[i];
        execs[i] = new Option("" + i, false, oper.getName());
        options.addOption(execs[i]);
    }
    //        options.addOptionGroup(exec);

    Option input = new Option("i", "input", true, "input file or directory");
    options.addOption(input);

    Option cont = new Option("c", "continue", false, "continue from last processing");
    cont.setRequired(false);
    options.addOption(cont);

    Option verb = new Option("v", "verbose", false, "verbose processing");
    options.addOption(verb);

    Option output = new Option("o", "output", true, "output file");
    output.setRequired(false);
    options.addOption(output);

    CommandLineParser parser = new DefaultParser();
    HelpFormatter formatter = new HelpFormatter();
    CommandLine cmd;

    //        input.setRequired(true);
    //        exec.setRequired(true);
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        System.out.println(e.getMessage());
        formatter.printHelp("BatchExecuteOperation", options);
        System.exit(1);
        return;
    }

    boolean contProcess = false;

    String inputFilePath = cmd.getOptionValue("input");
    if (inputFilePath == null) {
        inputFilePath = getDefaultInput();
    }
    if (inputFilePath == null) {
        return;
    }

    if (cmd.hasOption("continue")) {
        contProcess = true;
    }

    if (cmd.hasOption("verbose")) {
        verbose = true;
    }

    List<IGraphOperation> operationsToExecute = new ArrayList<IGraphOperation>();
    for (int i = 0; i < opers.length; i++) {
        IGraphOperation oper = opers[i];
        String value = execs[i].getOpt();
        if (cmd.hasOption(value)) {
            operationsToExecute.add(oper);
        }
    }

    if (operationsToExecute.isEmpty()) {
        operationsToExecute.add(opers[0]);
        //            formatter.printHelp("BatchExecuteOperation", options);
        //            System.exit(1);
        //            return;
    }
    File dir = new File(inputFilePath);
    if (dir.isDirectory()) {
        processDirectory(operationsToExecute, inputFilePath, contProcess);
    } else if (inputFilePath.toLowerCase().endsWith(".mat")) {
        try {
            for (IGraphOperation operation : operationsToExecute) {
                processFileMat(operation, dir);
            }
        } catch (Exception ex) {
            Logger.getLogger(BatchExecuteOperation.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else if (inputFilePath.toLowerCase().endsWith(".g6")) {
        try {
            for (IGraphOperation operation : operationsToExecute) {
                processFileG6(operation, dir);
            }
        } catch (Exception ex) {
            Logger.getLogger(BatchExecuteOperation.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else if (inputFilePath.toLowerCase().endsWith(".g6.gz")) {
        try {
            for (IGraphOperation operation : operationsToExecute) {
                processFileG6GZ(operation, dir);
            }
        } catch (Exception ex) {
            Logger.getLogger(BatchExecuteOperation.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:cx2x.Cx2x.java

/**
 * Parse the arguments passed to the program.
 *
 * @param args Arguments passed to the program.
 * @return Parsed command line object./*w w w  . ja v  a2  s .  c om*/
 * @throws ParseException If one or several arguments are not found.
 */
private static CommandLine processCommandArgs(String[] args) throws ParseException {
    Options options = prepareOptions();
    CommandLineParser parser = new DefaultParser();
    return parser.parse(options, args);
}

From source file:com.norconex.collector.core.AbstractCollectorLauncher.java

protected CommandLine parseCommandLineArguments(String[] args) {
    Options options = new Options();
    options.addOption("c", ARG_CONFIG, true, "Required: Collector configuration file.");
    options.addOption("v", ARG_VARIABLES, true, "Optional: variable file.");
    options.addOption("a", ARG_ACTION, true, "Required: one of start|resume|stop");

    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = null;/*w  w  w  . java2s  .  c  o  m*/
    try {
        cmd = parser.parse(options, args);
        if (!cmd.hasOption(ARG_CONFIG) || !cmd.hasOption(ARG_ACTION) || EqualsUtil.equalsNone(
                cmd.getOptionValue(ARG_ACTION), ARG_ACTION_START, ARG_ACTION_RESUME, ARG_ACTION_STOP)) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("collector-http[.bat|.sh]", options);
            System.exit(-1);
        }
    } catch (ParseException e) {
        PrintStream err = System.err;
        err.println("Could not parse arguments.");
        e.printStackTrace(System.err);
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("collector-http[.bat|.sh]", options);
        System.exit(-1);
    }
    return cmd;
}

From source file:com.poker.brazillive.config.ConfigParser.java

private String getConfigPath(String configFilePath, String[] args) {

    Option optionConf = Option.builder("f").longOpt("conf").argName("configuration").hasArg()
            .desc("Path to the configuration file").build();

    Options options = new Options();
    CommandLineParser parser = new DefaultParser();
    options.addOption(optionConf);//  w w  w.j  a va 2  s  .  com
    try {
        CommandLine commandLine = parser.parse(options, args);
        if (commandLine.hasOption("f")) {
            configFilePath = commandLine.getOptionValue("f");
        } else if (commandLine.hasOption("conf")) {
            configFilePath = commandLine.getOptionValue("conf");
        }
    } catch (ParseException e) {
        LOGGER.log(Level.SEVERE, e.toString(), e);
    }
    return configFilePath;
}