Skip to content

Commit

Permalink
Backward compatible addition of generics
Browse files Browse the repository at this point in the history
This patch contains the minimal intrusive addition of generics to JArgs. This enables developers to use the new JArgs version as a drop in replacement without any modifications to exsting sources while benefitting from generics in new code; without the now unneeded typecasts, the user's code becomes more readable, emmits no unchecked warnings and averts typecast runtime errors.
  • Loading branch information
penSec.IT UG (haftungsbeschränkt) committed Sep 14, 2012
1 parent 11f5fe3 commit b3371be
Show file tree
Hide file tree
Showing 19 changed files with 1,370 additions and 119 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,3 +1,4 @@
/classes
/doc
/lib/jargs*.jar
/lib/jargs*.jar
*~
1 change: 1 addition & 0 deletions LICENCE
Expand Up @@ -2,6 +2,7 @@ Copyright (c) 2001-2003 Steve Purcell.
Copyright (c) 2002 Vidar Holen.
Copyright (c) 2002 Michal Ceresna.
Copyright (c) 2005 Ewan Mellor.
Copyright (c) 2010-2012 penSec.IT UG (haftungsbeschränkt).

All rights reserved.

Expand Down
8 changes: 5 additions & 3 deletions README.md
Expand Up @@ -5,6 +5,7 @@ JArgs command-line argument parsing library
- Copyright (c) 2002 Vidar Holen.
- Copyright (c) 2002 Michal Ceresna.
- Copyright (c) 2005 Ewan Mellor.
- Copyright (c) 2010-2012 penSec.IT UG (haftungsbeschränkt).

All rights reserved.

Expand All @@ -21,7 +22,7 @@ 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 (3.7 & 4.3.1), by Eric Gamma, et al, from http://www.junit.org/.
JUnit is used to run the unit tests, and is not needed to run the library
itself.

Expand All @@ -45,8 +46,9 @@ Documentation
-------------

The main documentation is the detailed worked example in
`src/jargs/examples/gnu/OptionTest.java`, plus the generated API documentation
in `doc/api/`.
`src/jargs/examples/gnu/OptionTest.java` (old Java 1.1 API),
`src/jargs/examples/gnu/OptionTest5.java` (new Java 1.5 API), plus the generated
API documentation in `doc/api/`.


Package contents
Expand Down
20 changes: 12 additions & 8 deletions build.xml
Expand Up @@ -3,15 +3,15 @@
<property name="debug" value="on"/>
<property name="build.src" value="src"/>
<property name="build.classes" value="classes"/>
<property name="source-version" value="1.2" />
<property name="jdk-version" value="1.1" />
<property name="source-version" value="1.5" />
<property name="jdk-version" value="1.5" />
<property name="javadoc.outdir" value="doc/api"/>
<property name="javadoc.doctitle" value="JArgs command line option parsing library"/>
<property name="javadoc.header" value='For updates and more see &lt;a target="_top" href="http://jargs.sourceforge.net/"&gt;jargs.sourceforge.net&lt;/a&gt;'/>
<property name="javadoc.bottom" value='Copyright &amp;copy; 2001-2003 Steve
Purcell. Copyright &amp;copy; 2002 Vidar Holen. Copyright &amp;copy; 2002 Michal Ceresna. Copyright &amp;copy; 2005 Ewan Mellor. Released under the terms of the BSD licence.'/>
<property name="javadoc.bottom" value='Copyright &amp;copy; 2001-2003 Steve Purcell. Copyright &amp;copy; 2002 Vidar Holen. Copyright &amp;copy; 2002 Michal Ceresna. Copyright &amp;copy; 2005 Ewan Mellor. Copyright (c) 2010-2012 penSec.IT UG (haftungsbeschränkt). Released under the terms of the BSD licence.'/>
<property name="javadoc.packages" value="jargs.gnu"/>
<property name="junit.jar" value="/usr/share/java/junit.jar"/>
<property name="junit3.jar" value="/usr/share/java/junit.jar"/>
<property name="junit4.jar" value="/usr/share/java/junit4.jar"/>

<target name="build" description="Build and test Jargs."
depends="compile,jars,javadoc,test" />
Expand All @@ -30,7 +30,7 @@ Purcell. Copyright &amp;copy; 2002 Vidar Holen. Copyright &amp;copy; 2002 Micha
<mkdir dir="classes"/>
<javac destdir="${build.classes}" debug="${debug}"
source="${source-version}" target="${jdk-version}">
<classpath path="${build.classes}:${junit.jar}:${java.class.path}"/>
<classpath path="${build.classes}:${junit3.jar}:${junit4.jar}:${java.class.path}"/>
<src path="${build.src}/jargs/test" />
</javac>
</target>
Expand Down Expand Up @@ -62,9 +62,13 @@ Purcell. Copyright &amp;copy; 2002 Vidar Holen. Copyright &amp;copy; 2002 Micha

