Example usage for org.apache.commons.cli OptionBuilder isRequired

List of usage examples for org.apache.commons.cli OptionBuilder isRequired

Introduction

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

Prototype

public static OptionBuilder isRequired(boolean newRequired) 

Source Link

Document

The next Option created will be required if required is true.

Usage

From source file:org.magdaaproject.analysis.rhizome.RhizomeAnalysis.java

private static Options createOptions() {

    Options options = new Options();

    // task type/* w ww.j  a  va  2s  .c o  m*/
    OptionBuilder.withArgName("string");
    OptionBuilder.hasArg(true);
    OptionBuilder.withDescription("task to undertake");
    OptionBuilder.isRequired(true);
    options.addOption(OptionBuilder.create("task"));

    // properties file path
    OptionBuilder.withArgName("path");
    OptionBuilder.hasArg(true);
    OptionBuilder.withDescription("path to the properties file");
    OptionBuilder.isRequired(true);
    options.addOption(OptionBuilder.create("properties"));

    // table name
    OptionBuilder.withArgName("string");
    OptionBuilder.hasArg(true);
    OptionBuilder.withDescription("name of table to work with");
    OptionBuilder.isRequired(true);
    options.addOption(OptionBuilder.create("table"));

    // path to input database
    OptionBuilder.withArgName("path");
    OptionBuilder.hasArg(true);
    OptionBuilder.withDescription("path to a single input rhizome database");
    options.addOption(OptionBuilder.create("input"));

    // id of the tablet
    OptionBuilder.withArgName("string");
    OptionBuilder.hasArg(true);
    OptionBuilder.withDescription("id of the tablet");
    options.addOption(OptionBuilder.create("tablet"));

    // parent directory of data to import
    OptionBuilder.withArgName("path");
    OptionBuilder.hasArg(true);
    OptionBuilder.withDescription("path to the parent directory of a dataset");
    options.addOption(OptionBuilder.create("dataset"));

    // path to output file
    OptionBuilder.withArgName("path");
    OptionBuilder.hasArg(true);
    OptionBuilder.withDescription("path to an output file");
    options.addOption(OptionBuilder.create("output"));

    return options;
}

From source file:org.midonet.benchmarks.mpi.MPIBenchApp.java

protected static Path getCfgFilePath(String[] args) throws ConfigException {
    Option cfgOpt = OptionBuilder.isRequired(true).hasArg(true).withLongOpt(CFGFILE_OPTION)
            .withDescription("configuration file").create();
    Options options = new Options();
    options.addOption(cfgOpt);/*from   ww w. java 2 s.  c  o m*/

    CommandLineParser parser = new PosixParser();
    CommandLine cl;
    try {
        cl = parser.parse(options, args);
    } catch (Exception e) {
        throw new ConfigException(e);
    }
    Path cfgPath = Paths.get(cl.getOptionValue(CFGFILE_OPTION));
    if (!Files.exists(cfgPath)) {
        throw new ConfigException("config file does not exist: " + cfgPath.toString());
    }
    return cfgPath.toAbsolutePath();
}

From source file:org.nuunframework.cli.NuunCliPlugin.java

