_JAVA COMMAND-LINE ARGUMENTS_ by Greg White Listing One // -- Declare this source to be a part of the CmdLnArg package. package CmdLnArg; // -- Make use of shipped package for Vector, String, StringBuffer classes import java.util.*; public class ArgProcessor { // -- Args as passed into application String args[] = null; // -- All arg types we will process. Must be derived from GenericArgType. Vector vArgs = new Vector(); // -- Constructor. Hang on to the arguments for when we need to // -- process them later. public ArgProcessor ( String args[] ) { this.args = args; } // -- end constructor // -- Adds one argument type for when we process public void add ( GenericArgType arg ) { vArgs.addElement(arg); } // -- end add // -- Process the args that were passed into the application and store // -- the results back in the argument types passed into the add method public void process ( ) throws Exception { int iArgIndex = 0; // -- Loop through all of the args passed into the application while (iArgIndex < args.length) { String thisArg = args[iArgIndex++]; GenericArgType arg = getArg(thisArg); // -- Set the argument and (possibly) increase the arg index. if (arg != null) iArgIndex = arg.set(args, iArgIndex); } // -- end while } // -- end process // -- Look for an argument type that matches the passed-in target protected GenericArgType getArg ( String argTarget ) { Enumeration allArgs = vArgs.elements(); GenericArgType foundArg = null; // -- Loop through each arg type until we find a match or there are // -- no more arg types. while (foundArg==null && allArgs.hasMoreElements()) { GenericArgType testArg = (GenericArgType)allArgs.nextElement(); if (testArg.argID.equalsIgnoreCase(argTarget)) foundArg = testArg; } // -- end while return foundArg; } // -- end getArg } // -- end class ArgProcessor Listing Two // -- Declare this source to be a part of the CmdLnArg package. package CmdLnArg; // -- Make use of shipped packages for Vector, String, StringBuffer classes import java.util.*; public abstract class GenericArgType { public String argID = null; protected boolean wasSet; public GenericArgType ( String argID ) { this.argID = argID; wasSet = false; } // -- end constructor public boolean wasSet ( ) { return wasSet; } // -- end wasSet public abstract int set ( String args[], int iThisIndex ) throws Exception ; } // -- end GenericArgType Listing Three // -- Declare this source to be a part of the CmdLnArg package. package CmdLnArg; // -- Make use of shipped packages for Vector, String, StringBuffer classes import java.util.*; public class WordArgType extends GenericArgType { public boolean argDest; public WordArgType ( String argID, boolean argDest ) { super(argID); this.argDest = argDest; } // -- end constructor public int set ( String args[], int iThisIndex ) { if (!wasSet) { argDest = !argDest; wasSet = true; } // -- end if return iThisIndex; } // -- end set } // -- end class WordArgType Listing Four // -- Declare this source to be a part of the CmdLnArg package. package CmdLnArg; // -- Make use of shipped packages for Vector, String, StringBuffer classes import java.util.*; public class ArgReqArgType extends GenericArgType { public StringBuffer argDest = null; public ArgReqArgType ( String argID, StringBuffer argDest ) { super(argID); this.argDest = argDest; } // -- end constructor public int set ( String args[], int iThisIndex ) throws Exception { if (args.length <= iThisIndex) throw new Exception("Command line param " + argID + " must be followed by a string"); // -- We can't directly change the string, so truncate it and add // -- what we want. argDest.setLength(0); argDest.append(args[iThisIndex]); wasSet = true; return iThisIndex + 1; } // -- end set } // -- end class ArgReqArgType Listing Five // -- Declare this source to be a part of the CmdLnArg package. import CmdLnArg.*; // -- Make use of shipped packages for Vector, String, StringBuffer, // -- System classes import java.util.*; import java.io.*; class Go { // -- Set by cmd line arguments static boolean bVerbose = false; static StringBuffer inputName = new StringBuffer(""); static StringBuffer outputName = new StringBuffer(""); static StringBuffer to = new StringBuffer("TextWriter"); static StringBuffer sWidth = new StringBuffer(""); static StringBuffer sTabSize = new StringBuffer(""); static Vector vArgs = new Vector(); public static void main ( String args[] ) { int iReturn = 0; try { processArgs(args); } // -- end try catch (Exception e) { e.printStackTrace(); iReturn = 1; } // -- end catch System.exit(iReturn); } // -- end main static void processArgs ( String args[] ) throws Exception { ArgProcessor processor = new ArgProcessor(args); add(processor, new ArgReqArgType("-in", inputName)); add(processor, new ArgReqArgType("-out", outputName)); add(processor, new ArgReqArgType("-to", to)); add(processor, new ArgReqArgType("-width", sWidth)); add(processor, new ArgReqArgType("-tabsize", sTabSize)); WordArgType verboseArg = new WordArgType("-verbose", false); add(processor, verboseArg); WordArgType includeRefs = new WordArgType("-NoRefs", true); add(processor, includeRefs); WordArgType refSections = new WordArgType("-NoSections", true); add(processor, refSections); WordArgType refPage = new WordArgType("-NoPages", true); add(processor, refPage); WordArgType horzRule = new WordArgType("-HR", true); add(processor, horzRule); processor.process(); checkSomethingSet(); // -- Print the results back to the user. System.out.println("Input name:" + inputName); System.out.println("Output name:" + outputName); System.out.println("to:" + to); System.out.println("width:" + sWidth); System.out.println("tabsize:" + sTabSize); if (verboseArg.wasSet()) System.out.print("Verbose was set to "); else System.out.print("Verbose left at the default of "); System.out.println(String.valueOf(verboseArg.argDest)); if (includeRefs.wasSet()) System.out.print("IncludeRefs was set to "); else System.out.print("IncludeRefs left at the default of "); System.out.println(String.valueOf(includeRefs.argDest)); if (refSections.wasSet()) System.out.print("RefSections was set to "); else System.out.print("RefSections left at the default of "); System.out.println(String.valueOf(refSections.argDest)); if (refPage.wasSet()) System.out.print("RefPages was set to "); else System.out.print("RefPages left at the default of "); System.out.println(String.valueOf(refPage.argDest)); if (horzRule.wasSet()) System.out.print("HR was set to "); else System.out.print("HR left at the default of "); System.out.println(String.valueOf(horzRule.argDest)); } // -- end processArgs // -- Adds an argument type to the processor and to our vector so we can // -- later make sure somethine was set. protected static void add ( ArgProcessor processor, GenericArgType argType ) { processor.add(argType); vArgs.addElement(argType); } // -- end add // -- Prints info and generates and exception of nothing is set protected static void checkSomethingSet ( ) throws Exception { Enumeration allArgs = vArgs.elements(); boolean bAnythingSet = false; while (bAnythingSet==false && allArgs.hasMoreElements()) { GenericArgType thisArg = (GenericArgType)allArgs.nextElement(); if (thisArg.wasSet()) bAnythingSet = true; } // -- end while if (!bAnythingSet) { System.out.println("Valid arguments requiring arguments:"); System.out.println(" -in"); System.out.println(" -out"); System.out.println(" -to"); System.out.println(" -width"); System.out.println(" -tabsize"); System.out.println(); System.out.println("Valid word arguments:"); System.out.println(" -verbose"); System.out.println(" -NoRefs"); System.out.println(" -NoSections"); System.out.println(" -NoPages"); System.out.println(" -HR"); throw new Exception("No valid command-line arguments found"); } // -- end if } // -- end checkSomethingSet } // -- end class Go Example 1: javac GenericArgType.java javac WordArgType.java javac ArgReqArgType.java javac ArgProcessor.java javac Go.java Example 2: (a) ArgProcessor processor = new ArgProcessor(args) (b) WordArgType verboseArg = new WordArgType("-verbose", false); processor.add(verboseArg); (c) StringBuffer inputName = new StringBuffer(""); processor.add(new ArgReqArgType("-in", inputName); (d) processor.process(); (e) boolean bVerbose = verboseArg.argDest; (f) String s = inputName.toString();