org.clapper.util.cmdline
Class CommandLineUtility
- java.lang.Object
-
- org.clapper.util.cmdline.CommandLineUtility
-
- Direct Known Subclasses:
- RollingFile, SplitString, TestDuration, TestFileHashMap, TestLRUMap, TestMIMEType, TestMultiIterator, TestNestedException, TestPropertiesMap, TestRegexUtil, TestSemaphore, XString
public abstract class CommandLineUtility extends java.lang.ObjectCommandLineUtility 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
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 filetimes. 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 voidexecute(java.lang.String[] args)Called to initiate execution of the command line utility.
-
-
-
Method Detail
-
execute
public final void execute(java.lang.String[] args) throws CommandLineExceptionCalled 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