<target name="test" depends="compile,compile-test">
<java classname="junit.textui.TestRunner"
classpath="${build.classes}:${junit.jar}:${java.class.path}">
classpath="${build.classes}:${junit3.jar}:${java.class.path}">
<arg value="jargs.test.gnu.AllTests"/>
</java>
<java classname="org.junit.runner.JUnitCore"
classpath="${build.classes}:${junit4.jar}:${java.class.path}" >
<arg value="jargs.test.gnu.AllTests5"/>
</java>
</target>

<target name="testgui" depends="compile,compile-test">
Expand Down Expand Up @@ -98,7 +102,7 @@ Purcell. Copyright &amp;copy; 2002 Vidar Holen. Copyright &amp;copy; 2002 Micha
footer="${javadoc.header}"
bottom="${javadoc.bottom}">
<package name="**.*" />
<classpath path="${build.classes}:${junit.jar}:${java.class.path}"/>
<classpath path="${build.classes}:${junit3.jar}:${junit4.jar}:${java.class.path}"/>
</javadoc>
</target>

Expand Down
32 changes: 32 additions & 0 deletions src/jargs/examples/gnu/AutoHelpParser.java
@@ -1,3 +1,35 @@
/**
* Copyright (c) 2001-2003 Steve Purcell.
* Copyright (c) 2002 Vidar Holen.
* Copyright (c) 2002 Michal Ceresna.
* Copyright (c) 2005 Ewan Mellor.
* Copyright (c) 2010-2012 penSec.IT UG (haftungsbeschränkt).
*
* 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.
*/

package jargs.examples.gnu;

import jargs.gnu.CmdLineParser;
Expand Down
117 changes: 117 additions & 0 deletions src/jargs/examples/gnu/AutoHelpParser5.java
@@ -0,0 +1,117 @@
/**
* Copyright (c) 2001-2003 Steve Purcell.
* Copyright (c) 2002 Vidar Holen.
* Copyright (c) 2002 Michal Ceresna.
* Copyright (c) 2005 Ewan Mellor.
* Copyright (c) 2010-2012 penSec.IT UG (haftungsbeschränkt).
*
* 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.
*/

package jargs.examples.gnu;

import jargs.gnu.CmdLineParser;

import java.util.ArrayList;
import java.util.List;

/**
* This example shows how to dynamically create basic output for a --help option.
*/
public class AutoHelpParser5 extends CmdLineParser {

List<String> optionHelpStrings = new ArrayList<String>();

public <T> Option<T> addHelp(Option<T> option, String helpString) {
optionHelpStrings.add(" -" + option.shortForm() + "/--" + option.longForm() + ": " + helpString);
return option;
}

public void printUsage() {
System.err.println("usage: prog [options]");
for (String help : optionHelpStrings) {
System.err.println(help);
}
}

public static void main(String[] args) {
AutoHelpParser5 parser = new AutoHelpParser5();
CmdLineParser.Option<Boolean> verbose = parser.addHelp(
parser.addBooleanOption('v', "verbose"),
"Print extra information");
CmdLineParser.Option<Integer> size = parser.addHelp(
parser.addIntegerOption('s', "size"),
"The extent of the thing");
CmdLineParser.Option<String> name = parser.addHelp(
parser.addStringOption('n', "name"),
"Name given to the widget");
CmdLineParser.Option<Double> fraction = parser.addHelp(
parser.addDoubleOption('f', "fraction"),
"What percentage should be discarded");
CmdLineParser.Option<Boolean> 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 = parser.getOptionValue(verbose);
Integer sizeValue = parser.getOptionValue(size);
String nameValue = parser.getOptionValue(name);
Double fractionValue = 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);
}
}
32 changes: 32 additions & 0 deletions src/jargs/examples/gnu/CustomOptionTest.java
@@ -1,3 +1,35 @@
/**
* Copyright (c) 2001-2003 Steve Purcell.
* Copyright (c) 2002 Vidar Holen.
* Copyright (c) 2002 Michal Ceresna.
* Copyright (c) 2005 Ewan Mellor.
* Copyright (c) 2010-2012 penSec.IT UG (haftungsbeschränkt).
*
* 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.
*/

package jargs.examples.gnu;

import jargs.gnu.CmdLineParser;
Expand Down
104 changes: 104 additions & 0 deletions src/jargs/examples/gnu/CustomOptionTest5.java
@@ -0,0 +1,104 @@
/**
* Copyright (c) 2001-2003 Steve Purcell.
* Copyright (c) 2002 Vidar Holen.
* Copyright (c) 2002 Michal Ceresna.
* Copyright (c) 2005 Ewan Mellor.
* Copyright (c) 2010-2012 penSec.IT UG (haftungsbeschränkt).
*
* 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.
*/

package 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 CustomOptionTest5 {

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<Date> {

public ShortDateOption(char shortForm, String longForm) {
super(shortForm, longForm, true);
}

@Override
protected Date 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> 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 = 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);
}
}

0 comments on commit b3371be

Please sign in to comment.