Example usage for org.apache.commons.cli Options getOptions

List of usage examples for org.apache.commons.cli Options getOptions

Introduction

In this page you can find the example usage for org.apache.commons.cli Options getOptions.

Prototype

public Collection getOptions() 

Source Link

Document

Retrieve a read-only list of options in this set

Usage

From source file:org.apache.accumulo.core.util.shell.commands.SetScanIterCommand.java

@Override
public Options getOptions() {
    // Remove the options that specify which type of iterator this is, since
    // they are all scan iterators with this command.
    final HashSet<OptionGroup> groups = new HashSet<OptionGroup>();
    final Options parentOptions = super.getOptions();
    final Options modifiedOptions = new Options();
    for (Iterator<?> it = parentOptions.getOptions().iterator(); it.hasNext();) {
        Option o = (Option) it.next();
        if (!IteratorScope.majc.name().equals(o.getOpt()) && !IteratorScope.minc.name().equals(o.getOpt())
                && !IteratorScope.scan.name().equals(o.getOpt())) {
            modifiedOptions.addOption(o);
            OptionGroup group = parentOptions.getOptionGroup(o);
            if (group != null)
                groups.add(group);/*from w ww. j a v a  2 s .  c  o  m*/
        }
    }
    for (OptionGroup group : groups) {
        modifiedOptions.addOptionGroup(group);
    }
    return modifiedOptions;
}

From source file:org.apache.accumulo.core.util.shell.commands.SetShellIterCommand.java

@Override
public Options getOptions() {
    // Remove the options that specify which type of iterator this is, since
    // they are all scan iterators with this command.
    final HashSet<OptionGroup> groups = new HashSet<OptionGroup>();
    final Options parentOptions = super.getOptions();
    final Options modifiedOptions = new Options();
    for (Iterator<?> it = parentOptions.getOptions().iterator(); it.hasNext();) {
        Option o = (Option) it.next();
        if (!IteratorScope.majc.name().equals(o.getOpt()) && !IteratorScope.minc.name().equals(o.getOpt())
                && !IteratorScope.scan.name().equals(o.getOpt()) && !"table".equals(o.getLongOpt())) {
            modifiedOptions.addOption(o);
            OptionGroup group = parentOptions.getOptionGroup(o);
            if (group != null)
                groups.add(group);/*from ww  w . ja  va  2  s .  c o m*/
        }
    }
    for (OptionGroup group : groups) {
        modifiedOptions.addOptionGroup(group);
    }

    profileOpt = new Option("pn", "profile", true, "iterator profile name");
    profileOpt.setRequired(true);
    profileOpt.setArgName("profile");

    modifiedOptions.addOption(profileOpt);

    return modifiedOptions;
}

From source file:org.apache.geronimo.cli.PrintHelper.java

public void printUsage(PrintWriter pw, int width, String app, Options options) {
    // create a list for processed option groups
    ArrayList list = new ArrayList();

    StringBuilder optionsBuff = new StringBuilder();

    // temp variable
    Option option;/*  w ww  .  j a  v a2  s .  c o m*/

    // iterate over the options
    for (Iterator i = options.getOptions().iterator(); i.hasNext();) {
        // get the next Option
        option = (Option) i.next();

        // check if the option is part of an OptionGroup
        OptionGroup group = options.getOptionGroup(option);

        // if the option is part of a group and the group has not already
        // been processed
        if (group != null && !list.contains(group)) {

            // add the group to the processed list
            list.add(group);

            // get the names of the options from the OptionGroup
            Collection names = group.getNames();

            optionsBuff.append("[");

            // for each option in the OptionGroup
            for (Iterator iter = names.iterator(); iter.hasNext();) {
                optionsBuff.append(iter.next());
                if (iter.hasNext()) {
                    optionsBuff.append("|");
                }
            }
            optionsBuff.append("] ");
        } else if (group == null) {
            // if the Option is not part of an OptionGroup
            // if the Option is not a required option
            if (!option.isRequired()) {
                optionsBuff.append("[");
            }

            if (!" ".equals(option.getOpt())) {
                optionsBuff.append("-").append(option.getOpt());
            } else {
                optionsBuff.append("--").append(option.getLongOpt());
            }

            if (option.hasArg()) {
                optionsBuff.append(" ");
            }

            // if the Option has a value
            if (option.hasArg()) {
                optionsBuff.append(option.getArgName());
            }

            // if the Option is not a required option
            if (!option.isRequired()) {
                optionsBuff.append("]");
            }
            optionsBuff.append(" ");
        }
    }

    app = app.replace("$options", optionsBuff.toString());

    // call printWrapped
    printWrapped(pw, width, app.indexOf(' ') + 1, app);
}

From source file:org.apache.geronimo.cli.PrintHelper.java

