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

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

Introduction

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

Prototype

public String getOptionValue(char opt, String defaultValue) 

Source Link

Document

Retrieve the argument, if any, of an option.

Usage

From source file:example.HelloConsole.java

public static void main(String[] args) throws Exception {
    Option msg = Option.builder("m").longOpt("message").hasArg().desc("the message to capitalize").build();
    Options options = new Options();
    options.addOption(msg);/*from   w w w . jav  a 2  s  .com*/

    CommandLineParser parser = new DefaultParser();
    CommandLine line = parser.parse(options, args);

    String message = line.getOptionValue("m", "Hello Ivy!");
    System.out.println("standard message : " + message);
    System.out.println(
            "capitalized by " + WordUtils.class.getName() + " : " + WordUtils.capitalizeFully(message));
}

From source file:ant_ivy.Hello.java

public static void main(String[] args) throws Exception {
    Option msg = OptionBuilder.withArgName("msg").hasArg().withDescription("the message to capitalize")
            .create("message");
    Options options = new Options();
    options.addOption(msg);//  w w  w  .  j ava2  s  .  co m

    CommandLineParser parser = new GnuParser();
    CommandLine line = parser.parse(options, args);

    String message = line.getOptionValue("message", "hello ivy !");
    System.out.println("standard message : " + message);
    System.out.println(
            "capitalized by " + WordUtils.class.getName() + " : " + WordUtils.capitalizeFully(message));
}

From source file:list.Main.java

public static void main(String[] args) throws Exception {
    Options options = getOptions();//www . j  a  v a  2 s  .co m
    try {

        CommandLineParser parser = new GnuParser();

        CommandLine line = parser.parse(options, args);
        File dir = new File(line.getOptionValue("dir", "."));
        Collection files = ListFile.list(dir);
        System.out.println("listing files in " + dir);
        for (Iterator it = files.iterator(); it.hasNext();) {
            System.out.println("\t" + it.next() + "\n");
        }
    } catch (ParseException exp) {
        // oops, something went wrong
        System.err.println("Parsing failed.  Reason: " + exp.getMessage());

        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("list", options);
    }
}

From source file:find.Main.java

public static void main(String[] args) throws Exception {
    Options options = getOptions();//w ww . ja v  a  2s .  c o  m
    try {

        CommandLineParser parser = new GnuParser();

        CommandLine line = parser.parse(options, args);
        File dir = new File(line.getOptionValue("dir", "."));
        String name = line.getOptionValue("name", "jar");
        Collection files = FindFile.find(dir, name);
        System.out.println("listing files in " + dir + " containing " + name);
        for (Iterator it = files.iterator(); it.hasNext();) {
            System.out.println("\t" + it.next() + "\n");
        }
    } catch (ParseException exp) {
        // oops, something went wrong
        System.err.println("Parsing failed.  Reason: " + exp.getMessage());

        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("find", options);
    }
}

From source file:com.nextdoor.bender.CreateSchema.java

public static void main(String[] args) throws ParseException, InterruptedException, IOException {

    /*/*from  w ww .  jav a2 s. co  m*/
     * Parse cli arguments
     */
    Options options = new Options();
    options.addOption(Option.builder().longOpt("out-file").hasArg()
            .desc("Filename to output schema to. Default: schema.json").build());
    options.addOption(Option.builder().longOpt("docson").hasArg(false)
            .desc("Create a schema that is able to be read by docson").build());
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = parser.parse(options, args);

    String filename = cmd.getOptionValue("out-file", "schema.json");

    /*
     * Write schema
     */
    BenderSchema schema = new BenderSchema();
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);

    JsonNode node = schema.getSchema();

    if (cmd.hasOption("docson")) {
        modifyNode(node);
    }

    mapper.writeValue(new File(filename), node);
}

From source file:com.dopsun.msg4j.tools.CodeGen.java

/**
 * @param args/*from w w w . j a v  a  2 s  . com*/
 * @throws IOException
 * @throws MalformedURLException
 * @throws ParseException
 */
public static void main(String[] args) throws MalformedURLException, IOException, ParseException {
    Options options = new Options();
    options.addOption("o", "out", true, "Output file");
    options.addOption("s", "src", true, "Source file");
    options.addOption("l", "lang", true, "Language");

    CommandLineParser cmdParser = new DefaultParser();
    CommandLine cmd = cmdParser.parse(options, args);

    String lang = cmd.getOptionValue("lang", "Java");
    String srcFile = cmd.getOptionValue("src");
    String outFile = cmd.getOptionValue("out");

    YamlModelSource modelSource = YamlModelSource.load(new File(srcFile).toURI().toURL());
    YamlModelParser parser = YamlModelParser.create();
    ModelInfo modelInfo = parser.parse(modelSource);

    String codeText = null;
    if (lang.equals("Java")) {
        codeText = Generator.JAVA.generate(modelInfo);
    } else {
        throw new UnsupportedOperationException("Unrecognized lang: " + lang);
    }

    if (outFile != null) {
        File file = new File(outFile);
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();

        Files.append(codeText, new File(outFile), Charsets.UTF_8);
    } else {
        System.out.println(codeText);
    }
}

