Example usage for org.apache.commons.cli CommandLine getOptionValues

List of usage examples for org.apache.commons.cli CommandLine getOptionValues

Introduction

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

Prototype

public String[] getOptionValues(char opt) 

Source Link

Document

Retrieves the array of values, if any, of an option.

Usage

From source file:etymology.config.CommandLineReader.java

private static List<String> getWordsToMonitor(CommandLine cl) {
    String[] words = cl.getOptionValues(WORDS_TO_MONITOR);
    // set languages to align
    if (words == null || words.length == 0) {
        //if(Configuration.getInstance().getWordsToMonitor() != null){
        //    return Configuration.getInstance().getWordsToMonitor();
        //}//w  w w. j av  a2s .c  o m
        return null;
    }

    return Arrays.asList(words);
}

From source file:etymology.config.CommandLineReader.java

private static List<String> getLanguages(CommandLine cl) {
    String[] langs = cl.getOptionValues(LANGUAGES_OPTION);
    // set languages to align
    if (langs == null || langs.length == 0 || langs.length > 3) {
        if (Configuration.getInstance().getLanguages() != null) {
            return Configuration.getInstance().getLanguages();//.toArray();
        } else {/*from  w ww.  j a  v  a  2s.  co  m*/
            langs = new String[] { "FIN", "EST" };
        }
    }

    return Arrays.asList(langs);
}

From source file:io.janusproject.Boot.java

/** Parse the command line.
 *
 * @param args - the CLI arguments given to the program.
 * @param propertyFiles - files that may be filled with the filenames given on the CLI.
 * @return the arguments that are not recognized as CLI options.
 *///from   w w w .j av  a2s.  co  m
public static String[] parseCommandLine(String[] args, List<URL> propertyFiles) {
    CommandLineParser parser = new GnuParser();
    try {
        CommandLine cmd = parser.parse(getOptions(), args);
        parseCommandForInfoOptions(cmd);
        parseCommandLineForSystemProperties(cmd);
        parseCommandLineForVerbosity(cmd);
        // Retreive the list of the property files given on CLI
        if (cmd.hasOption('f')) {
            for (String rawFilename : cmd.getOptionValues('f')) {
                if (rawFilename == null || "".equals(rawFilename)) { //$NON-NLS-1$
                    showHelp();
                }
                File file = new File(rawFilename);
                if (!file.canRead()) {
                    //CHECKSTYLE:OFF
                    System.err.println(Locale.getString("INVALID_PROPERTY_FILENAME", //$NON-NLS-1$
                            rawFilename));
                    //CHECKSTYLE:ON
                    System.exit(ERROR_EXIT_CODE);
                }
                propertyFiles.add(file.toURI().toURL());
            }
        }
        return cmd.getArgs();
    } catch (IOException | ParseException e) {
        //CHECKSTYLE:OFF
        e.printStackTrace();
        //CHECKSTYLE:ON
        showHelp();
        // Only to avoid compilation errors
        throw new Error();
    }
}

From source file:br.edu.ufcg.lsd.oursim.ui.CLI.java

private static JobSchedulerPolicy defineScheduler(CommandLine cmd, List<Peer> peers) {
    JobSchedulerPolicy jobScheduler = null;
    if (cmd.hasOption(SCHEDULER)) {
        String scheduler = cmd.getOptionValue(SCHEDULER);
        if (scheduler.equals("persistent")) {
            jobScheduler = new OurGridPersistentScheduler(peers);
        } else if (scheduler.equals("replication") && cmd.getOptionValues(SCHEDULER).length == 2) {
            int numberOfReplicas = Integer.parseInt(cmd.getOptionValues(SCHEDULER)[1]);
            jobScheduler = new OurGridReplicationScheduler(peers, numberOfReplicas);
        }/*from w ww.j  a v  a2  s  . c  om*/
    } else {
        jobScheduler = new OurGridScheduler(peers);
    }

    if (jobScheduler == null) {
        showMessageAndExit("Deve informar um tipo vlido de scheduler.");
    }

    return jobScheduler;
}

From source file:com.google.api.codegen.GeneratorMain.java

