Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backward compatible addition of generics
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
Showing
19 changed files
with
1,370 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/classes | ||
/doc | ||
/lib/jargs*.jar | ||
/lib/jargs*.jar | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.