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

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

Introduction

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

Prototype

public static OptionBuilder withLongOpt(String newLongopt) 

Source Link

Document

The next Option created will have the following long option value.

Usage

From source file:com.keriati.pqrcode.PQOptions.java

public void parse(String[] args) {
    Options options = new Options();

    options.addOption(//from w  ww .  j  a  va 2s  . c  o  m
            OptionBuilder.withLongOpt("width").withDescription("Output file width in pixels. Default: 250")
                    .hasArgs(1).withArgName("width").create("w"));

    options.addOption(
            OptionBuilder.withLongOpt("height").withDescription("Output file height in pixels. Default: 250")
                    .hasArgs(1).withArgName("height").create("h"));

    options.addOption(OptionBuilder.isRequired().withLongOpt("output")
            .withDescription("Output file name. (required)").hasArgs(1).withArgName("output").create("o"));

    options.addOption(OptionBuilder.isRequired().withLongOpt("data")
            .withDescription("Data to encode. (required)").hasArgs(1).withArgName("data").create("d"));

    options.addOption(OptionBuilder.withLongOpt("format")
            .withDescription("Image format. Supported values: JPEG, PNG, GIF, BMP, WBMP. Default is: PNG")
            .hasArgs(1).withArgName("format").create("f"));

    options.addOption(OptionBuilder.withLongOpt("quietZoneSize")
            .withDescription("White border size arround the QR Code. Default: 4.").hasArgs(1)
            .withArgName("quietZoneSize").create("q"));

    GnuParser parser = new GnuParser();

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

        width = Integer.parseInt(line.getOptionValue("width", "250"));
        height = Integer.parseInt(line.getOptionValue("height", "250"));
        quietZoneSize = Integer.parseInt(line.getOptionValue("quietZoneSize", "4"));
        format = line.getOptionValue("format", "PNG");

        if (line.hasOption("output")) {
            output = line.getOptionValue("output");
        }

        if (line.hasOption("data")) {
            data = line.getOptionValue("data");
        }

    } catch (ParseException exp) {
        System.out.println("ERROR:" + exp.getMessage());
    }

    if (output == null || data == null) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("java -jar PQRCode.jar", options);
        System.exit(0);
    }
}

From source file:jmxsh.ListCmd.java

private ListCmd() {
    this.opts = new Options();

    this.opts.addOption(OptionBuilder.withLongOpt("server").withDescription("Server containing mbean.")
            .withArgName("SERVER").hasArg().create("s"));

    this.opts.addOption(
            OptionBuilder.withLongOpt("help").withDescription("Display usage help.").hasArg(false).create("?"));
}

From source file:com.linkedin.databus.bootstrap.utils.BootstrapAvroRecordDumper.java

@SuppressWarnings("static-access")
public static void parseArgs(String[] args) throws IOException {
    CommandLineParser cliParser = new GnuParser();

    Option outputDirOption = OptionBuilder.withLongOpt(OUTPUT_DIR_OPT_LONG_NAME).withDescription("Help screen")
            .create(OUTPUT_DIR_OPT_CHAR);

    Options options = new Options();
    options.addOption(outputDirOption);/*from w  ww  .  j ava 2  s .co m*/

    CommandLine cmd = null;
    try {
        cmd = cliParser.parse(options, args);
    } catch (ParseException pe) {
        LOG.fatal("Bootstrap Avro Record Dumper: failed to parse command-line options.", pe);
        throw new RuntimeException("Bootstrap Avro Record Dumper: failed to parse command-line options.", pe);
    }

    if (cmd.hasOption(OUTPUT_DIR_OPT_CHAR)) {
        outputDir = cmd.getOptionValue(OUTPUT_DIR_OPT_CHAR);
    }
}

From source file:com.delcyon.capo.Configuration.java