From source file:com.nextdoor.bender.ValidateSchema.java

public static void main(String[] args) throws ParseException, InterruptedException, IOException {

    /*//w  ww  . ja  va 2 s  .  c  om
     * Parse cli arguments
     */
    Options options = new Options();
    options.addOption(Option.builder().longOpt("schema").hasArg()
            .desc("Filename to output schema to. Default: schema.json").build());
    options.addOption(Option.builder().longOpt("configs").hasArgs()
            .desc("List of config files to validate against schema.").build());
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = parser.parse(options, args);

    String schemaFilename = cmd.getOptionValue("schema", "schema.json");
    String[] configFilenames = cmd.getOptionValues("configs");

    /*
     * Validate config files against schema
     */
    boolean hasFailures = false;
    for (String configFilename : configFilenames) {
        StringBuilder sb = new StringBuilder();
        Files.lines(Paths.get(configFilename), StandardCharsets.UTF_8).forEach(p -> sb.append(p + "\n"));

        System.out.println("Attempting to validate " + configFilename);
        try {
            ObjectMapper mapper = BenderConfig.getObjectMapper(configFilename);
            mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
            BenderConfig.load(configFilename, sb.toString(), mapper, true);
            System.out.println("Valid");
            BenderConfig config = BenderConfig.load(configFilename, sb.toString());
        } catch (ConfigurationException e) {
            System.out.println("Invalid");
            e.printStackTrace();
            hasFailures = true;
        }
    }

    if (hasFailures) {
        System.exit(1);
    }
}

From source file:com.aerospike.example.cache.AsACache.java

public static void main(String[] args) throws AerospikeException {
    try {//from www  .  j  a v  a2 s. co m
        Options options = new Options();
        options.addOption("h", "host", true, "Server hostname (default: 127.0.0.1)");
        options.addOption("p", "port", true, "Server port (default: 3000)");
        options.addOption("n", "namespace", true, "Namespace (default: test)");
        options.addOption("s", "set", true, "Set (default: demo)");
        options.addOption("u", "usage", false, "Print usage.");

        CommandLineParser parser = new PosixParser();
        CommandLine cl = parser.parse(options, args, false);

        String host = cl.getOptionValue("h", "127.0.0.1");
        String portString = cl.getOptionValue("p", "3000");
        int port = Integer.parseInt(portString);
        String namespace = cl.getOptionValue("n", "test");
        String set = cl.getOptionValue("s", "demo");
        log.debug("Host: " + host);
        log.debug("Port: " + port);
        log.debug("Namespace: " + namespace);
        log.debug("Set: " + set);

        @SuppressWarnings("unchecked")
        List<String> cmds = cl.getArgList();
        if (cmds.size() == 0 && cl.hasOption("u")) {
            logUsage(options);
            return;
        }

        AsACache as = new AsACache(host, port, namespace, set);

        as.work();

    } catch (Exception e) {
        log.error("Critical error", e);
    }
}

From source file:jetbrains.exodus.console.Console.java

public static void main(String[] args) throws IOException, ParseException {
    CommandLine line = getCommandLine(args);

    Long port = (Long) line.getParsedOptionValue("l");
    port = port == null ? 2222 : port;// ww  w  .  j  av a  2s .  c o m
    String password = line.getOptionValue('p', null);

    Map<String, Object> config = new HashMap<>();
    config.put("location", line.getOptionValue('x', null));

    new RhinoServer(port.intValue(), password, config);
}

From source file:com.aerospike.client.rest.AerospikeRESTfulService.java

public static void main(String[] args) throws ParseException {

    Options options = new Options();
    options.addOption("h", "host", true, "Server hostname (default: localhost)");
    options.addOption("p", "port", true, "Server port (default: 3000)");

    // parse the command line args
    CommandLineParser parser = new PosixParser();
    CommandLine cl = parser.parse(options, args, false);

    // set properties
    Properties as = System.getProperties();
    String host = cl.getOptionValue("h", "localhost");
    as.put("seedHost", host);
    String portString = cl.getOptionValue("p", "3000");
    as.put("port", portString);

    // start app//from   ww  w.jav a2  s. co  m
    SpringApplication.run(AerospikeRESTfulService.class, args);

}