CliUsageExamplepublic class CliUsageExample extends Object
Fields Summary |
---|
org.apache.commons.cli.CommandLineParser | parser | org.apache.commons.cli.Options | options | org.apache.commons.cli.CommandLine | commandLine | private static final String | USAGE | private static final String | HEADER | private static final String | FOOTER |
Constructors Summary |
---|
public CliUsageExample()
parser = new PosixParser();
options = new Options();
initializeOptions();
|
Methods Summary |
---|
private void | initializeOptions()
options.addOption("h", "help", false, "Print this usage information");
options.addOption("z", "compress", false, "Compress program output");
options.addOption("v", "verbose", false, "Print out VERBOSE debugging information" );
options.addOption("f", "file", true, "File to save program output to");
| public static void | main(java.lang.String[] args)
CliUsageExample cliExample = new CliUsageExample();
cliExample.run( args );
System.exit(0);
| private void | printSuppliedOptions()
System.out.println( "Command Line Successfully Parsed!");
if( commandLine.hasOption('v") ) { System.out.println( "\tRunning in Verbose Mode."); }
if( commandLine.hasOption('z") ) { System.out.println( "\tCompressing Program Output."); }
if( commandLine.hasOption('f") ) { System.out.println( "\tSending Output to File: " + commandLine.getOptionValue('f")); }
| private void | printUsage()
HelpFormatter helpFormatter = new HelpFormatter();
helpFormatter.setWidth( 80 );
helpFormatter.printHelp( USAGE, HEADER, options, FOOTER, true);
| private void | run(java.lang.String[] args)
try {
commandLine = parser.parse( options, args );
} catch( ParseException pe ) {
printUsage();
}
if( commandLine.hasOption('h") ) {
printUsage();
return;
} else {
printSuppliedOptions();
}
|
|