protected StringBuilder renderOptions(StringBuilder sb, int width, Options options, int leftPad, int descPad,
        boolean displayDesc) {
    final String lpad = createPadding(leftPad);
    final String dpad = createPadding(descPad);

    //first create list containing only <lpad>-a,--aaa where -a is opt and --aaa is
    //long opt; in parallel look for the longest opt string
    //this list will be then used to sort options ascending
    int max = 0;//w  ww  .  j av a2  s.c  o  m
    StringBuilder optBuf;
    List prefixList = new ArrayList();
    Option option;
    List optList = new ArrayList(options.getOptions());
    Collections.sort(optList, new StringBuilderComparator());
    for (Iterator i = optList.iterator(); i.hasNext();) {
        option = (Option) i.next();
        optBuf = new StringBuilder(8);

        if (option.getOpt().equals(" ")) {
            optBuf.append(lpad).append("   " + defaultLongOptPrefix).append(option.getLongOpt());
        } else {
            optBuf.append(lpad).append(defaultOptPrefix).append(option.getOpt());
            if (option.hasLongOpt()) {
                optBuf.append(',').append(defaultLongOptPrefix).append(option.getLongOpt());
            }

        }

        if (option.hasArg()) {
            if (option.hasArgName()) {
                optBuf.append(" <").append(option.getArgName()).append('>');
            } else {
                optBuf.append(' ');
            }
        }

        prefixList.add(optBuf);
        max = optBuf.length() > max ? optBuf.length() : max;
    }
    int x = 0;
    for (Iterator i = optList.iterator(); i.hasNext();) {
        option = (Option) i.next();
        optBuf = new StringBuilder(prefixList.get(x++).toString());

        if (optBuf.length() < max) {
            optBuf.append(createPadding(max - optBuf.length()));
        }
        optBuf.append(dpad);

        if (displayDesc) {
            optBuf.append(option.getDescription());
        }
        int nextLineTabStop = max + descPad;
        renderWrappedText(sb, width, nextLineTabStop, optBuf.toString());
        if (i.hasNext()) {
            sb.append(defaultNewLine);
            if (displayDesc) {
                sb.append(defaultNewLine);
            }
        }
    }

    return sb;
}

From source file:org.apache.hadoop.hbase.regionserver.DataBlockEncodingTool.java

private static void printUsage(Options options) {
    System.err.println("Usage:");
    System.err.println(String.format("./hbase %s <options>", DataBlockEncodingTool.class.getName()));
    System.err.println("Options:");
    for (Object it : options.getOptions()) {
        Option opt = (Option) it;
        if (opt.hasArg()) {
            System.err//from  w w  w  .  java2 s.  c  o  m
                    .println(String.format("-%s %s: %s", opt.getOpt(), opt.getArgName(), opt.getDescription()));
        } else {
            System.err.println(String.format("-%s: %s", opt.getOpt(), opt.getDescription()));
        }
    }
}

From source file:org.apache.hadoop.yarn.client.cli.ApplicationCLI.java

@SuppressWarnings("unchecked")
private boolean hasAnyOtherCLIOptions(CommandLine cliParser, Options opts, String excludeOption) {
    Collection<Option> ops = opts.getOptions();
    for (Option op : ops) {
        // Skip exclude option from the option list
        if (op.getOpt().equals(excludeOption)) {
            continue;
        }/*  w  w w.j  a  v  a 2  s.  co m*/
        if (cliParser.hasOption(op.getOpt())) {
            return true;
        }
    }
    return false;
}

From source file:org.apache.oozie.cli.CLIParser.java

/**
 * Print the help for the parser to standard output.
 * /* www. jav  a  2 s.  c  om*/
 * @param commandLine the command line
 */
public void showHelp(CommandLine commandLine) {
    PrintWriter pw = new PrintWriter(System.out);
    pw.println("usage: ");
    for (String s : cliHelp) {
        pw.println(LEFT_PADDING + s);
    }
    pw.println();
    HelpFormatter formatter = new HelpFormatter();
    Set<String> commandsToPrint = commands.keySet();
    String[] args = commandLine.getArgs();
    if (args.length > 0 && commandsToPrint.contains(args[0])) {
        commandsToPrint = new HashSet<String>();
        commandsToPrint.add(args[0]);
    }
    for (String comm : commandsToPrint) {
        Options opts = commands.get(comm);
        String s = LEFT_PADDING + cliName + " " + comm + " ";
        if (opts.getOptions().size() > 0) {
            pw.println(s + "<OPTIONS> " + commandsHelp.get(comm));
            formatter.printOptions(pw, 100, opts, s.length(), 3);
        } else {
            pw.println(s + commandsHelp.get(comm));
        }
        pw.println();
    }
    pw.flush();
}

From source file:org.apache.openmeetings.cli.OmHelpFormatter.java

