summaryrefslogtreecommitdiff
path: root/3rdParty/jargs-1.0/src
diff options
context:
space:
mode:
Diffstat (limited to '3rdParty/jargs-1.0/src')
-rw-r--r--3rdParty/jargs-1.0/src/main/java/jargs/gnu/CmdLineParser.java526
-rw-r--r--3rdParty/jargs-1.0/src/test/java/jargs/gnu/test/AllTests.java12
-rw-r--r--3rdParty/jargs-1.0/src/test/java/jargs/gnu/test/CmdLineParserTestCase.java252
-rw-r--r--3rdParty/jargs-1.0/src/test/java/jargs/gnu/test/CustomOptionTestCase.java71
4 files changed, 861 insertions, 0 deletions
diff --git a/3rdParty/jargs-1.0/src/main/java/jargs/gnu/CmdLineParser.java b/3rdParty/jargs-1.0/src/main/java/jargs/gnu/CmdLineParser.java
new file mode 100644
index 0000000..eddc658
--- /dev/null
+++ b/3rdParty/jargs-1.0/src/main/java/jargs/gnu/CmdLineParser.java
@@ -0,0 +1,526 @@
+package jargs.gnu;
+
+import java.text.NumberFormat;
+import java.text.ParseException;
+import java.util.Hashtable;
+import java.util.Vector;
+import java.util.Enumeration;
+import java.util.Locale;
+
+/**
+ * Largely GNU-compatible command-line options parser. Has short (-v) and
+ * long-form (--verbose) option support, and also allows options with
+ * associated values (-d 2, --debug 2, --debug=2). Option processing
+ * can be explicitly terminated by the argument '--'.
+ *
+ * @author Steve Purcell
+ * @version $Revision: 1.10 $
+ * @see jargs.examples.gnu.OptionTest
+ */
+public class CmdLineParser {
+
+ /**
+ * Base class for exceptions that may be thrown when options are parsed
+ */
+ public static abstract class OptionException extends Exception {
+ OptionException(String msg) { super(msg); }
+ }
+
+ /**
+ * Thrown when the parsed command-line contains an option that is not
+ * recognised. <code>getMessage()</code> 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.
+ * <code>getMessage()</code> 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.
+ * <code>getMessage()</code> 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. <code>getMessage()</code> 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<curArg.length(); i++) {
+ Option opt=(Option)this.options.get
+ ("-"+curArg.charAt(i));
+ if(opt==null) throw new
+ UnknownSuboptionException(curArg,curArg.charAt(i));
+ if(opt.wantsValue()) throw new
+ NotFlagException(curArg,curArg.charAt(i));
+ addValue(opt, opt.getValue(null,locale));
+
+ }
+ position++;
+ continue;
+ }
+
+ Option opt = (Option)this.options.get(curArg);
+ if ( opt == null ) {
+ throw new UnknownOptionException(curArg);
+ }
+ Object value = null;
+ if ( opt.wantsValue() ) {
+ if ( valueArg == null ) {
+ position += 1;
+ if ( position < argv.length ) {
+ valueArg = argv[position];
+ }
+ }
+ value = opt.getValue(valueArg, locale);
+ }
+ else {
+ value = opt.getValue(null, locale);
+ }
+
+ addValue(opt, value);
+
+ position += 1;
+ }
+ else {
+ otherArgs.addElement(curArg);
+ position += 1;
+ }
+ }
+ for ( ; position < argv.length; ++position ) {
+ otherArgs.addElement(argv[position]);
+ }
+
+ this.remainingArgs = new String[otherArgs.size()];
+ otherArgs.copyInto(remainingArgs);
+ }
+
+
+ private void addValue(Option opt, Object value) {
+ String lf = opt.longForm();
+
+ Vector v = (Vector)values.get(lf);
+
+ if (v == null) {
+ v = new Vector();
+ values.put(lf, v);
+ }
+
+ v.addElement(value);
+ }
+
+
+ private String[] remainingArgs = null;
+ private Hashtable options = new Hashtable(10);
+ private Hashtable values = new Hashtable(10);
+}
diff --git a/3rdParty/jargs-1.0/src/test/java/jargs/gnu/test/AllTests.java b/3rdParty/jargs-1.0/src/test/java/jargs/gnu/test/AllTests.java
new file mode 100644
index 0000000..2edda77
--- /dev/null
+++ b/3rdParty/jargs-1.0/src/test/java/jargs/gnu/test/AllTests.java
@@ -0,0 +1,12 @@
+package gnu.jargs.test;
+
+import junit.framework.TestSuite;
+
+public class AllTests {
+ public static TestSuite suite() {
+ TestSuite s = new TestSuite();
+ s.addTestSuite(CmdLineParserTestCase.class);
+ s.addTestSuite(CustomOptionTestCase.class);
+ return s;
+ }
+}
diff --git a/3rdParty/jargs-1.0/src/test/java/jargs/gnu/test/CmdLineParserTestCase.java b/3rdParty/jargs-1.0/src/test/java/jargs/gnu/test/CmdLineParserTestCase.java
new file mode 100644
index 0000000..aa8d3d4
--- /dev/null
+++ b/3rdParty/jargs-1.0/src/test/java/jargs/gnu/test/CmdLineParserTestCase.java
@@ -0,0 +1,252 @@
+package gnu.jargs.test;
+
+import jargs.gnu.CmdLineParser;
+
+import java.util.Enumeration;
+import java.util.Locale;
+import java.util.Vector;
+
+import junit.framework.TestCase;
+
+public class CmdLineParserTestCase extends TestCase {
+
+ public CmdLineParserTestCase(String name) {
+ super(name);
+ }
+
+ public void testStandardOptions() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
+ CmdLineParser.Option size = parser.addIntegerOption('s', "size");
+ CmdLineParser.Option name = parser.addStringOption('n', "name");
+ CmdLineParser.Option fraction = parser.addDoubleOption('f', "fraction");
+ CmdLineParser.Option missing = parser.addBooleanOption('m', "missing");
+ CmdLineParser.Option careful = parser.addBooleanOption("careful");
+ CmdLineParser.Option bignum = parser.addLongOption('b', "bignum");
+ assertEquals(null, parser.getOptionValue(size));
+ Long longValue = new Long(new Long(Integer.MAX_VALUE).longValue() + 1);
+ parser.parse(new String[] { "-v", "--size=100", "-b",
+ longValue.toString(), "-n", "foo", "-f", "0.1", "rest" },
+ Locale.US);
+ assertEquals(null, parser.getOptionValue(missing));
+ assertEquals(Boolean.TRUE, parser.getOptionValue(verbose));
+ assertEquals(100, ((Integer) parser.getOptionValue(size)).intValue());
+ assertEquals("foo", parser.getOptionValue(name));
+ assertEquals(longValue, parser.getOptionValue(bignum));
+ assertEquals(0.1, ((Double) parser.getOptionValue(fraction))
+ .doubleValue(), 0.1e-6);
+ assertArrayEquals(new String[]{"rest"}, parser.getRemainingArgs());
+ }
+
+
+ public void testDefaults() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option boolean1 = parser.addBooleanOption("boolean1");
+ CmdLineParser.Option boolean2 = parser.addBooleanOption("boolean2");
+ CmdLineParser.Option boolean3 = parser.addBooleanOption("boolean3");
+ CmdLineParser.Option boolean4 = parser.addBooleanOption("boolean4");
+ CmdLineParser.Option boolean5 = parser.addBooleanOption("boolean5");
+
+ CmdLineParser.Option int1 = parser.addIntegerOption("int1");
+ CmdLineParser.Option int2 = parser.addIntegerOption("int2");
+ CmdLineParser.Option int3 = parser.addIntegerOption("int3");
+ CmdLineParser.Option int4 = parser.addIntegerOption("int4");
+
+ CmdLineParser.Option string1 = parser.addStringOption("string1");
+ CmdLineParser.Option string2 = parser.addStringOption("string2");
+ CmdLineParser.Option string3 = parser.addStringOption("string3");
+ CmdLineParser.Option string4 = parser.addStringOption("string4");
+
+ parser.parse(new String[] {
+ "--boolean1", "--boolean2",
+ "--int1=42", "--int2=42",
+ "--string1=Hello", "--string2=Hello",
+ });
+
+ assertEquals(Boolean.TRUE, parser.getOptionValue(boolean1));
+ assertEquals(Boolean.TRUE,
+ parser.getOptionValue(boolean2, Boolean.FALSE));
+ assertEquals(null, parser.getOptionValue(boolean3));
+ assertEquals(Boolean.FALSE,
+ parser.getOptionValue(boolean4, Boolean.FALSE));
+ assertEquals(Boolean.TRUE,
+ parser.getOptionValue(boolean5, Boolean.TRUE));
+
+ Integer forty_two = new Integer(42);
+ Integer thirty_six = new Integer(36);
+
+ assertEquals(forty_two, parser.getOptionValue(int1));
+ assertEquals(forty_two, parser.getOptionValue(int2, thirty_six));
+ assertEquals(null, parser.getOptionValue(int3));
+ assertEquals(thirty_six, parser.getOptionValue(int4, thirty_six));
+
+ assertEquals("Hello", parser.getOptionValue(string1));
+ assertEquals("Hello", parser.getOptionValue(string2, "Goodbye"));
+ assertEquals(null, parser.getOptionValue(string3));
+ assertEquals("Goodbye", parser.getOptionValue(string4, "Goodbye"));
+ }
+
+
+ public void testMultipleUses() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
+ CmdLineParser.Option foo = parser.addBooleanOption('f', "foo");
+ CmdLineParser.Option bar = parser.addBooleanOption('b', "bar");
+
+ parser.parse(new String[] {
+ "--foo", "-v", "-v", "--verbose", "-v", "-b", "rest"
+ });
+
+ int verbosity = 0;
+ while (true) {
+ Boolean b = (Boolean)parser.getOptionValue(verbose);
+
+ if (b == null) {
+ break;
+ }
+
+ if (b == Boolean.TRUE) {
+ verbosity++;
+ }
+ else {
+ assertEquals(Boolean.FALSE, b);
+ verbosity--;
+ }
+ }
+
+ assertEquals(4, verbosity);
+ }
+
+
+ public void testCombinedFlags() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option alt = parser.addBooleanOption('a', "alt");
+ CmdLineParser.Option debug = parser.addBooleanOption('d', "debug");
+ CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
+ parser.parse(new String[] {
+ "-dv"
+ });
+
+ assertEquals(null, parser.getOptionValue(alt));
+ assertEquals(Boolean.TRUE, parser.getOptionValue(debug));
+ assertEquals(Boolean.TRUE, parser.getOptionValue(verbose));
+ }
+
+
+ public void testExplictlyTerminatedOptions() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option alt = parser.addBooleanOption('a', "alt");
+ CmdLineParser.Option debug = parser.addBooleanOption('d', "debug");
+ CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
+ CmdLineParser.Option fraction = parser.addDoubleOption('f', "fraction");
+ parser.parse(new String[] {
+ "-a", "hello", "-d", "-f", "10", "--", "goodbye", "-v", "welcome",
+ "-f", "-10"
+ });
+
+ assertEquals(Boolean.TRUE, parser.getOptionValue(alt));
+ assertEquals(Boolean.TRUE, parser.getOptionValue(debug));
+ assertEquals(null, parser.getOptionValue(verbose));
+ assertEquals(new Double(10), parser.getOptionValue(fraction));
+
+ assertArrayEquals(
+ new String[]{"hello", "goodbye", "-v", "welcome", "-f", "-10"},
+ parser.getRemainingArgs());
+ }
+
+
+ public void testGetOptionValues() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
+ CmdLineParser.Option foo = parser.addBooleanOption('f', "foo");
+ CmdLineParser.Option bar = parser.addBooleanOption('b', "bar");
+
+ parser.parse(new String[] {
+ "--foo", "-v", "-v", "--verbose", "-v", "-b", "rest"
+ });
+
+ int verbosity = 0;
+ Vector v = parser.getOptionValues(verbose);
+ Enumeration e = v.elements();
+ while (e.hasMoreElements()) {
+ Boolean b = (Boolean)e.nextElement();
+
+ if (b == Boolean.TRUE) {
+ verbosity++;
+ }
+ else {
+ assertEquals(Boolean.FALSE, b);
+ verbosity--;
+ }
+ }
+
+ assertEquals(4, verbosity);
+ }
+
+
+ public void testBadFormat() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option size = parser.addIntegerOption('s', "size");
+ try {
+ parser.parse(new String[] { "--size=blah" });
+ fail("Expected IllegalOptionValueException");
+ } catch (CmdLineParser.IllegalOptionValueException e) {
+ // pass
+ }
+ }
+
+ public void testResetBetweenParse() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
+ parser.parse(new String[] { "-v" });
+ assertEquals(Boolean.TRUE, parser.getOptionValue(verbose));
+ parser.parse(new String[] {});
+ assertEquals(null, parser.getOptionValue(verbose));
+ }
+
+ public void testLocale() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option fraction = parser.addDoubleOption('f', "fraction");
+ parser.parse(new String[] { "--fraction=0.2" }, Locale.US);
+ assertEquals(0.2, ((Double) parser.getOptionValue(fraction))
+ .doubleValue(), 0.1e-6);
+ parser.parse(new String[] { "--fraction=0,2" }, Locale.GERMANY);
+ assertEquals(0.2, ((Double) parser.getOptionValue(fraction))
+ .doubleValue(), 0.1e-6);
+ }
+
+ public void testDetachedOption() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option detached = new CmdLineParser.Option.BooleanOption(
+ 'v', "verbose");
+ assertEquals(null, parser.getOptionValue(detached));
+ try {
+ parser.parse(new String[] { "-v" });
+ fail("UnknownOptionException expected");
+ } catch (CmdLineParser.UnknownOptionException e) {
+ // pass
+ }
+ assertEquals(null, parser.getOptionValue(detached));
+ }
+
+ public void testMissingValueForStringOption() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ parser.addBooleanOption('v', "verbose");
+ parser.addStringOption('c', "config");
+
+ try {
+ parser.parse(new String[] {"-v", "-c"});
+ fail();
+ } catch (CmdLineParser.IllegalOptionValueException e) {
+ }
+ }
+
+ private void assertArrayEquals(Object[] expected, Object[] actual) {
+ assertNotNull(actual);
+ assertEquals(expected.length, actual.length);
+ for (int i = 0; i < expected.length; ++i) {
+ assertEquals(expected[i], actual[i]);
+ }
+ }
+
+}
diff --git a/3rdParty/jargs-1.0/src/test/java/jargs/gnu/test/CustomOptionTestCase.java b/3rdParty/jargs-1.0/src/test/java/jargs/gnu/test/CustomOptionTestCase.java
new file mode 100644
index 0000000..336d162
--- /dev/null
+++ b/3rdParty/jargs-1.0/src/test/java/jargs/gnu/test/CustomOptionTestCase.java
@@ -0,0 +1,71 @@
+package gnu.jargs.test;
+
+import jargs.gnu.CmdLineParser;
+
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Locale;
+
+import junit.framework.TestCase;
+
+public class CustomOptionTestCase extends TestCase {
+
+ public CustomOptionTestCase(String name) {
+ super(name);
+ }
+
+ public void testCustomOption() throws Exception {
+ Calendar calendar = Calendar.getInstance();
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option date =
+ parser.addOption(new ShortDateOption('d', "date"));
+
+ parser.parse(new String[]{"-d", "11/03/2003"}, Locale.UK);
+ Date d = (Date)parser.getOptionValue(date);
+ calendar.setTime(d);
+ assertEquals(11, calendar.get(Calendar.DAY_OF_MONTH));
+ assertEquals(Calendar.MARCH, calendar.get(Calendar.MONTH));
+ assertEquals(2003, calendar.get(Calendar.YEAR));
+
+ parser.parse(new String[]{"-d", "11/03/2003"}, Locale.US);
+ d = (Date)parser.getOptionValue(date);
+ calendar.setTime(d);
+ assertEquals(3, calendar.get(Calendar.DAY_OF_MONTH));
+ assertEquals(Calendar.NOVEMBER, calendar.get(Calendar.MONTH));
+ assertEquals(2003, calendar.get(Calendar.YEAR));
+ }
+
+ public void testIllegalCustomOption() throws Exception {
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option date =
+ parser.addOption(new ShortDateOption('d', "date"));
+ try {
+ parser.parse(new String[]{"-d", "foobar"}, Locale.US);
+ fail("Expected IllegalOptionValueException");
+ }
+ catch (CmdLineParser.IllegalOptionValueException e) {
+ //pass
+ }
+ }
+
+ 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);
+ }
+ }
+ }
+
+
+}