private Option createOptionFromField(Field field) {
    Option option = null;/*from ww  w  .  ja v  a 2s . c  o  m*/
    // Cli Option Builder is completly static :-/
    // so we synchronized it ...
    synchronized (OptionBuilder.class) {
        // reset the builder creating a dummy option
        OptionBuilder.withLongOpt("dummy");
        OptionBuilder.create();

        // 
        NuunOption nuunOption = field.getAnnotation(NuunOption.class);

        if (nuunOption == null) {
            for (Annotation anno : field.getAnnotations()) {
                if (AssertUtils.hasAnnotationDeep(anno.annotationType(), NuunOption.class)) {
                    nuunOption = AssertUtils.annotationProxyOf(NuunOption.class, anno);
                    break;
                }
            }
        }

        // longopt
        if (!Strings.isNullOrEmpty(nuunOption.longOpt())) {
            OptionBuilder.withLongOpt(nuunOption.longOpt());
        }
        // description
        if (!Strings.isNullOrEmpty(nuunOption.description())) {
            OptionBuilder.withDescription(nuunOption.description());
        }
        // required
        OptionBuilder.isRequired((nuunOption.required()));

        // arg
        OptionBuilder.hasArg((nuunOption.arg()));

        // args
        if (nuunOption.args()) {
            if (nuunOption.numArgs() > 0) {
                OptionBuilder.hasArgs(nuunOption.numArgs());
            } else {
                OptionBuilder.hasArgs();
            }
        }
        // is optional 
        if (nuunOption.optionalArg()) {
            OptionBuilder.hasOptionalArg();
        }

        // nuun 
        OptionBuilder.withValueSeparator(nuunOption.valueSeparator());

        // opt 
        if (!Strings.isNullOrEmpty(nuunOption.opt())) {
            option = OptionBuilder.create(nuunOption.opt());
        } else {
            option = OptionBuilder.create();
        }
    }

    return option;

}

From source file:org.openmainframe.ade.ext.main.Analyze.java

/**
 * Method to parse specific arguments for "Analyze".
 *//*from w  ww.ja va 2 s  .co  m*/
@Override
protected final void parseArgs(String[] args) throws AdeException {
    final Options options = new Options();

    OptionBuilder.withArgName(OPTION_SOURCES);
    OptionBuilder.withLongOpt(OPTION_SOURCES);
    OptionBuilder.isRequired(false);
    OptionBuilder.hasArg();
    OptionBuilder.withDescription("Specify the Source to be analyzed.  ");
    options.addOption(OptionBuilder.create("s"));

    super.parseArgs(options, args);

}

From source file:org.openmainframe.ade.ext.main.helper.AdeExtOptions.java

public static Options buildOptions(Options subClassOptions) {
    /* Add the options from subClass */
    Options options = new Options();
    for (Object subClassOption : subClassOptions.getOptions()) {
        Option option = (Option) subClassOption;
        options.addOption(option);//from w  w w  .j a  va 2  s  .  c om
    }

    /* Add the general options */
    OptionBuilder.withArgName(OPTION_HELP);
    OptionBuilder.withLongOpt(OPTION_HELP);
    OptionBuilder.isRequired(false);
    OptionBuilder.withDescription("Print help message and exit");
    options.addOption(OptionBuilder.create('h'));

    OptionBuilder.withArgName(OPTION_INPUT_FILE);
    OptionBuilder.withLongOpt(OPTION_INPUT_FILE);
    OptionBuilder.hasArg();
    OptionBuilder.isRequired(false);
    OptionBuilder.withDescription("Input file name or 'stdin'");
    options.addOption(OptionBuilder.create('f'));

    OptionBuilder.withArgName(OPTION_INPUT_DIR);
    OptionBuilder.withLongOpt(OPTION_INPUT_DIR);
    OptionBuilder.hasArg();
    OptionBuilder.isRequired(false);
    OptionBuilder.withDescription("Input dir name");
    options.addOption(OptionBuilder.create('d'));

    OptionBuilder.withArgName(OPTION_SOURCES);
    OptionBuilder.withLongOpt(OPTION_SOURCES);
    OptionBuilder.hasArg();
    OptionBuilder.isRequired(false);
    OptionBuilder.withDescription("Source Names.");
    options.addOption(OptionBuilder.create('s'));

    OptionBuilder.withArgName(OPTION_OS_TYPE);
    OptionBuilder.withLongOpt(OPTION_OS_TYPE);
    OptionBuilder.hasArg();
    OptionBuilder.isRequired(false);
    OptionBuilder.withDescription("The OS Type." + "If this option is omitted, the default is Linux");
    options.addOption(OptionBuilder.create('o'));

    OptionBuilder.withArgName(OPTION_GMT_OFFSET);
    OptionBuilder.withLongOpt(OPTION_GMT_OFFSET);
    OptionBuilder.hasArg();
    OptionBuilder.isRequired(false);
    OptionBuilder.withDescription("hours offset from GMT");
    options.addOption(OptionBuilder.create('g'));

    return options;
}

