summaryrefslogtreecommitdiff
path: root/3rdParty/jargs-1.0/examples
diff options
context:
space:
mode:
Diffstat (limited to '3rdParty/jargs-1.0/examples')
-rw-r--r--3rdParty/jargs-1.0/examples/gnu/AutoHelpParser.java87
-rw-r--r--3rdParty/jargs-1.0/examples/gnu/CustomOptionTest.java73
-rw-r--r--3rdParty/jargs-1.0/examples/gnu/OptionParserSubclassTest.java71
-rw-r--r--3rdParty/jargs-1.0/examples/gnu/OptionTest.java139
4 files changed, 370 insertions, 0 deletions
diff --git a/3rdParty/jargs-1.0/examples/gnu/AutoHelpParser.java b/3rdParty/jargs-1.0/examples/gnu/AutoHelpParser.java
new file mode 100644
index 0000000..f42ec5f
--- /dev/null
+++ b/3rdParty/jargs-1.0/examples/gnu/AutoHelpParser.java
@@ -0,0 +1,87 @@
+package 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);
+ }
+
+}
diff --git a/3rdParty/jargs-1.0/examples/gnu/CustomOptionTest.java b/3rdParty/jargs-1.0/examples/gnu/CustomOptionTest.java
new file mode 100644
index 0000000..2a1c45f
--- /dev/null
+++ b/3rdParty/jargs-1.0/examples/gnu/CustomOptionTest.java
@@ -0,0 +1,73 @@
+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 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);
+ }
+
+}
diff --git a/3rdParty/jargs-1.0/examples/gnu/OptionParserSubclassTest.java b/3rdParty/jargs-1.0/examples/gnu/OptionParserSubclassTest.java
new file mode 100644
index 0000000..551abf1
--- /dev/null
+++ b/3rdParty/jargs-1.0/examples/gnu/OptionParserSubclassTest.java
@@ -0,0 +1,71 @@
+package 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; j<allOptions.length; ++j ) {
+ System.out.println(allOptions[j].longForm() + ": " +
+ myOptions.getOptionValue(allOptions[j]));
+ }
+
+ String[] otherArgs = myOptions.getRemainingArgs();
+ System.out.println("remaining args: ");
+ for ( int i = 0; i<otherArgs.length; ++i ) {
+ System.out.println(otherArgs[i]);
+ }
+ System.exit(0);
+ }
+
+}
diff --git a/3rdParty/jargs-1.0/examples/gnu/OptionTest.java b/3rdParty/jargs-1.0/examples/gnu/OptionTest.java
new file mode 100644
index 0000000..9b38b04
--- /dev/null
+++ b/3rdParty/jargs-1.0/examples/gnu/OptionTest.java
@@ -0,0 +1,139 @@
+package jargs.examples.gnu;
+
+import java.util.Enumeration;
+import java.util.Vector;
+
+import jargs.gnu.CmdLineParser;
+
+
+public class OptionTest {
+
+ private static void printUsage() {
+ System.err.println(
+"Usage: OptionTest [-d,--debug] [{-v,--verbose}] [{--alt}] [{--name} a_name]\n" +
+" [{-s,--size} a_number] [{-f,--fraction} a_float] [a_nother]");
+ }
+
+ public static void main( String[] args ) {
+
+ // First, you must create a CmdLineParser, and add to it the
+ // appropriate Options.
+
+ // To start with, we add the Options -d, -v, -s, and -f, with aliases
+ // --debug, --verbose, --size, and --fraction respectively.
+
+ // The -d and -v options have no associated value -- they are either
+ // present, or they are not. The -s and -f options take integer and
+ // double-precision floating-point values respectively.
+
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option debug = parser.addBooleanOption('d', "debug");
+ CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
+ CmdLineParser.Option size = parser.addIntegerOption('s', "size");
+ CmdLineParser.Option fraction = parser.addDoubleOption('f', "fraction");
+
+ // Options may have just a long form with no corresponding short form.
+ // Here, we add --alt and --name options.
+
+ CmdLineParser.Option alt = parser.addBooleanOption("alt");
+ CmdLineParser.Option name = parser.addStringOption("name");
+
+
+ // Next, you must parse the user-provided command line arguments, and
+ // catch any errors therein.
+
+ // Options may appear on the command line in any order, and may even
+ // appear after some or all of the non-option arguments.
+
+ // If the user needs to specify non-option arguments that start with a
+ // minus, then they may indicate the end of the parsable options with
+ // -- , like this:
+
+ // prog -f 20 -- -10 -fred
+
+ // The -f 20 will be parsed as the fraction option, with the value 20.
+ // The -10 and -fred arguments will be regarded as non-option
+ // arguments, and passed through getRemainingArgs as unparsed Strings.
+
+ // Short boolean options may be specified separately (-d -v) or
+ // together (-dv).
+
+ // Options with values may be given on the command line as -f 1.0 or
+ // --fraction=1.0.
+
+ try {
+ parser.parse(args);
+ }
+ catch ( CmdLineParser.OptionException e ) {
+ System.err.println(e.getMessage());
+ printUsage();
+ System.exit(2);
+ }
+
+
+ // For options that may be specified only zero or one time, the value
+ // of that option may be extracted as shown below. If the options
+ // were not specified, the corresponding values will be null.
+
+ Boolean debugValue = (Boolean)parser.getOptionValue(debug);
+ String nameValue = (String)parser.getOptionValue(name);
+
+ // Alternatively, you may specify a default value. This will be
+ // returned (instead of null) when the command line argument is
+ // missing.
+
+ Boolean altValue =
+ (Boolean)parser.getOptionValue(alt, Boolean.FALSE);
+ Integer sizeValue =
+ (Integer)parser.getOptionValue(size, new Integer(42));
+
+ // If your application requires it, options may be specified more than
+ // once. In this case, you may get all the values specified by the
+ // user, as a Vector:
+
+ Vector fractionValues = parser.getOptionValues(fraction);
+
+ // Alternatively, you may make the loop explicit:
+
+ int verbosity = 0;
+ while (true) {
+ Boolean verboseValue = (Boolean)parser.getOptionValue(verbose);
+
+ if (verboseValue == null) {
+ break;
+ }
+ else {
+ verbosity++;
+ }
+ }
+
+ // The remaining command-line arguments -- those that do not start
+ // with a minus sign -- can be captured like this:
+
+ String[] otherArgs = parser.getRemainingArgs();
+
+
+ // For testing purposes, we just print out the option values and
+ // remaining command-line arguments. In a real program, of course,
+ // one would pass them to a function that does something more useful.
+
+ System.out.println("debug: " + debugValue);
+ System.out.println("alt: " + altValue);
+ System.out.println("size: " + sizeValue);
+ System.out.println("name: " + nameValue);
+
+ System.out.println("verbosity: " + verbosity);
+
+ Enumeration e = fractionValues.elements();
+ while (e.hasMoreElements()) {
+ System.out.println("fraction: " + (Double)e.nextElement());
+ }
+
+ System.out.println("remaining args: ");
+ for ( int i = 0; i < otherArgs.length; ++i ) {
+ System.out.println(otherArgs[i]);
+ }
+
+ System.exit(0);
+ }
+}