summaryrefslogtreecommitdiff
path: root/3rdParty/jargs-1.0/examples/gnu/CustomOptionTest.java
diff options
context:
space:
mode:
Diffstat (limited to '3rdParty/jargs-1.0/examples/gnu/CustomOptionTest.java')
-rw-r--r--3rdParty/jargs-1.0/examples/gnu/CustomOptionTest.java73
1 files changed, 73 insertions, 0 deletions
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);
+ }
+
+}