From source file:org.openmainframe.ade.ext.main.UpdateGroups.java

/**
 * Method for building the allowed options.
 * @param options Options object for adding the options created with OptionBuilder.
 */// w  w w  . jav a  2  s  . co m
private void buildOptions(Options options) {
    OptionBuilder.withArgName(OPTION_HELP);
    OptionBuilder.withLongOpt(OPTION_HELP);
    OptionBuilder.isRequired(false);
    OptionBuilder.withDescription("Print help message and exit.");
    options.addOption(OptionBuilder.create('h'));

    OptionBuilder.withArgName(OPTION_INPUT_JSON);
    OptionBuilder.withLongOpt(OPTION_INPUT_JSON);
    OptionBuilder.isRequired(false);
    OptionBuilder.hasArg();
    OptionBuilder.withDescription("Specify the JSON input file.");
    options.addOption(OptionBuilder.create("j"));
}

From source file:org.openmainframe.ade.ext.main.Upload.java

/**
 * Method to parse specific arguments for "Upload".
 * /*from   w  w w. j a v a  2s.com*/
 * @throws AdeException 
 * 
 */
protected final void parseArgs(String[] args) throws AdeException {
    final Options options = new Options();

    OptionBuilder.withArgName(OPTION_DUMP_PARSE_REPORT);
    OptionBuilder.isRequired(false);
    OptionBuilder.withDescription("Specify this option to generate parse report");
    options.addOption(OptionBuilder.create(OPTION_DUMP_PARSE_REPORT));

    final CommandLine line = super.parseArgs(options, args);

    if (line.hasOption(OPTION_DUMP_PARSE_REPORT)) {
        getAdeExtProperties().setParseReportRequested(true);
    }
}

From source file:org.openmainframe.ade.main.ControlDB.java

@Override
protected void parseArgs(String[] args) throws AdeException {

    final Option helpOpt = new Option("h", "help", false, "Print help message and exit");
    final Option forceOpt = new Option("f", "force", false, "Force operation. Do not prompt for confirmation");

    OptionBuilder.withLongOpt("create");
    OptionBuilder.hasArg(false);//  w  ww. ja  v  a2  s .  c  om
    OptionBuilder.isRequired(false);
    OptionBuilder.withDescription("Create Ade DB tables");
    final Option createOpt = OptionBuilder.create('c');

    OptionBuilder.withLongOpt("drop");
    OptionBuilder.hasArg(false);
    OptionBuilder.isRequired(false);
    OptionBuilder.withDescription("Drops all Ade DB tables, and clears the data store dictionaries");
    final Option deleteOpt = OptionBuilder.create('d');

    OptionBuilder.withLongOpt("reset");
    OptionBuilder.hasArg(false);
    OptionBuilder.isRequired(false);
    OptionBuilder.withDescription("Clears all Ade DB tables content");
    final Option resetOpt = OptionBuilder.create('r');

    OptionBuilder.withLongOpt("query");
    OptionBuilder.hasArg();
    OptionBuilder.withArgName("SQL query string");
    OptionBuilder.isRequired(false);
    OptionBuilder.withDescription("Performs the input query");
    final Option queryOpt = OptionBuilder.create('q');

    final OptionGroup actionGroupOpt = new OptionGroup().addOption(createOpt).addOption(deleteOpt)
            .addOption(resetOpt).addOption(queryOpt);
    actionGroupOpt.setRequired(true);

    final Options options = new Options();
    options.addOption(helpOpt);
    options.addOption(forceOpt);
    options.addOptionGroup(actionGroupOpt);

    final CommandLineParser parser = new GnuParser();
    CommandLine line = null;

    try {
        // parse the command line arguments
        line = parser.parse(options, args);
    } catch (MissingOptionException exp) {
        logger.error("Command line parsing failed.", exp);
        new HelpFormatter().printHelp(ControlDB.class.getName(), options);
        System.exit(0);
    } catch (ParseException exp) {
        // oops, something went wrong
        logger.error("Parsing failed.  Reason: " + exp.getMessage());
        throw new AdeUsageException("Argument Parsing failed", exp);
    }

    if (line.hasOption('h')) {
        new HelpFormatter().printHelp(getClass().getSimpleName(), options);
        closeAll();
        System.exit(0);
    }

    if (line.hasOption('f')) {
        m_forceOp = true;
    }

    if (line.hasOption('c')) {
        m_op = ControlDBOperator.Create;
    } else if (line.hasOption('d')) {
        m_op = ControlDBOperator.Drop;
    } else if (line.hasOption('r')) {
        m_op = ControlDBOperator.Reset;
    } else if (line.hasOption('q')) {
        m_op = ControlDBOperator.Query;
        m_args = new String[] { line.getOptionValue('q') };
    }
}

