edu.cmu.minorthird.util
Class BasicCommandLineProcessor

java.lang.Object
  extended by edu.cmu.minorthird.util.BasicCommandLineProcessor
All Implemented Interfaces:
CommandLineProcessor, Saveable
Direct Known Subclasses:
ClassifyCommandLineUtil, ClassifyCommandLineUtil.BaseParams, ClassifyCommandLineUtil.Learner, CommandLineUtil.AnnotatorOutputParams, CommandLineUtil.BaseParams, CommandLineUtil.ClassificationSignalParams, CommandLineUtil.EditParams, CommandLineUtil.ExtractionSignalParams, CommandLineUtil.GUIParams, CommandLineUtil.LoadAnnotatorParams, CommandLineUtil.MixupParams, CommandLineUtil.MultiClassificationSignalParams, CommandLineUtil.OnlineBaseParams, CommandLineUtil.OnlineLearnerParams, CommandLineUtil.OnlineSignalParams, CommandLineUtil.SaveParams, CommandLineUtil.SplitterParams, CommandLineUtil.TaggerSignalParams, CommandLineUtil.TrainClassifierParams, CommandLineUtil.TrainExtractorParams, CommandLineUtil.TrainTaggerParams, CommandLineUtil.ViewLabelsParams, ExtractorNameMatcher.MyCLP, ExtractorTweaker.MyCLP, LabeledDirectory.MyCLP, PreprocessTextForClassifier.LinkFileParams, PreprocessTextForExtractor.ExtractionReductionParams, RankingEvaluation.MyCLP, RankingExpt.MyCLP, Recommended.HMMTokenFE.MyCLP, Recommended.MultitokenSpanFE.MyCLP, Recommended.TokenFE.MyCLP, SpanDifference.Invoker, Test.DataClassificationTask.GUIParams, Train.DataClassificationTask.GUIParams, TrainTest.DataClassificationTask.GUIParams, UI.DataClassificationTask.GUIParams

public abstract class BasicCommandLineProcessor
extends java.lang.Object
implements CommandLineProcessor, Saveable

A lightweight command-line processing tool.

Author:
William Cohen

Nested Class Summary
 
Nested classes/interfaces inherited from interface edu.cmu.minorthird.util.CommandLineProcessor
CommandLineProcessor.Configurable
 
Constructor Summary
BasicCommandLineProcessor()
           
 
Method Summary
 void config(java.lang.String fileName)
          Implements the -config option.
static void config(java.lang.String fileName, CommandLineProcessor clp)
          Implements the -config option for the given clp.
 int consumeArguments(java.lang.String[] args, int startPos)
          Try to consume a the command-line argument at position i.
 java.lang.String getExtensionFor(java.lang.String format)
          Recomended extension for the format with the given name.
 java.lang.String[] getFormatNames()
          List of formats in which the object can be saved.
 void help()
          Override this to make --help do something other than call usage
static void main(java.lang.String[] args)
          Test and/or example code.
 void processArguments(java.lang.String[] args)
          Loop thru the command-line arguments.
protected  java.util.List propertyList()
          A list of arguments from the command line, in order.
protected  java.lang.String propertyValue(java.lang.String property)
          The value assigned to a property from the command line.
 java.lang.Object restore(java.io.File file)
          Restore the object from a file.
 void saveAs(java.io.File file, java.lang.String format)
          Save this object to the given file, in the given format.
 boolean shouldTerminate()
          Specifies whether the commandline processing should terminate.
protected  CommandLineProcessor tryToGetCLP(java.lang.Object o)
           
 void usage()
          Override this to print a meaningful usage error.
 void usage(java.lang.String errorMessage)
          Prints errorMessage and then calls usage().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BasicCommandLineProcessor

public BasicCommandLineProcessor()
Method Detail

processArguments

public final void processArguments(java.lang.String[] args)
Description copied from interface: CommandLineProcessor
Loop thru the command-line arguments. Option pairs like "-foo bar" should cause a call to x.foo("bar"), where x is this CommandLineProcessor. Options like "-gorp" cause a call to x.gorp(). Options without corresponding methods are considered errors. If an error occurs, then an IllegalArgumentException is thrown.

If function like -foo happens to return a non-null CommandLineProcessor, that processor is invoked on the arguments immediately after foo. This allows one CommandLineProcessor to pass command-line options to a second one. For instance, if x and y are CommandLineProcessors, x has an method colorOpts() that returns y, and y has methods background(String s) and foreground(String t) then the command line sequence -colorOpts -background blue -foreground red would call the expected methods of y.

The optional "-config FILE" should cause a Properties object to be loaded from FILE, and the property/value pairs in the Properties object to be treated as if they were option/argument pairs. The order of lines in the property file may not be preserved, however, and duplicated keys will be ignored.

For instance, calling -config FILE for a FILE containing

baz=
foo=bar
would be the same as the command options -foo bar -baz or possibly -baz -foo bar

Specified by:
processArguments in interface CommandLineProcessor

shouldTerminate

public boolean shouldTerminate()
Description copied from interface: CommandLineProcessor
Specifies whether the commandline processing should terminate. For example, if the usage is to be displayed, we should ignore other arguments and terminate.

Specified by:
shouldTerminate in interface CommandLineProcessor

consumeArguments

public final int consumeArguments(java.lang.String[] args,
                                  int startPos)
Description copied from interface: CommandLineProcessor
Try to consume a the command-line argument at position i. Return the number of arguments successfully consumed.

Specified by:
consumeArguments in interface CommandLineProcessor

config

public void config(java.lang.String fileName)
Implements the -config option.


config

public static void config(java.lang.String fileName,
                          CommandLineProcessor clp)
Implements the -config option for the given clp. This is static so that JointCommandLineProcessor can use it also.


usage

public void usage(java.lang.String errorMessage)
Description copied from interface: CommandLineProcessor
Prints errorMessage and then calls usage().

Specified by:
usage in interface CommandLineProcessor

usage

public void usage()
Override this to print a meaningful usage error. Default will list all commands other than 'usage', 'help', 'getX', and 'setX'.

Specified by:
usage in interface CommandLineProcessor

help

public void help()
Override this to make --help do something other than call usage


propertyList

protected java.util.List propertyList()
A list of arguments from the command line, in order. For instance, if the command line includes -foo bar, then "foo" will appear on the propertyList.


propertyValue

protected java.lang.String propertyValue(java.lang.String property)
The value assigned to a property from the command line. For instance, if the command line includes -foo bar, then propertyValue("foo") will return "bar". This can only be called after propertyList has been called.


getFormatNames

public java.lang.String[] getFormatNames()
Description copied from interface: Saveable
List of formats in which the object can be saved.

Specified by:
getFormatNames in interface Saveable

getExtensionFor

public java.lang.String getExtensionFor(java.lang.String format)
Description copied from interface: Saveable
Recomended extension for the format with the given name.

Specified by:
getExtensionFor in interface Saveable

saveAs

public void saveAs(java.io.File file,
                   java.lang.String format)
            throws java.io.IOException
Description copied from interface: Saveable
Save this object to the given file, in the given format.

Specified by:
saveAs in interface Saveable
Throws:
java.io.IOException

restore

public java.lang.Object restore(java.io.File file)
                         throws java.io.IOException
Description copied from interface: Saveable
Restore the object from a file.

Specified by:
restore in interface Saveable
Throws:
java.io.IOException

main

public static void main(java.lang.String[] args)
Test and/or example code. For sample output, invoke this with arguments -scoff you -laff -family -mom gerbil -taunt


tryToGetCLP

protected CommandLineProcessor tryToGetCLP(java.lang.Object o)