List of usage examples for org.apache.commons.cli Options getOptions
public Collection getOptions()
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. j a v a2 s .co m 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:de.yaio.commons.cli.CmdLineHelper.java
/** * <h1>Bereich:</h1>//from w w w .jav a 2 s .c o m * Tools - CLI-Config * <h1>Funktionalitaet:</h1> * konfiguriert die verfuegbaren CLI-Optionen * <h1>Nebenwirkungen:</h1> * Rueckgabe als Options * @param newAvailiableCmdLineOptions cmd-options to add to availiableCmdLineOptions */ public void addAvailiableCmdLineOptions(final Options newAvailiableCmdLineOptions) { if (commandLine != null) { throw new IllegalStateException("addAvailiableCmdLineOptions: " + "cant add availiableCmdLineOptions " + "because commandLine already set"); } if (newAvailiableCmdLineOptions != null) { // add new Options if (LOGGER.isDebugEnabled()) { LOGGER.debug("addAvailiableCmdLineOptions: " + "add commandLineOptions:" + newAvailiableCmdLineOptions.getOptions().size()); LOGGER.debug("addAvailiableCmdLineOptions: " + "add commandLineOptions:" + newAvailiableCmdLineOptions.getOptions()); LOGGER.debug("addAvailiableCmdLineOptions: " + "add commandLineOptions:" + newAvailiableCmdLineOptions.getRequiredOptions()); LOGGER.debug("addAvailiableCmdLineOptions: " + " details:" + newAvailiableCmdLineOptions); } for (Object newOption : newAvailiableCmdLineOptions.getOptions()) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("addAvailiableCmdLineOptions: " + "add commandLineOption: " + (Option) newOption); } availiableCmdLineOptions.addOption((Option) newOption); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("done addAvailiableCmdLineOptions: " + "with commandLineOptions:" + newAvailiableCmdLineOptions); } } }
From source file:eu.stratosphere.pact.client.CliFrontend.java
/** * Displays exceptions./*w ww. j a v a 2s . c o m*/ * * @param e the exception to display. */ @SuppressWarnings({ "unchecked" }) private void handleError(Throwable t) { if (t instanceof UnrecognizedOptionException) { Options generalOptions = this.options.get(GENERAL_OPTS); boolean generalOption = false; for (Option o : (Collection<Option>) generalOptions.getOptions()) { if (t.getMessage().startsWith("Unrecognized option: -" + o.getOpt()) || t.getMessage().contains("Unrecognized option: -" + o.getLongOpt())) { generalOption = true; } } if (generalOption) { System.err.println("ERROR: General args must be placed directly after action."); } else { System.err.println("ERROR: " + t.getMessage()); } printHelp(); } else { System.err.println("ERROR: " + t.getMessage()); if (this.verbose) { t.printStackTrace(); } else { System.out.println("For a more detailed error message use the '-v' option"); } } System.exit(1); }
From source file:jtabwb.launcher.Launcher.java
/** * Add the specified command options to the command line options for this * launcher. The names of added options must not overlap the names of the * launcher options otherwise a {@link LauncherOptionDefinitionException} is * thrown.//from w w w. j a va 2s.c o m * * @param options the options to add * @throws LauncherOptionDefinitionException if one of the added options has a * name conflicting with one of the launcher options */ public void optConfigCmdLineOptions(Options options) throws LauncherConfigurationException { for (Option opt : options.getOptions()) currentConfiguration.cmdLineOptions.addOption(opt); }
From source file:kieker.tools.traceAnalysis.TraceAnalysisTool.java
@Override @SuppressWarnings("unchecked") protected void addAdditionalOptions(final Options options) { // Remember the inherited options for the help formatter final List<Option> inheritedOptions = new ArrayList<Option>(); inheritedOptions.addAll(options.getOptions()); for (final Object option : Constants.CMDL_OPTIONS.getOptions()) { options.addOption((Option) option); }/*from w w w . java 2 s . co m*/ for (final Option option : inheritedOptions) { if (!Constants.SORTED_OPTION_LIST.contains(option)) { Constants.SORTED_OPTION_LIST.add(option); } } }
From source file:OneParameterization.java
@SuppressWarnings("static-access") @Override/*from w w w .java 2s. c om*/ public Options getOptions() { Options options = super.getOptions(); options.addOption( OptionBuilder.withLongOpt("parameterFile").hasArg().withArgName("file").isRequired().create('p')); options.addOption(OptionBuilder.withLongOpt("input").hasArg().withArgName("file").isRequired().create('i')); options.addOption(OptionBuilder.withLongOpt("inputnumber").hasArg().withArgName("input number").isRequired() .create('u')); options.addOption( OptionBuilder.withLongOpt("output").hasArg().withArgName("file").isRequired().create('o')); options.addOption( OptionBuilder.withLongOpt("problem").hasArg().withArgName("name").isRequired().create('b')); options.addOption( OptionBuilder.withLongOpt("algorithm").hasArg().withArgName("name").isRequired().create('a')); options.addOption(OptionBuilder.withLongOpt("properties").hasArgs().withArgName("p1=v1;p2=v2;...") .withValueSeparator(';').create('x')); options.addOption(OptionBuilder.withLongOpt("seed").hasArg().withArgName("value").create('s')); options.addOption(OptionBuilder.withLongOpt("epsilon").hasArg().withArgName("e1,e2,...").create('e')); options.addOption(OptionBuilder.withLongOpt("metrics").create('m')); options.addOption(OptionBuilder.withLongOpt("reference").hasArg().withArgName("file").create('r')); options.addOption(OptionBuilder.withLongOpt("novariables").create('n')); options.addOption(OptionBuilder.withLongOpt("force").create('f')); //fill in option descriptions for (Object obj : options.getOptions()) { Option option = (Option) obj; option.setDescription(option.getLongOpt()); } return options; }
From source file:net.thackbarth.sparrow.SparrowTest.java
@Test public void testConfiguration() throws ParseException { Options options = Sparrow.createOptions(); Assert.assertEquals("The size of option does not match!", 4, options.getOptions().size()); Assert.assertTrue("The options did not have option 'c'", options.hasOption("c")); Assert.assertTrue("The options did not have option 'clean'", options.hasOption("clean")); Assert.assertTrue("The options did not have option 'f'", options.hasOption("f")); Assert.assertTrue("The options did not have option 'folder'", options.hasOption("folder")); Assert.assertTrue("The options did not have option 'h'", options.hasOption("h")); Assert.assertTrue("The options did not have option 'help'", options.hasOption("help")); Assert.assertTrue("The options did not have option 'l'", options.hasOption("l")); Assert.assertTrue("The options did not have option 'limit'", options.hasOption("limit")); PosixParser parser = new PosixParser(); // Set new data to the configuration String[] args = { "-f", "newdata", "-l", "2000" }; CommandLine commandLine = parser.parse(options, args); Sparrow.processConfiguration(configuration, commandLine); Assert.assertEquals("The folder is not correct!", "newdata", configuration.getDataFolder()); Assert.assertEquals("The scanlimit is not corrent", Integer.valueOf(2000), configuration.getScanLimit()); // Set null configuration Sparrow.processConfiguration(null, null); // no change in the config Assert.assertEquals("The folder is not correct!", "newdata", configuration.getDataFolder()); Assert.assertEquals("The scanlimit is not corrent", Integer.valueOf(2000), configuration.getScanLimit()); // Create new configuration String[] newArgs = {};// w ww . ja va 2 s. co m commandLine = parser.parse(options, newArgs); Sparrow.processConfiguration(configuration, commandLine); // no change in the config Assert.assertEquals("The folder is not correct!", "newdata", configuration.getDataFolder()); Assert.assertEquals("The scanlimit is not corrent", Integer.valueOf(2000), configuration.getScanLimit()); }
From source file:nl.systemsgenetics.eqtlinteractionanalyser.eqtlinteractionanalyser.createOptionsListForManual.java
/** * @param args the command line arguments */// w ww. j a v a2s . c o m public static void main(String[] args) { Options options = EQTLInteractionAnalyser.OPTIONS; System.out.println("| Short | Long | Description |"); System.out.println("|-------|------|-------------|"); for (Object optionO : options.getOptions()) { Option option = (Option) optionO; System.out.print("| -"); System.out.print(option.getOpt()); System.out.print(" | --"); System.out.print(option.getLongOpt()); System.out.print(" | "); System.out.print(option.getDescription()); System.out.println(" | "); } }
From source file:openlr.otk.options.UsageBuilder.java
/** * Prints the command line syntax.// ww w.j a va 2s . c om * * @param target * The target stream to write to * @param options * The tool options * @param argsList * The tool arguments * @param toolID * The tool short name * @param toolDescription * The description of the tool */ public static void usage(final OutputStream target, final Options options, final List<Argument<?>> argsList, final String toolID, final String toolDescription) { HelpFormatter formatter = new HelpFormatter(); PrintWriter pw = new PrintWriter(new OutputStreamWriter(target, IOUtils.SYSTEM_DEFAULT_CHARSET)); String optionsListStr = buildOptionsList(options); String argsListStr = buildArgumentsList(argsList); formatter.printUsage(pw, HelpFormatter.DEFAULT_WIDTH, "java -jar otk-<version>.jar " + toolID + " " + optionsListStr + " " + argsListStr); formatter.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, toolDescription); if (options.getOptions().size() > 0) { formatter.printOptions(pw, HelpFormatter.DEFAULT_WIDTH, options, HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD); } printArgumentDescriptions(pw, argsList); formatter.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, USAGE_FOOTER); pw.flush(); }
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 a2 s .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"); }