Documentation of 'org.clapper.util.cmdline.CommandLineUtility' Java class
CommandLineUtility
org.clapper.util.cmdline

Class CommandLineUtility

  • Direct Known Subclasses:
    RollingFile, SplitString, TestDuration, TestFileHashMap, TestLRUMap, TestMIMEType, TestMultiIterator, TestNestedException, TestPropertiesMap, TestRegexUtil, TestSemaphore, XString


    public abstract class CommandLineUtility
    extends java.lang.Object

    CommandLineUtility is an abstract base class for command-line utilities. It provides:

    • Parameter-parsing logic, with call-outs for custom parameters
    • Built-in support for a --logging parameter, which enables logging via the java.util.logging API, by calling Logger.enableLogging()
    • Automatic generation of a usage message, with a call-out that permits subclasses to add subclass-specific usage information.
    • Automatic reporting of exceptions
    To use this class, subclass it, and have the subclass's main() method instantiate the subclass, and then call the resulting object's execute() method. The execute() method (which resides in this base class) will:

    • parse the parameters
    • call the runCommand() method, which must be provided by the subclass, to initiate processing

    Note that this class does not parse options the way GNU getopt() (or even traditional getopt()) does. In particular, it does not permit combining multiple single-character options into one command-line parameter. CommandLineUtility may be extended to support that capability in the future; however, it doesn't do that yet.

    Here's a sample subclass. It takes the usual --logging parameter, plus a -v (verbose) flag, a numeric count (-n) and a file name. (Exactly what it does with those parameters is left as an exercise for the reader.)

     public class Foo extends CommandLineUtility
     {
         private boolean verbose  = false;
         private int     count    = 1;
         private String  filename = null;
    
         public static void main (String[] args)
         {
             try
             {
                 Foo foo = new Foo();
                 foo.execute (args);
             }
    
             catch (CommandLineUsageException ex)
             {
                 // Already reported
    
                 System.exit (1);
             }
    
             catch (CommandLineException ex)
             {
                 System.err.println (ex.getMessage());
                 System.exit (1);
             }
    
             System.exit (0);
         }
    
         private Foo()
         {
             super();
         }
    
         protected void runCommand() throws CommandLineUtilityException
         {
             ...
         }
    
         protected void parseCustomOption (char shortOption,
                                           String longOption,
                                           Iterator<String> it)
             throws CommandLineUsageException,
                    NoSuchElementException
         {
             if (longOption.equals ("verbose") || (shortOption == 'v'))
                 verbose = true;
    
             else if (longOption.equals ("count") || (shortOption == 'n'))
                 count = parseIntParameter ((String) it.next());
    
             else
                 throw new CommandLineUsageException ("Unknown option: " + option);
         }
    
         protected void processPostOptionCommandLine (Iterator<String> it)
             throws CommandLineUsageException,
                    NoSuchElementException
         {
             filename = it.next();
         }
    
         protected void getCustomUsageInfo (UsageInfo info)
         {
             info.addOption ('v', "verbose", null, "Enable verbose messages")
             info.addOption ('n', "count", "total", "Read specified file  times. Defaults to 1.");
             info.addParameter ("filename", "File to process.", false);
         }
     }
     
    See Also:
    ParameterParser
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      void execute(java.lang.String[] args)
      Called to initiate execution of the command line utility.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • execute

        public final void execute(java.lang.String[] args)
                           throws CommandLineException
        Called to initiate execution of the command line utility. This method
        • parse the parameters
        • call the runCommand() method, which must be provided by the subclass, to initiate processing.
        Parameters:
        args - The command-line parameters
        Throws:
        CommandLineException - command failed

DMelt 3.0 © DataMelt by jWork.ORG

You see the box below because you did not login.