public static void gapicGeneratorMain(ArtifactType artifactType, String[] args) throws Exception {
    Options options = new Options();
    options.addOption("h", "help", false, "show usage");
    options.addOption(DESCRIPTOR_SET_OPTION);
    options.addOption(SERVICE_YAML_OPTION);
    // TODO make required after artman passes this in
    options.addOption(LANGUAGE_NONREQUIRED_OPTION);
    options.addOption(GAPIC_YAML_OPTION);
    options.addOption(PACKAGE_YAML2_OPTION);
    options.addOption(OUTPUT_OPTION);//from   w  w w . ja v  a2 s .  c  o  m
    Option enabledArtifactsOption = Option.builder().longOpt("enabled_artifacts")
            .desc("Optional. Artifacts enabled for the generator. "
                    + "Currently supports 'surface' and 'test'.")
            .hasArg().argName("ENABLED_ARTIFACTS").required(false).build();
    options.addOption(enabledArtifactsOption);

    CommandLine cl = (new DefaultParser()).parse(options, args);
    if (cl.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("GapicGeneratorTool", options);
    }

    ToolOptions toolOptions = ToolOptions.create();
    toolOptions.set(ToolOptions.DESCRIPTOR_SET, cl.getOptionValue(DESCRIPTOR_SET_OPTION.getLongOpt()));
    toolOptions.set(ToolOptions.CONFIG_FILES,
            Lists.newArrayList(cl.getOptionValues(SERVICE_YAML_OPTION.getLongOpt())));
    toolOptions.set(GapicGeneratorApp.LANGUAGE, cl.getOptionValue(LANGUAGE_NONREQUIRED_OPTION.getLongOpt()));
    toolOptions.set(GapicGeneratorApp.OUTPUT_FILE, cl.getOptionValue(OUTPUT_OPTION.getLongOpt(), ""));
    toolOptions.set(GapicGeneratorApp.GENERATOR_CONFIG_FILES,
            Lists.newArrayList(cl.getOptionValues(GAPIC_YAML_OPTION.getLongOpt())));
    toolOptions.set(GapicGeneratorApp.PACKAGE_CONFIG2_FILE,
            cl.getOptionValue(PACKAGE_YAML2_OPTION.getLongOpt()));

    checkFile(toolOptions.get(ToolOptions.DESCRIPTOR_SET));
    checkFiles(toolOptions.get(ToolOptions.CONFIG_FILES));
    checkFiles(toolOptions.get(GapicGeneratorApp.GENERATOR_CONFIG_FILES));
    if (!Strings.isNullOrEmpty(toolOptions.get(GapicGeneratorApp.PACKAGE_CONFIG2_FILE))) {
        checkFile(toolOptions.get(GapicGeneratorApp.PACKAGE_CONFIG2_FILE));
    }

    if (cl.getOptionValues(enabledArtifactsOption.getLongOpt()) != null) {
        toolOptions.set(GapicGeneratorApp.ENABLED_ARTIFACTS,
                Lists.newArrayList(cl.getOptionValues(enabledArtifactsOption.getLongOpt())));
    }
    GapicGeneratorApp codeGen = new GapicGeneratorApp(toolOptions, artifactType);
    int exitCode = codeGen.run();
    System.exit(exitCode);
}

From source file:groovy.ui.GroovyMain.java

/**
 * Process the users request.// ww  w  .java 2 s  .  c om
 *
 * @param line the parsed command line.
 * @throws ParseException if invalid options are chosen
 */
