jargs-1.0/0000755000175200017520000000000010512022420012272 5ustar debyanndebyannjargs-1.0/doc/0000755000175200017520000000000010512022226013043 5ustar debyanndebyannjargs-1.0/doc/.cvsignore0000644000175200017520000000000407741764621015063 0ustar debyanndebyannapi jargs-1.0/doc/CHANGES0000644000175200017520000000415110225321751014045 0ustar debyanndebyannChanges from 0.5 to 1.0 ======================= - Added getOptionValue(Option, Object) which takes an Object as a potential default value for the given Option. If the option is not present on the command line, the default is returned instead. This addresses bug 1051346 submitted by Tomas Znamenacek, and feature request 527808. - Added getOptionValues(Option) which will return a list of occurrences of a single option, and added the ability internally to cope with such multiple occurrences. This accepts and uses patch 1164413, and closes patch 1083615 (it solves the same problem in a better way). - Added support for concatenated boolean options (those written -bcd where -b, -c, and -d are boolean options). This accepts and uses patch 621087 by Vidar Holen. - Added some more unit tests. - Some code clean-ups. Changes from 0.4 to 0.5 ======================= - Allow Options that have a long form only - Added LongOption - Corrected some typos in example code (thanks Chris McKay) - More tests Changes from 0.3 to 0.4 ======================= - Switched from makefiles to Ant - Some code clean-ups - Support locale-dependent parsing of Double options Changes from 0.2 to 0.3 ======================= - Added 'DoubleOption' class, and 'addDoubleOption()' convenience method (suggested independently by Nick Roy and Karl Koster) - Made 'Option.getValue()' and 'Option.parseValue()' take a Locale, so that parsing of values can be locale-dependent. (Locale-specific parsing is not however implemented for DoubleOption and IntegerOption.) - Constructor of 'IllegalOptionValueException' is now public so that it can be thrown by the 'parseValue()' methods of custom Option subclasses - 'CmdLineParser.addOption()' now returns the passed in Option, which can make client code less clunky. - Added 'DoubleOption' to example classes - Added a 'CustomOptionTest' class to demonstrate subclassing of Option via the implementation of a ShortDateOption class. - Added this CHANGES file. - Added overridden CmdLineParser.parse() which takes a Locale instance, and made both versions of the method final. jargs-1.0/.cvsignore0000644000175200017520000000006010225760132014300 0ustar debyanndebyannclasses .classpath .project jargs.ipr jargs.iws jargs-1.0/LICENCE0000644000175200017520000000307110225316242013271 0ustar debyanndebyannCopyright (c) 2001-2003 Steve Purcell. Copyright (c) 2002 Vidar Holen. Copyright (c) 2002 Michal Ceresna. Copyright (c) 2005 Ewan Mellor. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. jargs-1.0/README0000644000175200017520000000332010225760076013171 0ustar debyanndebyannJArgs command-line argument parsing library =========================================== Copyright (c) 2001-2003 Steve Purcell. Copyright (c) 2002 Vidar Holen. Copyright (c) 2002 Michal Ceresna. Copyright (c) 2005 Ewan Mellor. All rights reserved. Released under the terms of the BSD licence. See the file LICENCE for details. Prerequisites ------------- For each prerequisite, the version with which JArgs has been tested is given in parentheses. Any version equal to or later than this should work. Apache Ant (1.4.1), by The Apache Software Foundation, from http://ant.apache.org/. Ant is used to build JArgs, and to run its tests. JUnit (3.7), by Eric Gamma, et al, from http://sourceforge.net/projects/junit. JUnit is used to run the unit tests, and is not needed to run the library itself. Installation ------------ To compile, package, and test the code, run ant Two jars are created, one called lib/jargs.jar, which contains the runtime library, and one called lib/jargs-test.jar, which contains the unit tests and the examples. The Javadoc APIs are created in doc/api. To use the library with your own code, simply ensure that lib/jargs.jar is on the CLASSPATH. Documentation ------------- The main documentation is the detailed worked example in src/jargs/examples/gnu/OptionTest.java, plus the generated API documentation in doc/api/. Package contents ---------------- src/jargs/gnu -- The library itself. src/jargs/examples/gnu -- Examples showing how to use the library. src/jargs/test/gnu -- JUnit tests. doc/ -- API and other documentation. classes/ -- Compiled classes, once built. lib/ -- JArgs jars, once built. jargs-1.0/TODO0000644000175200017520000000012010225774517013001 0ustar debyanndebyannJArgs to-do list ================ Nothing! (Fate, consider yourself tempted.) jargs-1.0/build.xml0000644000175200017520000000656510225316746014151 0ustar debyanndebyann jargs-1.0/src/0000755000175200017520000000000010225775322013102 5ustar debyanndebyannjargs-1.0/src/jargs/0000755000175200017520000000000010225775323014211 5ustar debyanndebyannjargs-1.0/src/jargs/examples/0000755000175200017520000000000010225775322016026 5ustar debyanndebyannjargs-1.0/src/jargs/examples/gnu/0000755000175200017520000000000010225775322016617 5ustar debyanndebyannjargs-1.0/src/jargs/examples/gnu/AutoHelpParser.java0000644000175200017520000000601110145372027022352 0ustar debyanndebyannpackage jargs.examples.gnu; import jargs.gnu.CmdLineParser; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * This example shows how to dynamically create basic output for a --help option. */ public class AutoHelpParser extends CmdLineParser { List optionHelpStrings = new ArrayList(); public Option addHelp(Option option, String helpString) { optionHelpStrings.add(" -" + option.shortForm() + "/--" + option.longForm() + ": " + helpString); return option; } public void printUsage() { System.err.println("usage: prog [options]"); for (Iterator i = optionHelpStrings.iterator(); i.hasNext(); ) { System.err.println(i.next()); } } public static void main( String[] args ) { AutoHelpParser parser = new AutoHelpParser(); CmdLineParser.Option verbose = parser.addHelp( parser.addBooleanOption('v', "verbose"), "Print extra information"); CmdLineParser.Option size = parser.addHelp( parser.addIntegerOption('s', "size"), "The extent of the thing"); CmdLineParser.Option name = parser.addHelp( parser.addStringOption('n', "name"), "Name given to the widget"); CmdLineParser.Option fraction = parser.addHelp( parser.addDoubleOption('f', "fraction"), "What percentage should be discarded"); CmdLineParser.Option help = parser.addHelp( parser.addBooleanOption('h', "help"), "Show this help message"); try { parser.parse(args); } catch ( CmdLineParser.OptionException e ) { System.err.println(e.getMessage()); parser.printUsage(); System.exit(2); } if ( Boolean.TRUE.equals(parser.getOptionValue(help))) { parser.printUsage(); System.exit(0); } // Extract the values entered for the various options -- if the // options were not specified, the corresponding values will be // null. Boolean verboseValue = (Boolean)parser.getOptionValue(verbose); Integer sizeValue = (Integer)parser.getOptionValue(size); String nameValue = (String)parser.getOptionValue(name); Double fractionValue = (Double)parser.getOptionValue(fraction); // For testing purposes, we just print out the option values System.out.println("verbose: " + verboseValue); System.out.println("size: " + sizeValue); System.out.println("name: " + nameValue); System.out.println("fraction: " + fractionValue); // Extract the trailing command-line arguments ('a_nother') 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); } } jargs-1.0/src/jargs/examples/gnu/CustomOptionTest.java0000644000175200017520000000445607633365006023001 0ustar debyanndebyannpackage jargs.examples.gnu; import jargs.gnu.CmdLineParser; import java.text.DateFormat; import java.text.ParseException; import java.util.Locale; import java.util.Date; public class CustomOptionTest { private static void printUsage() { System.err.println("usage: prog [{-d,--date} date]"); } /** * A custom type of command line option corresponding to a short * date value, e.g. . */ public static class ShortDateOption extends CmdLineParser.Option { public ShortDateOption( char shortForm, String longForm ) { super(shortForm, longForm, true); } protected Object parseValue( String arg, Locale locale ) throws CmdLineParser.IllegalOptionValueException { try { DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale); return dateFormat.parse(arg); } catch (ParseException e) { throw new CmdLineParser.IllegalOptionValueException(this, arg); } } } 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); } } jargs-1.0/src/jargs/examples/gnu/OptionParserSubclassTest.java0000644000175200017520000000435307633365006024457 0ustar debyanndebyannpackage jargs.examples.gnu; import jargs.gnu.CmdLineParser; public class OptionParserSubclassTest { private static class MyOptionsParser extends CmdLineParser { public static final Option VERBOSE = new CmdLineParser.Option.BooleanOption('v',"verbose"); public static final Option SIZE = new CmdLineParser.Option.IntegerOption('s',"size"); public static final Option NAME = new CmdLineParser.Option.StringOption('n',"name"); public static final Option FRACTION = new CmdLineParser.Option.DoubleOption('f',"fraction"); public MyOptionsParser() { super(); addOption(VERBOSE); addOption(SIZE); addOption(NAME); addOption(FRACTION); } } private static void printUsage() { System.err.println("usage: prog [{-v,--verbose}] [{-n,--name} a_name]"+ "[{-s,--size} a_number] [{-f,--fraction} a_float]"); } public static void main( String[] args ) { MyOptionsParser myOptions = new MyOptionsParser(); try { myOptions.parse(args); } catch ( CmdLineParser.UnknownOptionException e ) { System.err.println(e.getMessage()); printUsage(); System.exit(2); } catch ( CmdLineParser.IllegalOptionValueException e ) { System.err.println(e.getMessage()); printUsage(); System.exit(2); } CmdLineParser.Option[] allOptions = new CmdLineParser.Option[] { MyOptionsParser.VERBOSE, MyOptionsParser.NAME, MyOptionsParser.SIZE, MyOptionsParser.FRACTION }; for ( int j = 0; jgetMessage() returns * an error string suitable for reporting the error to the user (in * English). */ public static class UnknownOptionException extends OptionException { UnknownOptionException( String optionName ) { this(optionName, "Unknown option '" + optionName + "'"); } UnknownOptionException( String optionName, String msg ) { super(msg); this.optionName = optionName; } /** * @return the name of the option that was unknown (e.g. "-u") */ public String getOptionName() { return this.optionName; } private String optionName = null; } /** * Thrown when the parsed commandline contains multiple concatenated * short options, such as -abcd, where one is unknown. * getMessage() returns an english human-readable error * string. * @author Vidar Holen */ public static class UnknownSuboptionException extends UnknownOptionException { private char suboption; UnknownSuboptionException( String option, char suboption ) { super(option, "Illegal option: '"+suboption+"' in '"+option+"'"); this.suboption=suboption; } public char getSuboption() { return suboption; } } /** * Thrown when the parsed commandline contains multiple concatenated * short options, such as -abcd, where one or more requires a value. * getMessage() returns an english human-readable error * string. * @author Vidar Holen */ public static class NotFlagException extends UnknownOptionException { private char notflag; NotFlagException( String option, char unflaggish ) { super(option, "Illegal option: '"+option+"', '"+ unflaggish+"' requires a value"); notflag=unflaggish; } /** * @return the first character which wasn't a boolean (e.g 'c') */ public char getOptionChar() { return notflag; } } /** * Thrown when an illegal or missing value is given by the user for * an option that takes a value. getMessage() returns * an error string suitable for reporting the error to the user (in * English). */ public static class IllegalOptionValueException extends OptionException { public IllegalOptionValueException( Option opt, String value ) { super("Illegal value '" + value + "' for option " + (opt.shortForm() != null ? "-" + opt.shortForm() + "/" : "") + "--" + opt.longForm()); this.option = opt; this.value = value; } /** * @return the name of the option whose value was illegal (e.g. "-u") */ public Option getOption() { return this.option; } /** * @return the illegal value */ public String getValue() { return this.value; } private Option option; private String value; } /** * Representation of a command-line option */ public static abstract class Option { protected Option( String longForm, boolean wantsValue ) { this(null, longForm, wantsValue); } protected Option( char shortForm, String longForm, boolean wantsValue ) { this(new String(new char[]{shortForm}), longForm, wantsValue); } private Option( String shortForm, String longForm, boolean wantsValue ) { if ( longForm == null ) throw new IllegalArgumentException("Null longForm not allowed"); this.shortForm = shortForm; this.longForm = longForm; this.wantsValue = wantsValue; } public String shortForm() { return this.shortForm; } public String longForm() { return this.longForm; } /** * Tells whether or not this option wants a value */ public boolean wantsValue() { return this.wantsValue; } public final Object getValue( String arg, Locale locale ) throws IllegalOptionValueException { if ( this.wantsValue ) { if ( arg == null ) { throw new IllegalOptionValueException(this, ""); } return this.parseValue(arg, locale); } else { return Boolean.TRUE; } } /** * Override to extract and convert an option value passed on the * command-line */ protected Object parseValue( String arg, Locale locale ) throws IllegalOptionValueException { return null; } private String shortForm = null; private String longForm = null; private boolean wantsValue = false; public static class BooleanOption extends Option { public BooleanOption( char shortForm, String longForm ) { super(shortForm, longForm, false); } public BooleanOption( String longForm ) { super(longForm, false); } } /** * An option that expects an integer value */ public static class IntegerOption extends Option { public IntegerOption( char shortForm, String longForm ) { super(shortForm, longForm, true); } public IntegerOption( String longForm ) { super(longForm, true); } protected Object parseValue( String arg, Locale locale ) throws IllegalOptionValueException { try { return new Integer(arg); } catch (NumberFormatException e) { throw new IllegalOptionValueException(this, arg); } } } /** * An option that expects a long integer value */ public static class LongOption extends Option { public LongOption( char shortForm, String longForm ) { super(shortForm, longForm, true); } public LongOption( String longForm ) { super(longForm, true); } protected Object parseValue( String arg, Locale locale ) throws IllegalOptionValueException { try { return new Long(arg); } catch (NumberFormatException e) { throw new IllegalOptionValueException(this, arg); } } } /** * An option that expects a floating-point value */ public static class DoubleOption extends Option { public DoubleOption( char shortForm, String longForm ) { super(shortForm, longForm, true); } public DoubleOption( String longForm ) { super(longForm, true); } protected Object parseValue( String arg, Locale locale ) throws IllegalOptionValueException { try { NumberFormat format = NumberFormat.getNumberInstance(locale); Number num = (Number)format.parse(arg); return new Double(num.doubleValue()); } catch (ParseException e) { throw new IllegalOptionValueException(this, arg); } } } /** * An option that expects a string value */ public static class StringOption extends Option { public StringOption( char shortForm, String longForm ) { super(shortForm, longForm, true); } public StringOption( String longForm ) { super(longForm, true); } protected Object parseValue( String arg, Locale locale ) { return arg; } } } /** * Add the specified Option to the list of accepted options */ public final Option addOption( Option opt ) { if ( opt.shortForm() != null ) this.options.put("-" + opt.shortForm(), opt); this.options.put("--" + opt.longForm(), opt); return opt; } /** * Convenience method for adding a string option. * @return the new Option */ public final Option addStringOption( char shortForm, String longForm ) { return addOption(new Option.StringOption(shortForm, longForm)); } /** * Convenience method for adding a string option. * @return the new Option */ public final Option addStringOption( String longForm ) { return addOption(new Option.StringOption(longForm)); } /** * Convenience method for adding an integer option. * @return the new Option */ public final Option addIntegerOption( char shortForm, String longForm ) { return addOption(new Option.IntegerOption(shortForm, longForm)); } /** * Convenience method for adding an integer option. * @return the new Option */ public final Option addIntegerOption( String longForm ) { return addOption(new Option.IntegerOption(longForm)); } /** * Convenience method for adding a long integer option. * @return the new Option */ public final Option addLongOption( char shortForm, String longForm ) { return addOption(new Option.LongOption(shortForm, longForm)); } /** * Convenience method for adding a long integer option. * @return the new Option */ public final Option addLongOption( String longForm ) { return addOption(new Option.LongOption(longForm)); } /** * Convenience method for adding a double option. * @return the new Option */ public final Option addDoubleOption( char shortForm, String longForm ) { return addOption(new Option.DoubleOption(shortForm, longForm)); } /** * Convenience method for adding a double option. * @return the new Option */ public final Option addDoubleOption( String longForm ) { return addOption(new Option.DoubleOption(longForm)); } /** * Convenience method for adding a boolean option. * @return the new Option */ public final Option addBooleanOption( char shortForm, String longForm ) { return addOption(new Option.BooleanOption(shortForm, longForm)); } /** * 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)}. */ public final Object getOptionValue( Option o ) { return getOptionValue(o, null); } /** * @return the parsed value of the given Option, or null if the * option was not set */ public final Object getOptionValue( Option o, Object def ) { Vector v = (Vector)values.get(o.longForm()); if (v == null) { return def; } else if (v.isEmpty()) { return null; } else { Object result = v.elementAt(0); v.removeElementAt(0); return result; } } /** * @return A Vector giving the parsed values of all the occurrences of the * given Option, or an empty Vector if the option was not set. */ public final Vector getOptionValues( Option option ) { Vector result = new Vector(); while (true) { Object o = getOptionValue(option, null); if (o == null) { return result; } else { result.addElement(o); } } } /** * @return the non-option arguments */ public final String[] getRemainingArgs() { return this.remainingArgs; } /** * 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()); } /** * Extract the options and non-option arguments from the given * list of command-line arguments. The specified locale is used for * parsing options whose values might be locale-specific. */ public final void parse( String[] argv, Locale locale ) 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. Vector otherArgs = new Vector(); int position = 0; this.values = new Hashtable(10); while ( position < argv.length ) { String curArg = argv[position]; if ( curArg.startsWith("-") ) { if ( curArg.equals("--") ) { // end of options position += 1; break; } String valueArg = null; if ( curArg.startsWith("--") ) { // handle --arg=value int equalsPos = curArg.indexOf("="); if ( equalsPos != -1 ) { valueArg = curArg.substring(equalsPos+1); curArg = curArg.substring(0,equalsPos); } } else if(curArg.length() > 2) { // handle -abcd for(int i=1; i