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:cloudlens.cli.Main.java

public static void main(String[] args) throws Exception {
    final CommandLineParser optionParser = new DefaultParser();
    final HelpFormatter formatter = new HelpFormatter();

    final Option lens = Option.builder("r").longOpt("run").hasArg().argName("lens file").desc("Lens file.")
            .required(true).build();/*w w w  .  j  a v  a 2 s. c  o  m*/
    final Option log = Option.builder("l").longOpt("log").hasArg().argName("log file").desc("Log file.")
            .build();
    final Option jsonpath = Option.builder().longOpt("jsonpath").hasArg().argName("path")
            .desc("Path to logs in a json object.").build();
    final Option js = Option.builder().longOpt("js").hasArg().argName("js file").desc("Load JS file.").build();
    final Option format = Option.builder("f").longOpt("format").hasArg()
            .desc("Choose log format (text or json).").build();
    final Option streaming = Option.builder().longOpt("stream").desc("Streaming mode.").build();
    final Option history = Option.builder().longOpt("history").desc("Store history.").build();

    final Options options = new Options();
    options.addOption(log);
    options.addOption(lens);
    options.addOption(format);
    options.addOption(jsonpath);
    options.addOption(js);
    options.addOption(streaming);
    options.addOption(history);

    try {
        final CommandLine cmd = optionParser.parse(options, args);

        final String jsonPath = cmd.getOptionValue("jsonpath");
        final String[] jsFiles = cmd.getOptionValues("js");
        final String[] lensFiles = cmd.getOptionValues("run");
        final String[] logFiles = cmd.getOptionValues("log");
        final String source = cmd.getOptionValue("format");

        final boolean stream = cmd.hasOption("stream") || !cmd.hasOption("log");
        final boolean withHistory = cmd.hasOption("history") || !stream;

        final CL cl = new CL(System.out, System.err, stream, withHistory);

        try {
            final InputStream input = (cmd.hasOption("log")) ? FileReader.readFiles(logFiles) : System.in;

            if (source == null) {
                cl.source(input);
            } else {
                switch (source) {
                case "text":
                    cl.source(input);
                    break;
                case "json":
                    cl.json(input, jsonPath);
                    break;
                default:
                    input.close();
                    throw new CLException("Unsupported format: " + source);
                }
            }

            for (final String jsFile : FileReader.fullPaths(jsFiles)) {
                cl.engine.eval("CL.loadjs('file://" + jsFile + "')");
            }

            final List<ASTElement> top = ASTBuilder.parseFiles(lensFiles);
            cl.launch(top);

        } catch (final CLException | ASTException e) {
            cl.errWriter.println(e.getMessage());
        } finally {
            cl.outWriter.flush();
            cl.errWriter.flush();
        }
    } catch (final ParseException e) {
        System.err.println(e.getMessage());
        formatter.printHelp("cloudlens", options);
    }
}

From source file:com.google.api.codegen.configgen.ConfigGeneratorTool.java

public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption("h", "help", false, "show usage");
    options.addOption(Option.builder().longOpt("descriptor_set")
            .desc("The descriptor set representing the compiled input protos.").hasArg()
            .argName("DESCRIPTOR-SET").required(true).build());
    options.addOption(//w ww .  j a v a  2s  .  com
            Option.builder().longOpt("service_yaml").desc("The service YAML configuration file or files.")
                    .hasArg().argName("SERVICE-YAML").required(true).build());
    options.addOption(
            Option.builder("o").longOpt("output").desc("The directory in which to output the generated config.")
                    .hasArg().argName("OUTPUT-FILE").required(true).build());

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

    generate(cl.getOptionValue("descriptor_set"), cl.getOptionValues("service_yaml"),
            cl.getOptionValue("output"));
}

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