private LinkedHashMap<String, List<OmOption>> getOptions(Options opts, int leftPad) {
    final String longOptSeparator = " ";
    final String lpad = createPadding(leftPad);
    final String lpadParam = createPadding(leftPad + 2);
    List<OmOption> reqOptions = getReqOptions(opts);
    LinkedHashMap<String, List<OmOption>> map = new LinkedHashMap<String, List<OmOption>>(reqOptions.size());
    map.put(GENERAL_OPTION_GROUP, new ArrayList<OmOption>());
    for (OmOption o : reqOptions) {
        map.put(o.getOpt(), new ArrayList<OmOption>());
    }//ww  w.  j  a  v a2 s .co m
    for (Option _o : opts.getOptions()) {
        OmOption o = (OmOption) _o;
        //TODO need better check (required option should go first and should not be duplicated
        boolean skipOption = map.containsKey(o.getOpt());
        boolean mainOption = skipOption || o.getGroup() == null;

        // first create list containing only <lpad>-a,--aaa where
        // -a is opt and --aaa is long opt; in parallel look for
        // the longest opt string this list will be then used to
        // sort options ascending
        StringBuilder optBuf = new StringBuilder();
        if (o.getOpt() == null) {
            optBuf.append(mainOption ? lpad : lpadParam).append("   ").append(getLongOptPrefix())
                    .append(o.getLongOpt());
        } else {
            optBuf.append(mainOption ? lpad : lpadParam).append(getOptPrefix()).append(o.getOpt());

            if (o.hasLongOpt()) {
                optBuf.append(',').append(getLongOptPrefix()).append(o.getLongOpt());
            }
        }

        if (o.hasArg()) {
            String argName = o.getArgName();
            if (argName != null && argName.length() == 0) {
                // if the option has a blank argname
                optBuf.append(' ');
            } else {
                optBuf.append(o.hasLongOpt() ? longOptSeparator : " ");
                optBuf.append("<").append(argName != null ? o.getArgName() : getArgName()).append(">");
            }
        }

        o.setHelpPrefix(optBuf);
        maxPrefixLength = Math.max(optBuf.length(), maxPrefixLength);

        if (skipOption) {
            //TODO need better check (required option should go first and should not be duplicated
            continue;
        }
        String grp = o.getGroup();
        grp = grp == null ? GENERAL_OPTION_GROUP : grp;
        String[] grps = grp.split(",");
        for (String g : grps) {
            map.get(g).add(o);
        }
    }
    for (Map.Entry<String, List<OmOption>> me : map.entrySet()) {
        final String key = me.getKey();
        List<OmOption> options = me.getValue();
        Collections.sort(options, new Comparator<OmOption>() {
            @Override
            public int compare(OmOption o1, OmOption o2) {
                boolean o1opt = !o1.isOptional(key);
                boolean o2opt = !o2.isOptional(key);
                return (o1opt && o2opt || !o1opt && !o2opt) ? (o1.getOpt() == null ? 1 : -1) : (o1opt ? -1 : 1);
            }

        });
        if (opts.hasOption(key)) {
            options.add(0, (OmOption) opts.getOption(key));
        }
    }
    return map;
}

From source file:org.apache.parquet.tools.Main.java

public static void mergeOptionsInto(Options opt, Options opts) {
    if (opts == null) {
        return;//from   w  w  w  .  j  ava  2 s .  com
    }

    Collection<Option> all = opts.getOptions();
    if (all != null && !all.isEmpty()) {
        for (Option o : all) {
            opt.addOption(o);
        }
    }
}

From source file:org.apache.stratos.adc.mgt.cli.completer.CommandCompleter.java

public CommandCompleter(Map<String, Command<StratosCommandContext>> commands) {
    if (logger.isDebugEnabled()) {
        logger.debug("Creating auto complete for {} commands", commands.size());
    }/* w w  w .  jav  a  2s  .  c  o m*/
    argumentMap = new HashMap<String, Collection<String>>();
    defaultCommandCompleter = new StringsCompleter(commands.keySet());
    helpCommandCompleter = new ArgumentCompleter(new StringsCompleter(CliConstants.HELP_ACTION),
            defaultCommandCompleter);
    for (String action : commands.keySet()) {
        Command<StratosCommandContext> command = commands.get(action);
        Options commandOptions = command.getOptions();
        if (commandOptions != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Creating argument completer for command: {}", action);
            }
            List<String> arguments = new ArrayList<String>();
            Collection<?> allOptions = commandOptions.getOptions();
            for (Object o : allOptions) {
                Option option = (Option) o;
                String longOpt = option.getLongOpt();
                String opt = option.getOpt();
                if (StringUtils.isNotBlank(longOpt)) {
                    arguments.add("--" + longOpt);
                } else if (StringUtils.isNotBlank(opt)) {
                    arguments.add("-" + opt);
                }
            }

            argumentMap.put(action, arguments);
        }
    }
}