@SuppressWarnings({ "unchecked", "static-access" })
public Configuration(String... programArgs) throws Exception {

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    documentBuilder = documentBuilderFactory.newDocumentBuilder();

    options = new Options();
    // the enum this is a little complicated, but it gives us a nice
    // centralized place to put all of the system parameters
    // and lets us iterate of the list of options and preferences
    PREFERENCE[] preferences = PREFERENCE.values();
    for (PREFERENCE preference : preferences) {
        // not the most elegant, but there is no default constructor, but
        // the has arguments value is always there
        OptionBuilder optionBuilder = OptionBuilder.hasArg(preference.hasArgument);
        if (preference.hasArgument == true) {
            String[] argNames = preference.arguments;
            for (String argName : argNames) {
                optionBuilder = optionBuilder.withArgName(argName);
            }//from  w ww.  j  a  va 2  s .c om
        }

        optionBuilder = optionBuilder.withDescription(preference.getDescription());
        optionBuilder = optionBuilder.withLongOpt(preference.getLongOption());
        options.addOption(optionBuilder.create(preference.getOption()));

        preferenceHashMap.put(preference.toString(), preference);
    }

    //add dynamic options

    Set<String> preferenceProvidersSet = CapoApplication.getAnnotationMap()
            .get(PreferenceProvider.class.getCanonicalName());
    if (preferenceProvidersSet != null) {
        for (String className : preferenceProvidersSet) {
            Class preferenceClass = Class.forName(className).getAnnotation(PreferenceProvider.class)
                    .preferences();
            if (preferenceClass.isEnum()) {
                Object[] enumObjects = preferenceClass.getEnumConstants();
                for (Object enumObject : enumObjects) {

                    Preference preference = (Preference) enumObject;
                    //filter out any preferences that don't belong on this server or client.
                    if (preference.getLocation() != Location.BOTH) {
                        if (CapoApplication.isServer() == true && preference.getLocation() == Location.CLIENT) {
                            continue;
                        } else if (CapoApplication.isServer() == false
                                && preference.getLocation() == Location.SERVER) {
                            continue;
                        }
                    }
                    preferenceHashMap.put(preference.toString(), preference);
                    boolean hasArgument = false;
                    if (preference.getArguments() == null || preference.getArguments().length == 0) {
                        hasArgument = false;
                    } else {
                        hasArgument = true;
                    }

                    OptionBuilder optionBuilder = OptionBuilder.hasArg(hasArgument);
                    if (hasArgument == true) {
                        String[] argNames = preference.getArguments();
                        for (String argName : argNames) {
                            optionBuilder = optionBuilder.withArgName(argName);
                        }
                    }

                    optionBuilder = optionBuilder.withDescription(preference.getDescription());
                    optionBuilder = optionBuilder.withLongOpt(preference.getLongOption());
                    options.addOption(optionBuilder.create(preference.getOption()));
                }

            }
        }
    }

    // create parser
    CommandLineParser commandLineParser = new GnuParser();
    this.commandLine = commandLineParser.parse(options, programArgs);

    Preferences systemPreferences = Preferences
            .systemNodeForPackage(CapoApplication.getApplication().getClass());
    String capoDirString = null;
    while (true) {
        capoDirString = systemPreferences.get(PREFERENCE.CAPO_DIR.longOption, null);
        if (capoDirString == null) {

            systemPreferences.put(PREFERENCE.CAPO_DIR.longOption, PREFERENCE.CAPO_DIR.defaultValue);
            capoDirString = PREFERENCE.CAPO_DIR.defaultValue;
            try {
                systemPreferences.sync();
            } catch (BackingStoreException e) {
                //e.printStackTrace();            
                if (systemPreferences.isUserNode() == false) {
                    System.err.println("Problem with System preferences, trying user's");
                    systemPreferences = Preferences
                            .userNodeForPackage(CapoApplication.getApplication().getClass());
                    continue;
                } else //just bail out
                {
                    throw e;
                }

            }
        }
        break;
    }

    disableAutoSync = hasOption(PREFERENCE.DISABLE_CONFIG_AUTOSYNC);

    File capoDirFile = new File(capoDirString);
    if (capoDirFile.exists() == false) {
        if (disableAutoSync == false) {
            capoDirFile.mkdirs();
        }
    }
    File configDir = new File(capoDirFile, PREFERENCE.CONFIG_DIR.defaultValue);
    if (configDir.exists() == false) {
        if (disableAutoSync == false) {
            configDir.mkdirs();
        }
    }

    if (disableAutoSync == false) {
        capoConfigFile = new File(configDir, CONFIG_FILENAME);
        if (capoConfigFile.exists() == false) {

            Document configDocument = CapoApplication.getDefaultDocument("config.xml");

            FileOutputStream configFileOutputStream = new FileOutputStream(capoConfigFile);
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.transform(new DOMSource(configDocument), new StreamResult(configFileOutputStream));
            configFileOutputStream.close();
        }

        configDocument = documentBuilder.parse(capoConfigFile);
    } else //going memory only, because of disabled auto sync
    {
        configDocument = CapoApplication.getDefaultDocument("config.xml");
    }
    if (configDocument instanceof CDocument) {
        ((CDocument) configDocument).setSilenceEvents(true);
    }
    loadPreferences();
    preferenceValueHashMap.put(PREFERENCE.CAPO_DIR.longOption, capoDirString);
    //print out preferences
    //this also has the effect of persisting all of the default values if a values doesn't already exist
    for (PREFERENCE preference : preferences) {
        if (getValue(preference) != null) {
            CapoApplication.logger.log(Level.CONFIG, preference.longOption + "='" + getValue(preference) + "'");
        }
    }

    CapoApplication.logger.setLevel(Level.parse(getValue(PREFERENCE.LOGGING_LEVEL)));
}

From source file:jmxsh.InvokeCmd.java

/** Initialize private variables.  Setup the command-line options. */
private InvokeCmd() {
    this.opts = new Options();

    this.opts.addOption(OptionBuilder.withLongOpt("server").withDescription("Server containing mbean.")
            .withArgName("SERVER").hasArg().create("s"));

    this.opts.addOption(OptionBuilder.withLongOpt("mbean").withDescription("MBean containing attribute.")
            .withArgName("MBEAN").hasArg().create("m"));

    this.opts.addOption(OptionBuilder.withLongOpt("noconvert")
            .withDescription(/* w  w w. ja v a 2  s .co  m*/
                    "Do not auto-convert the result to a Tcl string, instead create a java object reference.")
            .hasArg(false).create("n"));

    this.opts.addOption(
            OptionBuilder.withLongOpt("help").withDescription("Display usage help.").hasArg(false).create("?"));
}

