List of usage examples for org.apache.commons.cli Option isRequired
public boolean isRequired()
From source file:nl.toolforge.karma.console.CommandRenderer.java
private static StringBuffer printCommand(CommandDescriptor descriptor, Option[] options, boolean showOptions, boolean showHelp) { StringBuffer buffer = new StringBuffer(); String commandNameAlias = descriptor.getName() + " (" + descriptor.getAlias() + ")"; buffer.append(commandNameAlias).append(StringUtils.repeat(" ", COMMAND_FILL - commandNameAlias.length())); if (!showOptions) { buffer.append(descriptor.getDescription()); }// w ww . ja v a2 s .c o m buffer.append("\n"); if (showOptions) { for (int j = 0; j < options.length; j++) { Option o = options[j]; String leftPadding = " "; buffer.append(leftPadding).append("-" + o.getOpt()).append(", --" + o.getLongOpt()); String args = ""; if (o.hasArg()) { args = " <".concat(o.getArgName()).concat(">"); } // todo when the commands are described with too much of text, then FILL will run out of count ... // buffer.append(args.concat(StringUtils.repeat(" ", OPTION_FILL - (o.getLongOpt() + args).length()))); buffer.append(leftPadding); if (!o.isRequired()) { buffer.append("(Optional) "); } buffer.append(o.getDescription()); buffer.append("\n"); } } if (showHelp) { buffer.append("\n"); String trimmed = NiftyStringUtils.deleteWhiteSpaceExceptOne(descriptor.getHelp()); String[] split = NiftyStringUtils.split(trimmed, " ", 120); String joined = StringUtils.join(split, "\n"); buffer.append(joined); buffer.append("\n"); } return buffer; }
From source file:openlr.otk.options.CommandLineData.java
/** * Check required options./*from w w w . ja v a 2 s . c o m*/ * * @param cmdLine * the cmd line * @throws CommandLineParseException * the parse exception */ private void checkRequiredOptions(final CommandLine cmdLine) throws CommandLineParseException { for (Option opt : cmdLine.getOptions()) { if (opt.isRequired() && !cmdLine.hasOption(opt.getOpt())) { throw new CommandLineParseException("Missing mandatory parameter: " + opt.getOpt()); } } }
From source file:openlr.otk.options.UsageBuilder.java
/** * Builds the string displaying the list of available options in one row. * /*from w w w. j a v a 2s . c o m*/ * @param options * The tool options * @return The string listing all available options. */ private static String buildOptionsList(final Options options) { Collection<?> optionsColl = options.getOptions(); List<String> required = new ArrayList<String>(optionsColl.size()); List<String> optional = new ArrayList<String>(optionsColl.size()); for (Object option : optionsColl) { Option opt = (Option) option; if (opt.isRequired()) { required.add(opt.getOpt()); } else { optional.add(opt.getOpt()); } } return buildFormattedLists(required, optional, "-%s"); }
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; // 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);//from w w w . ja va 2 s . co m // 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.eclim.annotation.CommandListingProcessor.java
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) { Options options = new Options(); Pattern pattern = null;/*from w w w . ja v a2 s . c om*/ String filter = this.processingEnv.getOptions().get("filter"); if (filter != null) { pattern = Pattern.compile(filter); } for (TypeElement element : annotations) { for (Element e : env.getElementsAnnotatedWith(element)) { Command command = e.getAnnotation(Command.class); if (pattern == null || pattern.matcher(command.name()).matches()) { Collection<Option> opts = options.parseOptions(command.options()); System.out.print(command.name()); for (Option opt : opts) { String display = "-" + opt.getOpt(); if (opt.hasArg()) { display += " " + opt.getLongOpt(); } if (opt.isRequired()) { System.out.print(" " + display); } else { System.out.print(" [" + display + "]"); } } System.out.println("\n\tclass: " + e); } } } return true; }
From source file:org.eclim.command.Main.java
public static void usage(String cmd, PrintStream out) { ArrayList<org.eclim.annotation.Command> commands = new ArrayList<org.eclim.annotation.Command>(); for (Class<? extends Command> command : Services.getCommandClasses()) { commands.add(command.getAnnotation(org.eclim.annotation.Command.class)); }//from ww w .j a v a 2 s . c o m Collections.sort(commands, new Comparator<org.eclim.annotation.Command>() { public int compare(org.eclim.annotation.Command o1, org.eclim.annotation.Command o2) { return o1.name().compareTo(o2.name()); } }); boolean cmdFound = cmd == null; if (cmd == null) { String osOpts = StringUtils.EMPTY; if (SystemUtils.IS_OS_UNIX) { osOpts = " [-f eclimrc] [--nailgun-port port]"; } out.println("Usage: eclim" + osOpts + " -command command [args]"); out.println(" To view a full list of available commands:"); out.println(" eclim -? commands"); out.println(" To view info for a specific command:"); out.println(" eclim -? <command_name>"); out.println(" Ex."); out.println(" eclim -? project_create"); } else if (cmd.equals("commands")) { out.println("Available Commands:"); } else { out.println("Requested Command:"); } for (org.eclim.annotation.Command command : commands) { if (cmd == null || (!cmd.equals(command.name()) && !cmd.equals("commands"))) { continue; } cmdFound = true; Collection<Option> options = new Options().parseOptions(command.options()); StringBuffer opts = new StringBuffer(); Iterator<Option> iterator = options.iterator(); for (int ii = 0; iterator.hasNext(); ii++) { Option option = iterator.next(); opts.append(option.isRequired() ? " " : " ["); opts.append('-').append(option.getOpt()); if (option.hasArg()) { opts.append(' ').append(option.getLongOpt()); } if (!option.isRequired()) { opts.append(']'); } // wrap every 4 options if ((ii + 1) % 4 == 0 && ii != options.size() - 1) { opts.append(StringUtils.rightPad("\n", command.name().length() + 5)); } } StringBuffer info = new StringBuffer().append(" ").append(command.name()).append(opts); out.println(info); if (!command.description().equals(StringUtils.EMPTY)) { out.println(" " + command.description()); } } if (!cmdFound) { out.println(" No Such Command: " + cmd); } }
From source file:org.finra.dm.core.ArgumentParserTest.java
/** * Dump state of the Option object instance, suitable for debugging and comparing Options as Strings. * * @param option the Option object instance to dump the state of * * @return Stringified form of this Option object instance *//*from w w w . ja va2 s . c o m*/ private String optionToString(Option option) { StringBuilder buf = new StringBuilder(); buf.append("[ option: "); buf.append(option.getOpt()); if (option.getLongOpt() != null) { buf.append(" ").append(option.getLongOpt()); } buf.append(" "); if (option.hasArg()) { buf.append(" [ARG]"); } buf.append(" :: ").append(option.getDescription()); if (option.isRequired()) { buf.append(" [REQUIRED]"); } buf.append(" ]"); return buf.toString(); }
From source file:org.g_node.srv.CliOptionServiceTest.java
/** * Main assertions of all option arguments. * @param opt The actual {@link Option}. * @param shortOpt Short commandline argument of the current option. * @param longOpt Long commandline argument of the current option. * @param desc Description text of the current option. * @param isRequired Whether the current option is a required commandline argument. * @param hasArguments Whether the current option requires an additional argument. *//* w ww .j av a2s .c o m*/ private void assertOption(final Option opt, final String shortOpt, final String longOpt, final String desc, final Boolean isRequired, final Boolean hasArgument, final Boolean hasArguments) { assertThat(opt.getOpt()).isEqualToIgnoringCase(shortOpt); assertThat(opt.getLongOpt()).isEqualToIgnoringCase(longOpt); assertThat(opt.getDescription()).contains(desc); assertThat(opt.isRequired()).isEqualTo(isRequired); assertThat(opt.hasArg()).isEqualTo(hasArgument); assertThat(opt.hasArgs()).isEqualTo(hasArguments); }
From source file:org.linqs.psl.cli.Launcher.java
private static HelpFormatter getHelpFormatter() { HelpFormatter helpFormatter = new HelpFormatter(); // Hack the option ordering to put argumentions without options first and then required options first. // infer and learn go first, then required, then just normal. helpFormatter.setOptionComparator(new Comparator<Option>() { @Override/*from www. jav a 2 s . co m*/ public int compare(Option o1, Option o2) { String name1 = o1.getOpt(); if (name1 == null) { name1 = o1.getLongOpt(); } String name2 = o2.getOpt(); if (name2 == null) { name2 = o2.getLongOpt(); } if (name1.equals(OPERATION_INFER)) { return -1; } if (name2.equals(OPERATION_INFER)) { return 1; } if (name1.equals(OPERATION_LEARN)) { return -1; } if (name2.equals(OPERATION_LEARN)) { return 1; } if (o1.isRequired() && !o2.isRequired()) { return -1; } if (!o1.isRequired() && o2.isRequired()) { return 1; } return name1.compareTo(name2); } }); helpFormatter.setWidth(100); return helpFormatter; }
From source file:org.os.interpreter.RunnerStartupInfo.java
public boolean isValidOptions(boolean printHelpWhenInvalid) { try {// w ww. j a v a 2s.c o m initOptions(); for (Option opt : options.getOptions()) { if (opt.isRequired() && !commandLine.hasOption(opt.getOpt())) { if (printHelpWhenInvalid) { showHelp(); } return false; } } return true; } catch (ParseException e) { showHelp(); return false; } catch (Exception e) { e.printStackTrace(); return false; } }