The CommandLineHandler is a framework that allows you to more easily make command line applications consistent with current standards. The help for a CommandLineApplication is automatically generated. All parsing and grouping of the command line options used is done for you. Options that are supplied at command line cause handler methods to be called, or fields to be assigned. This is all made possible by Annotations (which requires Java 5.0) and reflection.
It will always remain free and open source.
Event Driven Command Line Handler on SourceForge
Download
JavaDoc API
Change Log
LGPL License
If nobody uses this library, then the goals won't be met (mainly consistency). It will probably someday be donated to Apache in the hope that it will be used and improved by many people. If you can help make this happen, please email me.
class ExampleCLA implements CommandLineApplication { /** * A method. Does some implementation specific stuff. * @param f a parameter */ @CommandLineOption( longOption = "foo", shortOption = "f", description = "an example of an option handler method. prints out the parameter's value.", parameterName = "cost" ) public void someMethod(float f) { System.out.println("This option's parameter's value is "+f); } /** * A field. */ @CommandLineOption( longOption = "bar", shortOption = "b", description = "an example of an option handler field.", parameterName = "frequency" ) public Long someValue = null; //initialize to null so we'll know if it wasn't changed ... }If the user types "java ExampleCLA --foo 5", that method will be called. If someone types "java ExampleCLA --bar 5", someValue will be set to 5 before doneHandling() is called.
/** @param args command line arguments */ public static void main(String[] args) { try{ CommandLineHandler.startHandling(new ExampleCLA(), args); }catch(RuntimeException e) { System.err.println("Caught an exception: " + e.getMessage() ); } }OR
/** @param args command line arguments */ public static void main(String[] args) { CommandLineApplication app = new ExampleCLA(); try{ CommandLineHandler h = new CommandLineHandler( args, app ); h.handleArguments(); // don't put anything else in here! put it in doneHandling() or else // it will get called even if "--help" is used } catch ( ParameterUsageException e ) { // handle errors (caused by bad use of your options by the user) System.err.println( e.getMessage() ); } catch ( ParameterDefinitionException e ) { // handle errors (caused by bad CommandLineOption Annotations in // your CommandLineApplication) System.err.println( e.getMessage() ); } catch ( RuntimeException e ) { // this catch is optional. // catch whatever is thrown from your CommandLineOption handler // methods System.err.println( e.getMessage() ); } }
class ExampleCLA implements CommandLineApplication { ... public void validate(CommandLineHandler handler) throws ParameterUsageException { //bar was initialized to null so we could do this check if( someValue == null ) throw new ParameterUsageException( "The \"" + handler.getOption(this, "someValue").longOption() + "\" parameter is required" ); } public void doneHandling() { // this message doesn't show up in the help, or when there is an error // with parameters. that is why we put it here and not in main(...) System.out.println("The program would run now. someValue is " + someValue + "."); } ... }
class ExampleCLA implements CommandLineApplication { ... public String getCustomDocumentationHeader(CommandLineHandler handler) { // NOTE that we use "someValue" here instead of "bar" because // refactoring programs could replace them, but not the strings "bar" // and "foo" (tested in Eclipse 3.1.0), so multiple changes aren't // technically required (CommandLineHandler goal number 3) return getTitle() + ", version " + getVersion() + ", by " + getAuthor() + StringTool.NEWLINE + StringTool.NEWLINE + handler.getSynopsis(this, "someMethod", "someValue") + StringTool.NEWLINE + handler.getSynopsis(this, "someValue"); } ... }
peterMaloney.util-1.0.2.jar -requires StringTool, ReflectionTool, AttributeList, and UniqueList from the "org.peterMaloney.util" package.
Original author and only coder: Peter Maloney <circumlocuter@yahoo.com> Additional contributiors: (Ideas, misc. help) Sam Maloney <sam.maloney@gmail.com> Testing and bug reports: Sam Maloney <sam.maloney@gmail.com>
example: "--outputFile a.out test"
-"outputFile" is an option
-"a.out" is a parameter to that option.
-"test" is an argument, but is neither an option nor a parameter.