From source file:com.vectorization.client.Application.java

private static Options createOptions() {
    Options options = new Options();
    options.addOption("D", "database", true, "Database to use.");
    options.addOption("h", "host", true, "Connect to host.");
    OptionBuilder.withLongOpt("help");
    OptionBuilder.withDescription("Print this message");
    Option help = OptionBuilder.create();
    options.addOption(help);/*from w  ww .  j  a  v  a 2  s  . c om*/
    Option passwordOption = new Option("p", "password", true, "Password to use when connecting "
            + "to server.  If password is not " + "given it's asked from the tty.");
    passwordOption.setOptionalArg(true);
    options.addOption(passwordOption);
    options.addOption("P", "port", true, "Port number to use for connection");
    options.addOption("u", "user", true, "User for login if not current user.");
    options.addOption("V", "version", false, "Output version information and exit.");
    return options;
}

From source file:jmxsh.GetCmd.java

private GetCmd() {
    this.opts = new Options();

    this.opts.addOption(OptionBuilder.withLongOpt("server").withDescription("Server containing mbean.")
            .withArgName("SERVER").hasArg().create("s"));

    this.opts.addOption(OptionBuilder.withLongOpt("mbean").withDescription("MBean containing attribute.")
            .withArgName("MBEAN").hasArg().create("m"));

    this.opts.addOption(OptionBuilder.withLongOpt("noconvert")
            .withDescription(/*from   ww  w  .  jav a2s .co m*/
                    "Do not auto-convert the result to a Tcl string, instead create a java object reference.")
            .hasArg(false).create("n"));

    this.opts.addOption(
            OptionBuilder.withLongOpt("help").withDescription("Display usage help.").hasArg(false).create("?"));
}

From source file:com.blackboard.WebdavBulkDeleterClient.java

@SuppressWarnings("static-access")
private static Options addAllOptions(Options options, Map<String, String> optionsAvailable) {
    Iterator<Entry<String, String>> it = optionsAvailable.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, String> pairs = it.next();
        String name = pairs.getKey();
        String description = pairs.getValue();
        options.addOption(OptionBuilder.withLongOpt(name).withDescription(description).hasArg()
                .withArgName(name).create());
    }/*from   w  w w .  j  av  a  2  s  .co  m*/
    return (options);
}

From source file:edu.scripps.fl.pubchem.util.CommandLineHandler.java

public CommandLineHandler(String cmdName) {
    this.cmdName = cmdName;
    options = new Options();
    options.addOption(OptionBuilder.withLongOpt("hib_config").withType("").withValueSeparator('=').hasArg()
            .isRequired().create());//  ww  w.  j a v a2s. com
    options.addOption(
            OptionBuilder.withLongOpt("log_config").withType("").withValueSeparator('=').hasArg().create());
}

From source file:com.rockagen.commons.util.CLITest.java

@Test
@Ignore//  www .  java  2 s .  co m
public void testCLI() {

    // create the Options
    Options options = new Options();
    options.addOption("h", "help", false, "print help for the command.");
    options.addOption("v", "verbose", false, "verbose");
    OptionBuilder.withArgName("property=value");
    OptionBuilder.hasArgs(2);
    OptionBuilder.withLongOpt("desc");
    OptionBuilder.withValueSeparator();
    OptionBuilder.withDescription("use value for given property");
    options.addOption(OptionBuilder.create("D"));

    OptionBuilder.withArgName("file1,file2...");
    OptionBuilder.hasArgs();
    OptionBuilder.withLongOpt("input");
    OptionBuilder.withValueSeparator(' ');
    OptionBuilder.withDescription("file name");
    options.addOption(OptionBuilder.create("i"));

    String formatstr = "CLITest [-h/--help][-v/--verbose]..";
    try {
        String[] args = { "CLITest", "--help", "-v", "-s", "--desc", "name=value", "--input", "file1", "file2",
                "file3" };
        // parse the command line arguments
        CommandLine line = CmdUtil.parse(options, args);

        if (line.hasOption("h")) {
            CmdUtil.printHelp(formatstr, "weclome usa", options,
                    "If you hava some quesion,please mail to agen@rockagen.com");
        }
        if (line.hasOption("v")) {
            System.out.println("VERSION 0.0.1");
        }
        if (line.hasOption("D")) {
            System.out.println("hey,guys,you input " + ArrayUtil.toString(line.getOptionValues("D")));
        }
        if (line.hasOption("i")) {
            System.out.println("hey,guys,you input " + ArrayUtil.toString(line.getOptionValues("i")));
        } else {
            CmdUtil.printHelp(formatstr, options);
        }
    } catch (ParseException exp) {
        CmdUtil.printHelp(formatstr, options);
        System.err.println();
        System.err.println(exp.getMessage());
    }

}