org.kohsuke.args4j
Class CmdLineParser

java.lang.Object
  extended by org.kohsuke.args4j.CmdLineParser

public final class CmdLineParser
extends java.lang.Object

Command line argument parser.

Author:
Kohsuke Kawaguchi (kk@kohsuke.org)

Nested Class Summary
private  class CmdLineParser.CmdLineImpl
          Essentially a pointer over a String array.
 
Field Summary
private  java.util.List arguments
          Non-option arguments that are parsed.
private  java.util.List options
          Set of registered CmdLineOptions.
private  java.util.List readonlyArguments
          Read-only view of arguments.
 
Constructor Summary
CmdLineParser()
           
 
Method Summary
 CmdLineOption addOption(CmdLineOption opt)
          Adds a new option to the parser.
 void addOptionClass(java.lang.Object obj)
          Adds all the CmdLineOption-derived fields on this object.
 java.util.List getArguments()
          Returns the list of non-option arguments.
 void parse(java.lang.String[] args)
          Parse the arguments.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

options

private final java.util.List options
Set of registered CmdLineOptions.


arguments

private final java.util.List arguments
Non-option arguments that are parsed.


readonlyArguments

private final java.util.List readonlyArguments
Read-only view of arguments.

Constructor Detail

CmdLineParser

public CmdLineParser()
Method Detail

addOption

public CmdLineOption addOption(CmdLineOption opt)
Adds a new option to the parser.

Returns:
The value passed as the opt parameter.

addOptionClass

public void addOptionClass(java.lang.Object obj)
Adds all the CmdLineOption-derived fields on this object.

This method uses Java reflection to the specified object, and looks for fields whose types derive from CmdLineOption. All such fields are added through addOption(CmdLineOption) method.

This method would be convenient if you have a class that defines a bunch of CmdLineOptions as its fields. For example,

 class MyMain {
     BooleanOption opt1 = ...;
     StringOption  opt2 = ...;
     IntOption     opt3 = ...;
     ...
 
     void doMain( String[] args ) {
         CmdLineParser parser = new CmdLineParser();
         parser.addOptionClass(this);
 
         parser.parse(args);
 
         ....
     }
 }
 
 

Note that because of the access modifier restriction, only public fields of a clss can be registered through this method.

Throws:
java.lang.IllegalArgumentException - If the specified object doesn't contain any CmdLineOption fields. Given the typical use case, this is more likely to be a bug of the caller, but I appreciate your input on this behavior.

parse

public void parse(java.lang.String[] args)
           throws CmdLineException
Parse the arguments. Can be invoked multiple times or recursively if necessary.

Throws:
CmdLineException

getArguments

public java.util.List getArguments()
Returns the list of non-option arguments.

For example, if this parser is being used by javac and the command line is like "-d x abc.java def.java -target 1.1", then this method will return ["abc.java","def.java"].



Copyright © 2003 Kohsuke Kawaguchi. All Rights Reserved.