FreeHEP API
Version current

org.freehep.util.argv
Class ArgumentParser

java.lang.Object
  extended byorg.freehep.util.argv.ArgumentParser

public class ArgumentParser
extends Object

A parser for processing command line arguments. It is typically used as such:

     // Initialize arguments that may appear in the command line. By convention,
     // '-help' simply prints the command line usage and exits.

     StringOption destination = new StringOption( "-dest", "localhost", "Destination for request tests" );
     BooleanOption help = new BooleanOption( "-help", "Describe command line args", true );

     // Initialize and invoke the parser. Note that arguments not consumed during
     // the parse are returned in case they may be subject to additional processing,
     // etc. Variable 'args' is assumed to contain the String array passed to main().

     ArgumentParser parser = new ArgumentParser();
     parser.add( destination );
     parser.add( skipClasses );
     parser.add( skipRequests );
     List extra = parser.parse( args );

     // For this application, extra arguments will be treated as a usage error.

     if( !extra.isEmpty() || help.getValue())
     {
         PrintWriter out = new PrintWriter( System.out );
         parser.printUsage( out );
         out.close();
         System.exit( 0 );
     }
 

See Also:
BooleanOption, StringOption
Source Code:
ArgumentParser.java

Constructor Summary
ArgumentParser(String name)
           
 
Method Summary
 void add(Option opt)
          Add a new option to be taken into consideration during the next parse.
 void add(Parameter param)
           
static void main(String[] ignored)
          Test driver.
 List parse(String[] args)
          Parses the given argument list according to all Options registered with this parser.
 void printUsage(OutputStream out)
          Dumps the usage of the program and each parameter and option to out.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArgumentParser

public ArgumentParser(String name)
Method Detail

add

public void add(Option opt)
Add a new option to be taken into consideration during the next parse. Behavior when an option is added multiple times or if multiple options share the same flag is undefined.


add

public void add(Parameter param)

printUsage

public void printUsage(OutputStream out)
Dumps the usage of the program and each parameter and option to out.


parse

public List parse(String[] args)
           throws MissingArgumentException,
                  ArgumentFormatException
Parses the given argument list according to all Options registered with this parser. Returns any arguments not consumed by the parse.

Throws:
MissingArgumentException
ArgumentFormatException

main

public static void main(String[] ignored)
                 throws MissingArgumentException,
                        ArgumentFormatException
Test driver.

Throws:
MissingArgumentException
ArgumentFormatException

FreeHEP API
Version current

Copyright © 2000-2004 FreeHEP, All Rights Reserved.