From source file:org.openmainframe.ade.main.Train.java

@Override
protected void parseArgs(String[] args) throws AdeException {
    final Option helpOpt = new Option("h", "help", false, "Print help message and exit");

    OptionBuilder.withLongOpt(ALL_OPT);//from ww  w  .j ava 2 s.c  om
    OptionBuilder.isRequired(false);
    OptionBuilder.withDescription("All analysis groups");
    final Option allAnalysisGroupsOpt = OptionBuilder.create('a');

    OptionBuilder.withLongOpt(GROUPS_OPT);
    OptionBuilder.withArgName("ANALYSIs GROUPS");
    OptionBuilder.hasArgs();
    OptionBuilder.isRequired(false);
    OptionBuilder.withDescription("Selected analysis groups");
    final Option selectAnalysisGroupsOpt = OptionBuilder.create('s');

    final OptionGroup inputAnalysisGroupsOptGroup = new OptionGroup().addOption(allAnalysisGroupsOpt)
            .addOption(selectAnalysisGroupsOpt);
    inputAnalysisGroupsOptGroup.setRequired(true);

    OptionBuilder.withLongOpt(UNSELECT_OPT);
    OptionBuilder.withArgName("ANALYSIS GROUPS");
    OptionBuilder.hasArgs();
    OptionBuilder.isRequired(false);
    OptionBuilder.withDescription("Unselect analysis groups. Used only with '" + ALL_OPT + "'");
    final Option unselectAnalysisGroupsOpt = OptionBuilder.create('u');

    OptionBuilder.withLongOpt(DURATION_OPT);
    OptionBuilder.withArgName("DURATION (ISO 8601)");
    OptionBuilder.isRequired(false);
    OptionBuilder.hasArg();
    OptionBuilder.withDescription(
            "Duration from/to start/end date. Defaults to infinity. Replaces either 'start-date' or 'end-date'");
    final Option periodOpt = OptionBuilder.create();

    OptionBuilder.withLongOpt(NUM_DAYS_OPT);
    OptionBuilder.withArgName("INT");
    OptionBuilder.hasArg();
    OptionBuilder.isRequired(false);
    OptionBuilder.withDescription("Number of days. same as '" + DURATION_OPT + "'");
    final Option numDaysOpt = OptionBuilder.create('n');

    final OptionGroup periodOptGroup = new OptionGroup().addOption(periodOpt).addOption(numDaysOpt);

    OptionBuilder.withLongOpt(START_DATE_OPT);
    OptionBuilder.withArgName("MM/dd/yyyy[ HH:mm][ Z]");
    OptionBuilder.hasArg();
    OptionBuilder.isRequired(false);
    OptionBuilder.withDescription(
            "Start of date range. Optional. Replaces 'duration'/'num-days' when used along with 'end-date'");
    final Option startDateOpt = OptionBuilder.create();

    OptionBuilder.withLongOpt(END_DATE_OPT);
    OptionBuilder.withArgName("MM/dd/yyyy[ HH:mm][ Z]");
    OptionBuilder.hasArg();
    OptionBuilder.isRequired(false);
    OptionBuilder
            .withDescription("End of date range. Defaults to this moment. Replaces 'duration'/'num-days' when"
                    + " used along with 'start-date'");
    final Option endDateOpt = OptionBuilder.create();

    final Options options = new Options();
    options.addOption(helpOpt);
    options.addOptionGroup(inputAnalysisGroupsOptGroup);
    options.addOption(unselectAnalysisGroupsOpt);
    options.addOptionGroup(periodOptGroup);
    options.addOption(endDateOpt);
    options.addOption(startDateOpt);

    final CommandLineParser parser = new GnuParser();
    CommandLine line;

    try {
        // parse the command line arguments
        line = parser.parse(options, args);
    } catch (MissingOptionException exp) {
        new HelpFormatter().printHelp(HELP + "\nOptions:", options);
        throw new AdeUsageException("Command line parsing failed", exp);
    } catch (ParseException exp) {
        // oops, something went wrong
        throw new AdeUsageException("Argument Parsing failed", exp);
    }

    if (line.hasOption(helpOpt.getLongOpt())) {
        new HelpFormatter().printHelp(HELP, options);
        closeAll();
        System.exit(0);
    }

    if (line.hasOption(UNSELECT_OPT) && !line.hasOption(ALL_OPT)) {
        throw new AdeUsageException("'" + UNSELECT_OPT + "' cannot be used without '" + ALL_OPT + "'");
    }

    final Set<Integer> allAnalysisGroups = Ade.getAde().getDataStore().sources().getAllAnalysisGroups();
    if (line.hasOption(ALL_OPT)) {
        System.out.println("Operating on all available analysis groups");
        if (!line.hasOption(UNSELECT_OPT)) {
            m_analysisGroups = allAnalysisGroups;
        } else {
            final Set<Integer> unselectedAnalysisGroups = parseAnalysisGroups(allAnalysisGroups,
                    line.getOptionValues(UNSELECT_OPT));
            final Set<String> unselectedGroupNames = getGroupNames(unselectedAnalysisGroups);
            System.out.println("Omitting analysis groups: " + unselectedGroupNames.toString());
            m_analysisGroups = new TreeSet<Integer>(allAnalysisGroups);
            m_analysisGroups.removeAll(unselectedAnalysisGroups);
        }
    } else if (line.hasOption(GROUPS_OPT)) {
        m_analysisGroups = parseAnalysisGroups(allAnalysisGroups, line.getOptionValues(GROUPS_OPT));
        final Set<String> operatingAnalysisGroups = getGroupNames(m_analysisGroups);
        System.out.println("Operating on analysis groups: " + operatingAnalysisGroups.toString());
    }

    if ((line.hasOption(NUM_DAYS_OPT) || line.hasOption(DURATION_OPT)) && line.hasOption(START_DATE_OPT)
            && line.hasOption(END_DATE_OPT)) {
        throw new AdeUsageException("Cannot use '" + DURATION_OPT + "'/'" + NUM_DAYS_OPT + "', '"
                + START_DATE_OPT + "' and '" + END_DATE_OPT + "' together");
    }
    if (line.hasOption(NUM_DAYS_OPT)) {
        final String numDaysStr = line.getOptionValue(NUM_DAYS_OPT);
        final int numDays = Integer.parseInt(numDaysStr);
        this.m_period = Period.days(numDays);
    }
    if (line.hasOption(DURATION_OPT)) {
        final String periodStr = line.getOptionValue(DURATION_OPT);
        this.m_period = ISOPeriodFormat.standard().parsePeriod(periodStr);
    }
    if (line.hasOption(START_DATE_OPT)) {
        m_startDate = parseDate(line.getOptionValue(START_DATE_OPT));
    }
    if (line.hasOption(END_DATE_OPT)) {
        m_endDate = parseDate(line.getOptionValue(END_DATE_OPT));
    }
}

From source file:org.projectforge.webserver.CommandLineStart.java

@SuppressWarnings("static-access")
private static Option createOption(char flag, String longOpt, boolean required, String description) {
    Option option = OptionBuilder.isRequired(required).withDescription(description).withLongOpt(longOpt)
            .create(flag);//from w  ww  .j a v a 2s. c o  m
    return option;
}