new PatternLayout("%d [%-25t] %-5p: %m%n"))); CmdLineParser parser = new CmdLineParser(); CmdLineParser.Option help = parser.addBooleanOption('h', "help"); CmdLineParser.Option output = parser.addStringOption('o', "output"); CmdLineParser.Option iface = parser.addStringOption('i', "iface"); CmdLineParser.Option seedTime = parser.addIntegerOption('s', "seed"); CmdLineParser.Option maxUpload = parser.addDoubleOption('u', "max-upload"); CmdLineParser.Option maxDownload = parser.addDoubleOption('d', "max-download"); parser.parse(args); } catch (CmdLineParser.OptionException oe) { System.err.println(oe.getMessage()); if (Boolean.TRUE.equals((Boolean) parser.getOptionValue(help))) { usage(System.out); System.exit(0); String outputValue = (String) parser.getOptionValue(output, DEFAULT_OUTPUT_DIRECTORY); String ifaceValue = (String) parser.getOptionValue(iface); int seedTimeValue = (Integer) parser.getOptionValue(seedTime, -1); String[] otherArgs = parser.getRemainingArgs(); if (otherArgs.length != 1) { usage(System.err);
public static void main( String[] args ) { CmdLineParser parser = new CmdLineParser(); CmdLineParser.Option date = parser.addOption(new ShortDateOption('d', "date")); try { parser.parse(args); } catch ( CmdLineParser.OptionException e ) { System.err.println(e.getMessage()); printUsage(); System.exit(2); } // Extract the values entered for the various options -- if the // options were not specified, the corresponding values will be // null. Date dateValue = (Date)parser.getOptionValue(date); // For testing purposes, we just print out the option values System.out.println("date: " + dateValue); // Extract the trailing command-line arguments ('a_number') in the // usage string above. String[] otherArgs = parser.getRemainingArgs(); System.out.println("remaining args: "); for ( int i = 0; i < otherArgs.length; ++i ) { System.out.println(otherArgs[i]); } // In a real program, one would pass the option values and other // arguments to a function that does something more useful. System.exit(0); }
String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0]; CmdLineParser parser = new CmdLineParser(); CmdLineParser.Option oUname = parser.addStringOption('u', "username"); CmdLineParser.Option oPasswd = parser.addStringOption('p', "password"); CmdLineParser.Option oSurl = parser.addStringOption('s', "serviceurl"); CmdLineParser.Option oIpAddr = parser.addStringOption('b', "bindaddress"); CmdLineParser.Option oPort = parser.addIntegerOption('P', "port"); CmdLineParser.Option oPid = parser.addStringOption('f', "pid"); CmdLineParser.Option oConfig = parser.addStringOption('c', "config"); parser.parse(args); } catch (CmdLineParser.OptionException e) { LOG.error(e.toString()); sdkUrl = (String) parser.getOptionValue(oSurl); uname = (String) parser.getOptionValue(oUname); passwd = (String) parser.getOptionValue(oPasswd); ipaddr = (String) parser.getOptionValue(oIpAddr); port = (Integer) parser.getOptionValue(oPort); pidFile = (String) parser.getOptionValue(oPid); String config = (String) parser.getOptionValue(oConfig); if (config != null) { // If a configuration file is specified Properties prop = new Properties();
/** * Equivalent to {@link #getOptionValue(Option, Object) getOptionValue(o, * null)}. */ public final Object getOptionValue( Option o ) { return getOptionValue(o, null); }
/** * Create a new command line parser. * * @return command line parser */ public static CmdLineParser buildParser() { CmdLineParser parser = new CmdLineParser(); HELP_ARG = parser.addBooleanOption(HELP); CONFIG_DIR_ARG = parser.addStringOption(CONFIG_DIR); SPRING_EXTS_ARG = parser.addStringOption(SPRING_EXTS); REQUESTER_ARG = parser.addStringOption(REQUESTER); ISSUER_ARG = parser.addStringOption(ISSUER); PRINCIPAL_ARG = parser.addStringOption(PRINCIPAL); AUTHN_METHOD_ARG = parser.addStringOption(AUTHN_METHOD); SAML1_ARG = parser.addBooleanOption(SAML1); return parser; } }
myOptions.parse(args); myOptions.getOptionValue(allOptions[j])); String[] otherArgs = myOptions.getRemainingArgs(); System.out.println("remaining args: "); for ( int i = 0; i<otherArgs.length; ++i ) {
/** * <p> * This method has to be called initially by the code using this instance in order to configure. * </p> * <p> * Every subclass has to call <code>super.parseArgs(cmdLineArgs)</code> and then retrieve the options needed from * the returned CmdLineParser! * </p> * * @param cmdLineArgs */ public void parseArgs(String[] cmdLineArgs) throws Exception { // The Exception is not caught: Testbucket is greedy for any type of // output (piped back to it). this.cmdLineParser.parse(cmdLineArgs); }
/** * Parses the command line arguments * * @param args command line arguments * * @return parsed command line arguments * * @throws Exception thrown if the underlying libraries could not be initialized */ private static CmdLineParser parseCommandArguments(String[] args) throws Exception { if (args.length < 2) { printHelp(System.out); System.out.flush(); System.exit(0); } CmdLineParser parser = CLIParserBuilder.buildParser(); try { parser.parse(args); } catch (CmdLineParser.OptionException e) { errorAndExit(e.getMessage(), e); } Boolean helpEnabled = (Boolean) parser.getOptionValue(CLIParserBuilder.HELP_ARG); if (helpEnabled != null) { printHelp(System.out); System.out.flush(); System.exit(0); } return parser; }
/** * */ public ACmdLineArgsInheritor() { this.cmdLineOptions = new HashMap(); this.cmdLineParser = new CmdLineParser(); }
/** * Convenience method for adding a double option. * @return the new Option */ public final Option addDoubleOption( String longForm ) { return addOption(new Option.DoubleOption(longForm)); }
/** * Equivalent to {@link #getOptionValue(Option, Object) getOptionValue(o, * null)}. */ public final Object getOptionValue( Option o ) { return getOptionValue(o, null); }
/** * Extract the options and non-option arguments from the given * list of command-line arguments. The default locale is used for * parsing options whose values might be locale-specific. */ public final void parse( String[] argv ) throws IllegalOptionValueException, UnknownOptionException { // It would be best if this method only threw OptionException, but for // backwards compatibility with old user code we throw the two // exceptions above instead. parse(argv, Locale.getDefault()); }
CmdLineParser parser = new CmdLineParser(); CmdLineParser.Option help = parser.addBooleanOption('h', "help"); CmdLineParser.Option filename = parser.addStringOption('t', "torrent"); CmdLineParser.Option create = parser.addBooleanOption('c', "create"); CmdLineParser.Option pieceLength = parser.addIntegerOption('l', "length"); CmdLineParser.Option announce = parser.addStringOption('a', "announce"); parser.parse(args); } catch (CmdLineParser.OptionException oe) { System.err.println(oe.getMessage()); if (Boolean.TRUE.equals(parser.getOptionValue(help))) { usage(System.out); System.exit(0); String filenameValue = (String) parser.getOptionValue(filename); if (filenameValue == null) { usage(System.err, "Torrent file must be provided!"); Integer pieceLengthVal = (Integer) parser.getOptionValue(pieceLength); if (pieceLengthVal == null) { pieceLengthVal = TorrentCreator.DEFAULT_PIECE_LENGTH; Boolean createFlag = (Boolean) parser.getOptionValue(create); Vector<String> announceURLs = (Vector<String>) parser.getOptionValues(announce); String[] otherArgs = parser.getRemainingArgs();
/** * Convenience method for adding a boolean option. * @return the new Option */ public final Option addBooleanOption( String longForm ) { return addOption(new Option.BooleanOption(longForm)); }
/** * Equivalent to {@link #getOptionValue(Option, Object) getOptionValue(o, null)}. * * @param o * the o * @return the option value */ public final Object getOptionValue(Option o) { return getOptionValue(o, null); }
/** * Extract the options and non-option arguments from the given * list of command-line arguments. The default locale is used for * parsing options whose values might be locale-specific. */ public final void parse( String[] argv ) throws IllegalOptionValueException, UnknownOptionException { // It would be best if this method only threw OptionException, but for // backwards compatibility with old user code we throw the two // exceptions above instead. parse(argv, Locale.getDefault()); }