private static boolean process(CommandLine line) throws ParseException, IOException {
    List args = line.getArgList();

    if (line.hasOption('D')) {
        String[] values = line.getOptionValues('D');

        for (int i = 0; i < values.length; i++) {
            setSystemPropertyFrom(values[i]);
        }
    }

    GroovyMain main = new GroovyMain();

    // add the ability to parse scripts with a specified encoding
    main.conf.setSourceEncoding(line.getOptionValue('c', main.conf.getSourceEncoding()));

    main.isScriptFile = !line.hasOption('e');
    main.debug = line.hasOption('d');
    main.conf.setDebug(main.debug);
    main.processFiles = line.hasOption('p') || line.hasOption('n');
    main.autoOutput = line.hasOption('p');
    main.editFiles = line.hasOption('i');
    if (main.editFiles) {
        main.backupExtension = line.getOptionValue('i');
    }
    main.autoSplit = line.hasOption('a');
    String sp = line.getOptionValue('a');
    if (sp != null)
        main.splitPattern = sp;

    if (main.isScriptFile) {
        if (args.isEmpty())
            throw new ParseException("neither -e or filename provided");

        main.script = (String) args.remove(0);
        if (main.script.endsWith(".java"))
            throw new ParseException("error: cannot compile file with .java extension: " + main.script);
    } else {
        main.script = line.getOptionValue('e');
    }

    main.processSockets = line.hasOption('l');
    if (main.processSockets) {
        String p = line.getOptionValue('l', "1960"); // default port to listen to
        main.port = Integer.parseInt(p);
    }

    // we use "," as default, because then split will create
    // an empty array if no option is set
    String disabled = line.getOptionValue("disableopt", ",");
    String[] deopts = disabled.split(",");
    for (String deopt_i : deopts) {
        main.conf.getOptimizationOptions().put(deopt_i, false);
    }

    if (line.hasOption("indy")) {
        CompilerConfiguration.DEFAULT.getOptimizationOptions().put("indy", true);
        main.conf.getOptimizationOptions().put("indy", true);
    }

    if (line.hasOption("basescript")) {
        main.conf.setScriptBaseClass(line.getOptionValue("basescript"));
    }

    if (line.hasOption("configscript")) {
        String path = line.getOptionValue("configscript");
        File groovyConfigurator = new File(path);
        Binding binding = new Binding();
        binding.setVariable("configuration", main.conf);

        CompilerConfiguration configuratorConfig = new CompilerConfiguration();
        ImportCustomizer customizer = new ImportCustomizer();
        customizer
                .addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder");
        configuratorConfig.addCompilationCustomizers(customizer);

        GroovyShell shell = new GroovyShell(binding, configuratorConfig);
        shell.evaluate(groovyConfigurator);
    }

    main.args = args;

    return main.run();
}

From source file:groovyx.groovyserv.GroovyMain2.java

/**
 * Process the users request./*from   w w  w.j a  va 2s  .com*/
 *
 * @param line the parsed command line.
 * @throws ParseException if invalid options are chosen
 */
private static boolean process(CommandLine line, String classpath) throws ParseException, IOException { // for GroovyServ
    List args = line.getArgList();

    if (line.hasOption('D')) {
        String[] values = line.getOptionValues('D');

        for (int i = 0; i < values.length; i++) {
            setSystemPropertyFrom(values[i]);
        }
    }

    GroovyMain2 main = new GroovyMain2();

    // add the ability to parse scripts with a specified encoding
    main.conf.setSourceEncoding(line.getOptionValue('c', main.conf.getSourceEncoding()));

    main.isScriptFile = !line.hasOption('e');
    main.debug = line.hasOption('d');
    main.conf.setDebug(main.debug);
    main.processFiles = line.hasOption('p') || line.hasOption('n');
    main.autoOutput = line.hasOption('p');
    main.editFiles = line.hasOption('i');
    if (main.editFiles) {
        main.backupExtension = line.getOptionValue('i');
    }
    main.autoSplit = line.hasOption('a');
    String sp = line.getOptionValue('a');
    if (sp != null)
        main.splitPattern = sp;

    if (main.isScriptFile) {
        if (args.isEmpty())
            throw new ParseException("neither -e or filename provided");

        main.script = (String) args.remove(0);
        if (main.script.endsWith(".java"))
            throw new ParseException("error: cannot compile file with .java extension: " + main.script);
    } else {
        main.script = line.getOptionValue('e');
    }

    main.processSockets = line.hasOption('l');
    if (main.processSockets) {
        String p = line.getOptionValue('l', "1960"); // default port to listen to
        main.port = Integer.parseInt(p);
    }

    // we use "," as default, because then split will create
    // an empty array if no option is set
    String disabled = line.getOptionValue("disableopt", ",");
    String[] deopts = disabled.split(",");
    for (String deopt_i : deopts) {
        main.conf.getOptimizationOptions().put(deopt_i, false);
    }

    if (line.hasOption("indy")) {
        CompilerConfiguration.DEFAULT.getOptimizationOptions().put("indy", true);
        main.conf.getOptimizationOptions().put("indy", true);
    }

    if (line.hasOption("basescript")) {
        main.conf.setScriptBaseClass(line.getOptionValue("basescript"));
    }

    if (line.hasOption("configscript")) {
        String path = line.getOptionValue("configscript");
        File groovyConfigurator = new File(path);
        Binding binding = new Binding();
        binding.setVariable("configuration", main.conf);

        CompilerConfiguration configuratorConfig = new CompilerConfiguration();
        ImportCustomizer customizer = new ImportCustomizer();
        customizer
                .addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder");
        configuratorConfig.addCompilationCustomizers(customizer);

        GroovyShell shell = new GroovyShell(binding, configuratorConfig);
        shell.evaluate(groovyConfigurator);
    }

    main.args = args;

    // for GroovyServ: classpath is given by GroovyInvokeHandler
    if (classpath != null) {
        main.conf.setClasspath(classpath);
    }

    return main.run();
}

