Next: Idiosyncrasies, Previous: C++ Output, Up: Making Genparse Files [Contents][Index]
This section shows the Java output and how to interface a Java program with a command line parsing class created by Genparse.
For Java, Genparse creates a Java interface, a parser class that implements
it and an exception class that may be thrown by the parser. A separate output
file is written for each of them. We’ll use mycopy5.gp
as input to
create the two output files. Genparse generates Java code as follows.
genparse -l java -o Cmdline mycopy5.gp
The three created files are named, CmdlineInterface.java
,
Cmdline.java
and CmdlineEx.java
. We’ll walk through each one
in turn.
• Java Interface: | ||
• Java Implementation: | ||
• Java Exception Class: | ||
• Main Java Program: |
Next: Java Implementation, Up: Java Output [Contents][Index]
/* CmdlineInterface.java */ /*---------------------------------------------------------------------------- ** ** interface CmdlineInterface ** ** Interface of the command line parser class ** **--------------------------------------------------------------------------*/ public interface CmdlineInterface { /* usage function */ void usage (int status, String program_name); /* return next (non-option) parameter */ int next_param (); /* callback functions */ boolean my_callback (); boolean outfile_cb (); /* getter functions for command line parameters */ int i (); String o (); boolean h (); boolean v (); };
Each parameter value is stored in a private member variable, and must be accessed through a call to the member function named with the short form of the option. If Genparse was called with the option --longmembers then this function is named by the long form of the option.
A copy of the optind
variable is stored in a private member
variable, and is accessible through the next_param ()
member
function.
Next: Java Exception Class, Previous: Java Interface, Up: Java Output [Contents][Index]
/* Cmdline.java */ import gnu.getopt.LongOpt; import gnu.getopt.Getopt; /*---------------------------------------------------------------------------- ** ** class Cmdline () ** ** Command line parser class. ** **--------------------------------------------------------------------------*/ public class Cmdline implements CmdlineInterface { /* parameters */ private int _i; private String _o; private boolean _h; private boolean _v; /* Name of the calling program */ private String _executable; /* next (non-option) parameter */ private int _optind; /* Must be constructed with parameters. */ public Cmdline (String[] argv) throws CmdlineEx { /* character returned by optind () */ int c; LongOpt[] longopts = new LongOpt[4]; longopts[0] = new LongOpt ("iterations", LongOpt.REQUIRED_ARGUMENT, null, 'i'); longopts[1] = new LongOpt ("outfile", LongOpt.REQUIRED_ARGUMENT, null, 'o'); longopts[2] = new LongOpt ("help", LongOpt.NO_ARGUMENT, null, 'h'); longopts[3] = new LongOpt ("version", LongOpt.NO_ARGUMENT, null, 'v'); _executable = Cmdline.class.getName (); /* default values */ _i = 1; _h = false; _v = false; Getopt g = new Getopt (_executable, argv, "i:o:hv", longopts); while ((c = g.getopt ()) != -1) { switch (c) { case 'i': _i = Integer.parseInt (g.getOptarg ()); if (_i < 1) throw new CmdlineEx ("parameter range error: i must be >= 1"); if (_i > 10) throw new CmdlineEx ("parameter range error: i must be <= 10"); break; case 'o': _o = g.getOptarg (); if (!outfile_cb ()) usage (-1, _executable); break; case 'h': _h = true; usage (0, _executable); break; case 'v': _v = true; break; default: usage (-1, _executable); } } /* while */ _optind = g.getOptind (); if (!my_callback ()) usage (-1, _executable); } public void usage (int status, String program_name) { if (status != 0) { System.err.println ("Try `" + program_name + " --help' for more information."); } else { System.out.println ( "usage: " + program_name + " [ -iohv ] file\n" + "Print a file for a number of times to stdout.\n" + " [ -i ] [ --iterations ] (type=INTEGER, range=1...10, default=1)\n" + " Number of times to output <file>.\n" + " do it like this\n" + " [ -o ] [ --outfile ] (type=STRING)\n" + " Output file.\n" + " [ -h ] [ --help ] (type=FLAG)\n" + " Display this help and exit.\n" + " [ -v ] [ --version ] (type=FLAG)\n" + " Output version information and exit."); } System.exit (status); } /* return next (non-option) parameter */ public int next_param () { return _optind; } /* Callback functions */ /* Derive your own class and overwrite any of the callback */ /* functions if you need customized callbacks. */ public boolean my_callback () { return true; } public boolean outfile_cb () { return true; } /* getter functions for command line parameters */ public int i () { return _i; } public String o () { return _o; } public boolean h () { return _h; } public boolean v () { return _v; } }
There is no default constructor. Instead the parser class must be
constructed with an array of strings which is usually the argument
to the main ()
function.
The constructor throws an exception if command line parsing fails. The exception type is defined in See Java Exception Class.
Note that no separate callback file is generated like for C or C++. The parser class contains default implementations for each of the callback functions, if you need customized callbacks then simply derive your own class from the generated parser class.
Next: Main Java Program, Previous: Java Implementation, Up: Java Output [Contents][Index]
/* CmdlineEx.java */ /*---------------------------------------------------------------------------- ** ** class CmdlineEx ** ** Exception class thrown by the command line parser class ** **--------------------------------------------------------------------------*/ public class CmdlineEx extends RuntimeException { public CmdlineEx (String text) { super (text); } }
This exception class is always the same of course. Only the name of the class would change if –parsefunc was set with a different name than Cmdline.
Previous: Java Exception Class, Up: Java Output [Contents][Index]
The example code for Java is very similar to the C++ example (See Main C++ Program.).
/* mycopy5.java */ import gnu.getopt.LongOpt; import gnu.getopt.Getopt; import java.io.*; class mycopy5 { public static void main (String args[]) { final String _executable = "mycopy5"; /* Don't know how to set this automatically in Java */ final String VERSION = "3.0"; final int EOF = - 1; /* Is EOF predefined in Java? */ int i; int c; int length; OutputStreamWriter output_file; InputStreamReader input_file; boolean ofile = false, ifile = false; Cmdline cl = new Cmdline (args); if (cl.v ()) { System.out.println (_executable + " version " + VERSION); System.exit (0); } try { length = cl.o ().length (); if (length != 0) { output_file = new OutputStreamWriter (new FileOutputStream (cl.o ())); ofile = true; } else { output_file = new OutputStreamWriter (System.out); } if (cl.next_param () != 0) { input_file = new InputStreamReader (new FileInputStream (args[cl.next_param ()])); ifile = true; } else { input_file = new InputStreamReader (System.in); } for (i = 0; i < cl.i (); i++) { c = input_file.read (); while (c != EOF) { output_file.write (c); c = input_file.read (); } if (ifile) { input_file.close (); input_file = new InputStreamReader (new FileInputStream (args[cl.next_param ()])); } } input_file.close (); output_file.close (); } catch (IOException ex) { System.out.println ("File I/O error: " + ex); } System.exit (0); } }
Previous: Java Exception Class, Up: Java Output [Contents][Index]