Example usage for org.apache.commons.cli Option getOpt

List of usage examples for org.apache.commons.cli Option getOpt

Introduction

In this page you can find the example usage for org.apache.commons.cli Option getOpt.

Prototype

public String getOpt() 

Source Link

Document

Retrieve the name of this Option.

Usage

From source file:com.ericsson.eiffel.remrem.generate.cli.CLIOptions.java

public static void checkRequiredOptions() throws MissingOptionException {
    OptionGroup[] groups = { typeGroup, contentGroup };
    for (OptionGroup group : groups) {
        ArrayList<Option> groupOptions = new ArrayList<Option>(group.getOptions());
        boolean groupIsGiven = false;
        for (Option option : groupOptions) {
            if (commandLine.hasOption(option.getOpt())) {
                groupIsGiven = true;/*  w  ww.  ja  va  2 s .  c  o m*/
                break;
            }
        }
        if (!groupIsGiven) {
            throw new MissingOptionException(groupOptions);
        }
    }
}

From source file:dk.hippogrif.prettyxml.app.Main.java

private static void optionUsage(PrintStream ps) {
    for (Iterator iter = optionList.iterator(); iter.hasNext();) {
        Option option = (Option) iter.next();
        ps.print("  -" + option.getOpt());
        String spaces = "              ";
        int i = 0;
        if (option.hasArg()) {
            ps.print(" " + option.getArgName());
            i = option.getArgName().length() + 1;
        }/*from   w  w  w  . j  a  v a2 s. co  m*/
        ps.print(spaces.substring(i));
        ps.println(option.getDescription());
    }
}

From source file:edu.umass.cs.gnsserver.utils.ParametersAndOptions.java

/**
 * Returns a hash map with all options including options in config file and the command line arguments
 *
 * @param className/*from  w ww. j  a  v a  2  s  .  co  m*/
 * @param commandLineOptions
 * @param args command line arguments given to JVM
 * @return hash map with KEY = parameter names, VALUE = values of parameters in String form
 * @throws IOException
 */
public static HashMap<String, String> getParametersAsHashMap(String className, Options commandLineOptions,
        String... args) throws IOException {
    CommandLine parser = null;

    try {
        parser = new GnuParser().parse(commandLineOptions, args);
    } catch (ParseException e) {
        System.err.println("Problem parsing command line:" + e);
        printUsage(className, commandLineOptions);
        System.exit(1);
    }

    if (parser.hasOption(HELP)) {
        printUsage(className, commandLineOptions);
        System.exit(0);
    }

    // load options given in config file in a java properties object
    Properties prop = new Properties();

    if (parser.hasOption(CONFIG_FILE)) {
        String value = parser.getOptionValue(CONFIG_FILE);
        File f = new File(value);
        if (f.exists() == false) {
            System.err.println("Config file not found:" + value);
            System.exit(2);
        }
        InputStream input = new FileInputStream(value);
        // load a properties file
        prop.load(input);

    }

    // create a hash map with all options including options in config file and the command line arguments
    HashMap<String, String> allValues = new HashMap<String, String>();

    // add options given in config file to hash map
    for (String propertyName : prop.stringPropertyNames()) {
        allValues.put(propertyName, prop.getProperty(propertyName));
    }

    // add options given via command line to hashmap. these options can override options given in config file.
    for (Option option : parser.getOptions()) {
        String argName = option.getOpt();
        String value = option.getValue();
        // if an option has a boolean value, the command line arguments do not say true/false for some of these options
        // if option name is given as argument on the command line, it means the value is true. therefore, the hashmap
        // will also assign the value true for these options.
        if (value == null) {
            value = "true";
        }
        allValues.put(argName, value);
    }

    return allValues;
}

From source file:com.aliyun.openservices.odps.console.resource.CreateResourceCommand.java

public static AddResourceCommand parse(String commandString, ExecutionContext sessionContext)
        throws ODPSConsoleException {

    String[] tokens = new AntlrObject(commandString).getTokenStringArray();

    if (tokens != null && tokens.length >= 2 && tokens[0].toUpperCase().equals("CREATE")
            && tokens[1].toUpperCase().equals("RESOURCE")) {

        GnuParser parser = new GnuParser();
        Options options = new Options();
        options.addOption("p", "project", true, null);
        options.addOption("c", "comment", true, null);
        options.addOption("f", "force", false, null);

        try {//from  w w w  .jav a  2  s.c  o  m
            CommandLine cl = parser.parse(options, tokens);

            String refName = null;
            String alias = "";
            String comment = null;
            String type = null;
            String partitionSpec = "";
            boolean isUpdate = false;

            List<String> argList = cl.getArgList();
            int size = argList.size();

            if (size < 4) {
                throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "Missing parameters");
            }

            ListIterator<String> iter = argList.listIterator();
            iter.next();
            iter.next();
            type = iter.next();
            refName = iter.next();
            if (iter.hasNext()) {
                String item = iter.next();
                if (item.equals("(")) {
                    boolean isParenPaired = false;

                    while (iter.hasNext()) {
                        String s = iter.next();
                        if (s.equals(")")) {
                            isParenPaired = true;
                            break;
                        }
                        partitionSpec += s;
                    }

                    if (!isParenPaired) {
                        throw new ODPSConsoleException(
                                ODPSConsoleConstants.BAD_COMMAND + "Unpaired parenthesis");
                    }

                    if (!iter.hasNext()) {
                        throw new ODPSConsoleException(
                                ODPSConsoleConstants.BAD_COMMAND + "Missing parameter: alias");
                    }
                    item = iter.next();
                }

                alias = item;
            }

            if (iter.hasNext()) {
                throw new ODPSConsoleException(
                        ODPSConsoleConstants.BAD_COMMAND + "Illegal parameter: " + iter.next());
            }

            String projectName = null;
            Option[] opts = cl.getOptions();
            for (Option opt : opts) {
                if ("f".equals(opt.getOpt())) {
                    isUpdate = true;
                } else if ("c".equals(opt.getOpt())) {
                    comment = opt.getValue();
                } else if ("p".equals(opt.getOpt())) {
                    projectName = opt.getValue();
                } else {
                    throw new ODPSConsoleException(
                            ODPSConsoleConstants.BAD_COMMAND + "Illegal option: " + opt.getOpt());
                }
            }

            return new AddResourceCommand(commandString, sessionContext, refName, alias, comment, type,
                    partitionSpec, isUpdate, projectName);
        } catch (ParseException e) {
            throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "Invalid parameters");
        }
    } else {
        return null;
    }
}