public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption("h", "help", false, "show usage");
    options.addOption(Option.builder().longOpt("discovery_doc")
            .desc("The Discovery doc representing the service description.").hasArg().argName("DISCOVERY-DOC")
            .required(true).build());/* ww  w  .  ja v  a 2 s.  c om*/
    options.addOption(Option.builder().longOpt("overrides").desc("The path to the sample config overrides file")
            .hasArg().argName("OVERRIDES").build());
    options.addOption(Option.builder().longOpt("gapic_yaml").desc("The GAPIC YAML configuration file or files.")
            .hasArg().argName("GAPIC-YAML").required(true).build());
    options.addOption(Option.builder("o").longOpt("output")
            .desc("The directory in which to output the generated fragments.").hasArg()
            .argName("OUTPUT-DIRECTORY").build());
    options.addOption(Option.builder().longOpt("auth_instructions")
            .desc("An @-delimited map of language to auth instructions URL: lang:URL@lang:URL@...").hasArg()
            .argName("AUTH-INSTRUCTIONS").build());

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

    generate(cl.getOptionValue("discovery_doc"), cl.getOptionValues("gapic_yaml"),
            cl.getOptionValue("overrides", ""), cl.getOptionValue("output", ""),
            cl.getOptionValue("auth_instructions", ""));
}

From source file:net.extemporal.test.Main.java

public static void main(String[] args) throws IOException {
    Options opts = new Options();
    opts.addOption(OptionBuilder.withDescription("email id for 1st account").hasArg(true).create("e1"));
    opts.addOption(/*from   ww  w. ja v  a  2  s. c  o  m*/
            OptionBuilder.withDescription("either gmail password or full URNs for Store and Transport servers")
                    .hasOptionalArgs(3).create("c1"));

    opts.addOption(OptionBuilder.withDescription("email id for 2nd account").hasArg(true).create("e2"));
    opts.addOption(
            OptionBuilder.withDescription("either gmail password or full URNs for Store and Transport servers")
                    .hasOptionalArgs(3).create("c2"));

    // this parses the command line but doesn't throw an exception on unknown options
    try {
        CommandLine cmd = new GnuParser().parse(opts, args, true);
        if (cmd.hasOption("e1") && cmd.hasOption("c1")) {
            String e = cmd.getOptionValue("e1");
            String[] accounts = cmd.getOptionValues("c1");
            if (accounts.length == 1)
                email1 = EmailTestPlatform.getGMailCredentials(e, accounts[0]);
            else if (email1.length == 2) {
                email1 = new String[] { e, accounts[0], accounts[1] };
            }
        }
        if (cmd.hasOption("e2") && cmd.hasOption("c2")) {
            String e = cmd.getOptionValue("e2");
            String[] accounts = cmd.getOptionValues("c2");
            if (accounts.length == 1)
                email2 = EmailTestPlatform.getGMailCredentials(e, accounts[0]);
            else if (email2.length == 2) {
                email2 = new String[] { e, accounts[0], accounts[1] };
            }
        }
    } catch (ParseException pex) {
        System.err.println("error parsing commandline: " + pex);
    }

    ShellFactory.createConsoleShell("test-harness", "Type '?list' to see valid commands", new Main())
            .commandLoop();
}

From source file:com.google.api.codegen.grpcmetadatagen.GrpcMetadataGeneratorTool.java

public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption("h", "help", false, "show usage");
    options.addOption(Option.builder().longOpt("descriptor_set")
            .desc("The descriptor set representing the compiled input protos.").hasArg()
            .argName("DESCRIPTOR-SET").required(true).build());
    options.addOption(/*w  w  w. j ava  2s . co m*/
            Option.builder().longOpt("service_yaml").desc("The service YAML configuration file or files.")
                    .hasArg().argName("SERVICE-YAML").required(true).build());
    options.addOption(
            Option.builder("i").longOpt("input").desc("The input directory containing the gRPC package.")
                    .hasArg().argName("INPUT-DIR").required(true).build());
    options.addOption(
            Option.builder("o").longOpt("output").desc("The directory in which to output the generated config.")
                    .hasArg().argName("OUTPUT-DIR").required(true).build());
    options.addOption(
            Option.builder("l").longOpt("language").desc("The language for which to generate package metadata.")
                    .hasArg().argName("LANGUAGE").required(true).build());
    options.addOption(Option.builder("c").longOpt("metadata_config")
            .desc("The YAML file configuring the package metadata.").hasArg().argName("METADATA-CONFIG")
            .required(true).build());

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

    generate(cl.getOptionValue("descriptor_set"), cl.getOptionValues("service_yaml"),
            cl.getOptionValue("input"), cl.getOptionValue("output"), cl.getOptionValue("language"),
            cl.getOptionValue("metadata_config"));
}

