List of usage examples for org.apache.commons.cli Option hasLongOpt
public boolean hasLongOpt()
From source file:com.leshazlewood.scms.cli.Main.java
private static int calculateColumnWidth(Options options) { int max = 0;//w w w. j a va 2 s. com for (Object o : options.getOptions()) { Option opt = (Option) o; int columnWidth = "-".length() + opt.getOpt().length(); if (opt.hasLongOpt()) { columnWidth += ",--".length(); columnWidth += opt.getLongOpt().length(); } if (opt.hasArg()) { columnWidth += " <arg>".length(); } columnWidth += 3; //buffer between description max = Math.max(max, columnWidth); } return max; }
From source file:name.livitski.databag.cli.Syntax.java
protected static String getOptionId(Option option) { return option.hasLongOpt() ? LONG_OPT_PREFIX + option.getLongOpt() : OPT_PREFIX + option.getOpt(); }
From source file:name.livitski.databag.cli.Syntax.java
protected static String formatOptionHeader(Option option) { String title;//from w w w .j a v a2 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; }
From source file:com.github.horrorho.liquiddonkey.settings.commandline.CommandLineOptions.java
public String opt(Option option) { return option.hasLongOpt() ? option.getLongOpt() : option.getOpt(); }
From source file:com.netflix.exhibitor.standalone.ExhibitorCLI.java
private void logOptions(String sectionName, String prefix, Options options) { if (sectionName != null) { log.info("== " + sectionName + " =="); }//from w w w . ja v a 2s .c om //noinspection unchecked for (Option option : (Iterable<? extends Option>) options.getOptions()) { if (option.hasLongOpt()) { if (option.hasArg()) { log.info(prefix + option.getLongOpt() + " <arg> - " + option.getDescription()); } else { log.info(prefix + option.getLongOpt() + " - " + option.getDescription()); } } } }
From source file:kieker.tools.util.CLIHelpFormatter.java
@SuppressWarnings("unchecked") @Override/*from w ww. j av a 2 s. c o m*/ protected StringBuffer renderOptions(final StringBuffer sb, final int width, final Options options, final int leftPad, final int descPad) { final String lpad = this.createPadding(leftPad); final String dpad = this.createPadding(8); // we use a fixed value instead of descPad StringBuilder optBuf; final List<Option> optList = new ArrayList<Option>(options.getOptions()); Collections.sort(optList, this.getOptionComparator()); for (final Iterator<Option> i = optList.iterator(); i.hasNext();) { final Option option = i.next(); optBuf = new StringBuilder(8); if (option.getOpt() == null) { optBuf.append(lpad).append(" ").append(this.getLongOptPrefix()).append(option.getLongOpt()); } else { optBuf.append(lpad).append(this.getOptPrefix()).append(option.getOpt()); if (option.hasLongOpt()) { optBuf.append(',').append(this.getLongOptPrefix()).append(option.getLongOpt()); } } if (option.hasArg()) { if (option.hasArgName()) { optBuf.append(" <").append(option.getArgName()).append('>'); } else { optBuf.append(' '); } } sb.append(optBuf.toString()).append(this.getNewLine()); optBuf = new StringBuilder(); optBuf.append(dpad); if (option.getDescription() != null) { optBuf.append(option.getDescription()); } this.renderWrappedText(sb, width, dpad.length(), optBuf.toString()); if (i.hasNext()) { sb.append(this.getNewLine()); sb.append(this.getNewLine()); } } return sb; }
From source file:calculadora.controlador.ControladorComandos.java
/** * Controlo si un comando con posix largo se escribio a la vez que otro * posix largo como puede ser (--help) junto con --(seno) de error. * * Controlo si un comando posix largo se escribio a la vez que otro de posix * corto que no sean --help y -D(ya que si ocurriera esto mostraria la * ayuda), y viceversa tiene que dar error. * * @param opt dos opciones//from w ww .j a va 2s .com * @see Option * @throws ParseException Demasiados comandos */ private void hasBuenosComandos(Option[] opt) throws ParseException { //Obtengo la primera y la segunda opcion Option op1 = opt[0]; Option op2 = opt[1]; boolean isLongOpt1 = op1.hasLongOpt(); boolean isLongOpt2 = op2.hasLongOpt(); //Si los dos comandos son posix largo error if (isLongOpt1 && isLongOpt2) { throw errorComandos(); //Error de comandos //Si el primero es posix largo y el segundo no pero son el comando //-help y -D no debe ocurrir nada } else if ((isLongOpt1 && !isLongOpt2 && op1.getLongOpt().equals(HELP) && op2.getOpt().equals(PROPERTY))) { //No debe hacer nada importante esto vacio. //Si el primero es poxis largo y el segundo posix corto y viceversa } else if (isLongOpt1 && !isLongOpt2 || !isLongOpt1 && isLongOpt2) { throw errorComandos(); //Error de comandos } }
From source file:net.sf.clichart.main.FixedHelpFormatter.java
protected StringBuffer renderOptions(StringBuffer sb, int width, Options options, int leftPad, int descPad) { 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;/*from w ww .jav a 2s. com*/ StringBuffer optBuf; List prefixList = new ArrayList(); Option option; //List optList = options.helpOptions(); Collection optionsCollection = options.getOptions(); List optList = new ArrayList(optionsCollection); Collections.sort(optList, new StringBufferComparator()); for (Iterator i = optList.iterator(); i.hasNext();) { option = (Option) i.next(); optBuf = new StringBuffer(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 StringBuffer(prefixList.get(x++).toString()); if (optBuf.length() < max) { optBuf.append(createPadding(max - optBuf.length())); } optBuf.append(dpad); int nextLineTabStop = max + descPad; renderWrappedText(sb, width, nextLineTabStop, optBuf.append(option.getDescription()).toString()); if (i.hasNext()) { sb.append(defaultNewLine); } } return sb; }
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;//from w w w . j a va 2 s . com 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.codeseed.common.config.ext.CommandLineOptionsBuilder.java
/** * Potentially adds an option to the supplied collection. Used by the * {@link #build()} method to populate an options collection. * * @param options/* ww w . j a v a 2s .c om*/ * the current collection of options * @param groups * mappings of argument group identifiers to groups * @param method * the configuration method to look up */ protected void addOption(Options options, Map<String, OptionGroup> groups, Method method) { final CommandLine commandLine = method.getAnnotation(CommandLine.class); // Iterate over the triggers; take the first values String opt = null; String longOpt = null; for (String trigger : commandLine.value()) { if (!options.hasOption(trigger)) { if (opt == null && trigger.length() == 1) { opt = trigger; } else if (longOpt == null) { longOpt = trigger; } } } // Either we can use the method name or there is no option being added if (opt == null && longOpt == null) { String methodOpt = LOWER_CAMEL.to(LOWER_HYPHEN, method.getName()); if (!options.hasOption(methodOpt)) { longOpt = methodOpt; } else { // TODO Warn? return; } } // Create a new option Option option = new Option(opt, null); option.setLongOpt(longOpt); // Set the number of arguments based on the return type final Class<?> returnType = Primitives.wrap(method.getReturnType()); if (returnType.equals(Boolean.class)) { option.setArgs(0); } else if (Iterable.class.isAssignableFrom(returnType)) { option.setArgs(commandLine.maximum()); } else if (Map.class.isAssignableFrom(returnType)) { option.setArgs(2); option.setValueSeparator('='); } else { option.setArgs(1); } // Add some descriptive text if (bundle != null) { try { // TODO Does this make sense? String key = option.hasLongOpt() ? option.getLongOpt() : method.getName(); option.setDescription(bundle.getString(key + ".description")); } catch (MissingResourceException e) { option.setDescription(null); } } // Set argument names if (bundle != null && option.getArgs() > 0) { try { option.setArgName(bundle.getString(method.getName() + ".argName")); } catch (MissingResourceException e) { option.setArgName(null); } } // Add to either the collection or to the option groups String groupKey = commandLine.groupId(); if (groupKey.isEmpty()) { options.addOption(option); } else { OptionGroup group = groups.get(groupKey); if (group == null) { group = new OptionGroup(); groups.put(groupKey, group); } group.addOption(option); } }