From source file:de.oth.keycloak.util.CheckParams.java

private static void printCommandLineHelp(Options options) {
    HelpFormatter helpFormatter = new HelpFormatter();
    helpFormatter.setOptionComparator(new Comparator<Option>() {
        public int compare(Option option1, Option option2) {
            if (option1.isRequired() == option2.isRequired()) {
                if (option1.getOpt() != null && option2.getOpt() != null) {
                    return option1.getOpt().compareTo(option2.getOpt());
                } else if (option1.getLongOpt() != null && option2.getLongOpt() != null) {
                    return option1.getLongOpt().compareTo(option2.getLongOpt());
                } else {
                    return -1;
                }/*from www .  ja va  2 s .  com*/
            } else {
                return (option1.isRequired() ? -1 : 1);
            }
        }
    });
    helpFormatter.setWidth(256);
    helpFormatter.printHelp("java " + InitKeycloakServer.class.getName(), options, true);
}

From source file:com.datastax.brisk.demo.pricer.Pricer.java

/**
 * Printing out help message/*from  ww w  .ja v a2s.c  o m*/
 */
public static void printHelpMessage() {
    System.out.println("Usage: ./bin/pricer [options]\n\nOptions:");

    for (Object o : Session.availableOptions.getOptions()) {
        Option option = (Option) o;
        String upperCaseName = option.getLongOpt().toUpperCase();
        System.out.println(String.format("-%s%s, --%s%s%n\t\t%s%n", option.getOpt(),
                (option.hasArg()) ? " " + upperCaseName : "", option.getLongOpt(),
                (option.hasArg()) ? "=" + upperCaseName : "", option.getDescription()));
    }
}

From source file:com.nec.congenio.ConfigCli.java

private static void def(Option.Builder builder, String description) {
    Option opt = builder.desc(description).build();
    OPTS.put(opt.getOpt(), opt);
}

From source file:com.leshazlewood.scms.cli.Main.java

private static void printHelp(Options options, Exception e, boolean debug) {
    HelpFormatter help = new HelpFormatter();
    help.setWidth(80);/*from   ww  w . j  a  va 2 s  .  c o  m*/
    String command = "scms [options] [src_dir] dest_dir";
    String header = "Injests content files in [src dir] and renders a static website into dest_dir.\n\n"
            + "  [src_dir] is optional and defaults to the current working directory.\n"
            + "  dest_dir is required and cannot be the same as src_dir.";
    /*String footer = "\n" +
        "Injests source content files and page templates in [src dir] and renders a\n" +
        "renders a static website into destination_directory.\n\n" +
        "If unspecified, [source directory] defaults to the current working\n" +
        "directory.  destination_directory is required and cannot be the same\n" +
        "as the source directory.";*/

    printException(e, debug);

    System.out.println();

    System.out.println("Usage:");
    System.out.print("  ");
    System.out.println(command);
    System.out.println();
    System.out.println("Description:");
    System.out.print("  ");
    System.out.println(header);
    System.out.println();
    System.out.println("Options:");

    StringBuilder sb = new StringBuilder();

    int columnWidth = calculateColumnWidth(options);

    for (Object o : options.getOptions()) {
        Option option = (Option) o;
        StringBuilder csb = new StringBuilder("  ");
        csb.append("-").append(option.getOpt()).append(",--").append(option.getLongOpt());
        if (option.hasArg()) {
            csb.append(" <arg>");
        }
        int csbLength = csb.length();
        for (int i = 0; i < (columnWidth - csbLength); i++) {
            csb.append(" ");
        }
        sb.append(csb.toString()).append("   ").append(option.getDescription()).append("\n");
    }
    System.out.println(sb);

    //help.printHelp("", "", options, null);
    //System.out.println(footer);
}

From source file:co.cask.cdap.cli.CLIMain.java

private static boolean parseBooleanOption(CommandLine command, Option option, boolean defaultValue) {
    String value = command.getOptionValue(option.getOpt(), Boolean.toString(defaultValue));
    return "true".equals(value);
}

From source file:name.livitski.databag.cli.Syntax.java

protected static String formatOptionHeader(Option option) {
    String title;/*  w w w  .  ja  v  a 2  s  . c o  m*/
    if (null == option.getOpt())
        title = LONG_OPT_PREFIX + option.getLongOpt();
    else
        title = OPT_PREFIX + option.getOpt()
                + (option.hasLongOpt() ? ", " + LONG_OPT_PREFIX + option.getLongOpt() : "");
    return title;
}