From source file:com.mebigfatguy.roomstore.RoomStore.java

public static void main(String[] args) {
    Options options = createOptions();/* www  . j a  va2  s . c om*/

    try {
        CommandLineParser parser = new GnuParser();
        CommandLine cmdLine = parser.parse(options, args);
        String nickname = cmdLine.getOptionValue(NICK_NAME);
        String server = cmdLine.getOptionValue(IRCSERVER);
        String[] channels = cmdLine.getOptionValues(CHANNELS);
        String[] endPoints = cmdLine.getOptionValues(ENDPOINTS);
        String rf = cmdLine.getOptionValue(RF);

        if ((endPoints == null) || (endPoints.length == 0)) {
            endPoints = new String[] { "127.0.0.1" };
        }

        int replicationFactor;
        try {
            replicationFactor = Integer.parseInt(rf);
        } catch (Exception e) {
            replicationFactor = 1;
        }

        final IRCConnector connector = new IRCConnector(nickname, server, channels);

        Cluster cluster = new Cluster.Builder().addContactPoints(endPoints).build();
        final Session session = cluster.connect();

        CassandraWriter writer = new CassandraWriter(session, replicationFactor);
        connector.setWriter(writer);

        connector.startRecording();

        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                connector.stopRecording();
                session.close();
            }
        }));

    } catch (ParseException pe) {
        System.out.println("Parse Error on command line options:");
        System.out.println(commandLineRepresentation(args));
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("roomstore", options);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:backtype.storm.command.update_topology.java

/**
 * @param args/*from ww w.j a  va 2s .  co m*/
 */
public static void main(String[] args) {
    if (args == null || args.length < 3) {
        System.out.println("Invalid parameter");
        usage();
        return;
    }
    String topologyName = args[0];
    try {
        String[] str2 = Arrays.copyOfRange(args, 1, args.length);
        CommandLineParser parser = new GnuParser();
        Options r = buildGeneralOptions(new Options());
        CommandLine commandLine = parser.parse(r, str2, true);

        String pathConf = null;
        String pathJar = null;
        if (commandLine.hasOption("conf")) {
            pathConf = (commandLine.getOptionValues("conf"))[0];
        }
        if (commandLine.hasOption("jar")) {
            pathJar = (commandLine.getOptionValues("jar"))[0];
        }
        if (pathConf != null || pathJar != null)
            updateTopology(topologyName, pathJar, pathConf);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.hortonworks.streamline.storage.tool.SQLScriptRunner.java

public static void main(String[] args) throws Exception {
    Options options = new Options();

    options.addOption(option(1, "c", OPTION_CONFIG_FILE_PATH, "Config file path"));
    options.addOption(option(Option.UNLIMITED_VALUES, "f", OPTION_SCRIPT_PATH, "Script path to execute"));
    options.addOption(option(Option.UNLIMITED_VALUES, "m", OPTION_MYSQL_JAR_URL_PATH,
            "Mysql client jar url to download"));
    CommandLineParser parser = new BasicParser();
    CommandLine commandLine = parser.parse(options, args);

    if (!commandLine.hasOption(OPTION_CONFIG_FILE_PATH) || !commandLine.hasOption(OPTION_SCRIPT_PATH)
            || commandLine.getOptionValues(OPTION_SCRIPT_PATH).length <= 0) {
        usage(options);/*from   w w w  .  ja v  a2s  .co m*/
        System.exit(1);
    }

    String confFilePath = commandLine.getOptionValue(OPTION_CONFIG_FILE_PATH);
    String[] scripts = commandLine.getOptionValues(OPTION_SCRIPT_PATH);
    String mysqlJarUrl = commandLine.getOptionValue(OPTION_MYSQL_JAR_URL_PATH);

    try {
        Map<String, Object> conf = Utils.readStreamlineConfig(confFilePath);

        StorageProviderConfigurationReader confReader = new StorageProviderConfigurationReader();
        StorageProviderConfiguration storageProperties = confReader.readStorageConfig(conf);

        String bootstrapDirPath = System.getProperty("bootstrap.dir");

        MySqlDriverHelper.downloadMySQLJarIfNeeded(storageProperties, bootstrapDirPath, mysqlJarUrl);

        SQLScriptRunner sqlScriptRunner = new SQLScriptRunner(storageProperties);
        try {
            sqlScriptRunner.initializeDriver();
        } catch (ClassNotFoundException e) {
            System.err.println(
                    "Driver class is not found in classpath. Please ensure that driver is in classpath.");
            System.exit(1);
        }

        for (String script : scripts) {
            sqlScriptRunner.runScriptWithReplaceDBType(script);
        }
    } catch (IOException e) {
        System.err.println("Error occurred while reading config file: " + confFilePath);
        System.exit(1);
    }
}

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

public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption("h", "help", false, "show usage");
    options.addOption(Option.builder().longOpt("descriptor_set")
            .desc("The descriptor set representing the compiled input protos.").hasArg()
            .argName("DESCRIPTOR-SET").required(true).build());
    options.addOption(//from w w w .  j a va 2 s  .  c o m
            Option.builder().longOpt("service_yaml").desc("The service YAML configuration file or files.")
                    .hasArg().argName("SERVICE-YAML").required(true).build());
    options.addOption(Option.builder().longOpt("gapic_yaml").desc("The GAPIC YAML configuration file or files.")
            .hasArg().argName("GAPIC-YAML").required(true).build());
    options.addOption(Option.builder().longOpt("package_yaml")
            .desc("The package metadata YAML configuration file.").hasArg().argName("PACKAGE-YAML").build());
    options.addOption(Option.builder("o").longOpt("output")
            .desc("The directory in which to output the generated client library.").hasArg()
            .argName("OUTPUT-DIRECTORY").build());
    options.addOption(Option.builder().longOpt("enabled_artifacts")
            .desc("Optional. Artifacts enabled for the generator. "
                    + "Currently supports 'surface' and 'test'.")
            .hasArg().argName("ENABLED_ARTIFACTS").required(false).build());

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

    int exitCode = generate(cl.getOptionValue("descriptor_set"), cl.getOptionValues("service_yaml"),
            cl.getOptionValues("gapic_yaml"), cl.getOptionValue("package_yaml"),
            cl.getOptionValue("output", ""), cl.getOptionValues("enabled_artifacts"));
    System.exit(exitCode);
}

From source file:com.commonsware.android.gcm.cmd.GCM.java

@SuppressWarnings("static-access")
public static void main(String[] args) {
    Option helpOpt = new Option("h", "help", false, "print this message");
    Option apiKeyOpt = OptionBuilder.withArgName("key").hasArg().isRequired().withDescription("GCM API key")
            .withLongOpt("apiKey").create('a');
    Option deviceOpt = OptionBuilder.withArgName("regId").hasArg().isRequired()
            .withDescription("device to send to").withLongOpt("device").create('d');
    Option dataOpt = OptionBuilder.withArgName("key=value").hasArgs(2).withDescription("datum to send")
            .withValueSeparator().withLongOpt("data").create('D');

    Options options = new Options();

    options.addOption(apiKeyOpt);//from  www . j  av  a  2 s  .  c  o m
    options.addOption(deviceOpt);
    options.addOption(dataOpt);
    options.addOption(helpOpt);

    CommandLineParser parser = new PosixParser();

    try {
        CommandLine line = parser.parse(options, args);

        if (line.hasOption('h') || !line.hasOption('a') || !line.hasOption('d')) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("gcm", options, true);
        } else {
            sendMessage(line.getOptionValue('a'), Arrays.asList(line.getOptionValues('d')),
                    line.getOptionProperties("data"));
        }
    } catch (org.apache.commons.cli.MissingOptionException moe) {
        System.err.println("Invalid command syntax");
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("gcm", options, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}