From source file:com.google.api.ads.adwords.keywordoptimizer.KeywordOptimizer.java

/**
 * Adds the locations specified from the command line to the {@link SeedGenerator}.
 *
 * @param cmdLine the parsed command line parameters
 * @param seedGenerator the previously constructed {@link SeedGenerator}
 *//*from  w ww . j  ava2 s .  co  m*/
private static void addLocations(CommandLine cmdLine, SeedGenerator seedGenerator) {
    if (!cmdLine.hasOption("loc")) {
        return;
    }

    for (String location : cmdLine.getOptionValues("loc")) {
        long loc = Long.parseLong(location);

        log("Using location: " + loc);
        seedGenerator.addAdditionalLocation(loc);
    }
}

From source file:com.google.api.ads.adwords.keywordoptimizer.KeywordOptimizer.java

/**
 * Adds the languages specified from the command line to the {@link SeedGenerator}.
 *
 * @param cmdLine the parsed command line parameters
 * @param seedGenerator the previously constructed {@link SeedGenerator}
 */// w w  w.j a  v a  2s. c o  m
private static void addLanguages(CommandLine cmdLine, SeedGenerator seedGenerator) {
    if (!cmdLine.hasOption("lang")) {
        return;
    }

    for (String language : cmdLine.getOptionValues("lang")) {
        long lang = Long.parseLong(language);

        log("Using language: " + lang);
        seedGenerator.addAdditionalLanguage(lang);
    }
}

From source file:com.google.api.codegen.GeneratorMain.java

public static void gapicConfigGeneratorMain(String[] args) throws Exception {
    Options options = new Options();
    options.addOption("h", "help", false, "show usage");
    options.addOption(DESCRIPTOR_SET_OPTION);
    options.addOption(SERVICE_YAML_OPTION);
    options.addOption(OUTPUT_OPTION);//  w w w .  j  a  v  a  2  s  .c  o m

    CommandLine cl = (new DefaultParser()).parse(options, args);
    if (cl.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("ConfigGeneratorTool", options);
    }

    ToolOptions toolOptions = ToolOptions.create();
    toolOptions.set(GapicConfigGeneratorApp.OUTPUT_FILE, cl.getOptionValue(OUTPUT_OPTION.getLongOpt()));
    toolOptions.set(ToolOptions.DESCRIPTOR_SET, cl.getOptionValue(DESCRIPTOR_SET_OPTION.getLongOpt()));
    toolOptions.set(ToolOptions.CONFIG_FILES,
            Lists.newArrayList(cl.getOptionValues(SERVICE_YAML_OPTION.getLongOpt())));
    GapicConfigGeneratorApp configGen = new GapicConfigGeneratorApp(toolOptions);
    int exitCode = configGen.run();
    System.exit(exitCode);
}