FileDocCategorySizeDatePackage
CliUsageExample.javaAPI DocExample3273Wed May 18 09:38:58 BST 2005com.discursive.jccook.cmdline

CliUsageExample

public 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 voidinitializeOptions()

        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 voidmain(java.lang.String[] args)


        CliUsageExample cliExample = new CliUsageExample();
        cliExample.run( args );

        System.exit(0);
    
private voidprintSuppliedOptions()

        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 voidprintUsage()

        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.setWidth( 80 );
        helpFormatter.printHelp( USAGE, HEADER, options, FOOTER, true);
    
private voidrun(java.lang.String[] args)

        try {
            commandLine = parser.parse( options, args );
        } catch( ParseException pe ) {
            printUsage();
        }
        
        if( commandLine.hasOption('h") ) { 
            printUsage(); 
            return;
        } else {
            printSuppliedOptions();
        }