org.xerial.util.cui
Class OptionParser<OptionID extends Comparable>

java.lang.Object
  extended by org.xerial.util.cui.OptionParser<OptionID>

public class OptionParser<OptionID extends Comparable>
extends Object

OptionParser parses command line arguments

Usage:

 
  public static void main(String[] args)
  {
       enum Opt { HELP, OUTDIR } 
       OptionParser<Opt> p = new OptionParser<Opt>();
       p.addOption(Opt.HELP, "h", "help", "display help message");
       // adds an option with an argument whose default value is "."     
       p.addOptionWithArgument(Opt.OUTDIR, "o", "outdir", "DIR", "specify output direcotry", ".");
       
       p.parse(args);
       
       if(p.isSet(Opt.HELP))
       {
          // print help message
          System.out.println(p.helpMessage());
          return; 
       }
       
       if(p.isSet(Opt.OUTDIR))
       {
           String outDir = p.getValue(Opt.OUTDIR);
       }
   }
  
 

Author:
leo

Constructor Summary
OptionParser()
          A constructor
 
Method Summary
 void addOption(Option<OptionID> option)
          Deprecated. use addOption(Comparable, String, String, String, OptionHandler) instead.
 void addOption(OptionID optionID, String shortOptionName, String longOptionName, String description)
          Adds an option to detect
 void addOption(OptionID optionID, String shortOptionName, String longOptionName, String description, boolean presetValue)
          Adds an option with the default argument (set or not set)
 void addOption(OptionID optionID, String shortOptionName, String longOptionName, String description, OptionHandler handler)
          Adds an option to detect
 void addOptionGroup(OptionGroup<OptionID> optionGroup)
          Adds an option group
 void addOptionWithArgument(OptionID optionID, String shortOptionName, String longOptionName, String argumentName, String description)
          Adds an option that takes an argument value
 void addOptionWithArgument(OptionID optionID, String shortOptionName, String longOptionName, String argumentName, String description, OptionHandler<OptionID> handler)
          Adds an option that takes an argument value
 void addOptionWithArgument(OptionID optionID, String shortOptionName, String longOptionName, String argumentName, String description, String defaultValue)
          Adds an option that takes an argument value
 void addOptionWithArgument(OptionID optionID, String shortOptionName, String longOptionName, String argumentName, String description, String defaultValue, OptionHandler<OptionID> handler)
          Adds an option that takes an argument value
 List<String> getArgument()
          オプション関連以外のコマンドラインの引数のリストを得る
 String getArgument(int index)
          index番目のコマンドライン引数(オプション以外)
 int getArgumentLength()
          オプション以外のコマンドライン引数の数を返す
 double getDoubleValue(OptionID optionID)
          option引数の double値
 float getFloatValue(OptionID optionID)
          option引数のfloat値
 int getIntValue(OptionID optionID)
          option引数(例:--width=100 の100) のint value
 String getValue(OptionID optionID)
          Gets the option argument value as a String
 String getValue(OptionID optionID, String defaultValue)
          Gets the option argument value as a String
 String helpMessage()
          Generates the help message for this option parser
 boolean isSet(OptionID optionID)
          特定のoptionがセットされているか調べる
 void parse(String[] args)
          Parses the command line arguments.
 void setIgnoreUnknownOption(boolean ignore)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OptionParser

public OptionParser()
A constructor

Method Detail

addOption

public void addOption(OptionID optionID,
                      String shortOptionName,
                      String longOptionName,
                      String description)
Adds an option to detect

Parameters:
optionID - the option ID
shortOptionName - the short option name, e.g. "h", which corresponds to "-h" in the command line arguments.
longOptionName - the long option name, e.g. "help", which corresponds to "--help" in the command line arguments.
description - the description of the option, which is used to generate the help message of this option

addOption

public void addOption(OptionID optionID,
                      String shortOptionName,
                      String longOptionName,
                      String description,
                      OptionHandler handler)
Adds an option to detect

Parameters:
optionID - the option ID
shortOptionName - the short option name, e.g. "h", which corresponds to "-h" in the command line arguments.
longOptionName - the long option name, e.g. "help", which corresponds to "--help" in the command line arguments.
description - the description of the option, which is used to generate the help message of this option
handler - the option argument handler invoked when the option is set

addOption

public void addOption(Option<OptionID> option)
Deprecated. use addOption(Comparable, String, String, String, OptionHandler) instead.

Parameters:
option -

addOption

public void addOption(OptionID optionID,
                      String shortOptionName,
                      String longOptionName,
                      String description,
                      boolean presetValue)
Adds an option with the default argument (set or not set)

Parameters:
optionID - the option ID
shortOptionName - the short option name, e.g. "h", which corresponds to "-h" in the command line arguments.
longOptionName - the long option name, e.g. "help", which corresponds to "--help" in the command line arguments.
description - the description of the option, which is used to generate the help message of this option
presetValue - when true, the option is assumed to be set in default, otherwise false

addOptionWithArgument

public void addOptionWithArgument(OptionID optionID,
                                  String shortOptionName,
                                  String longOptionName,
                                  String argumentName,
                                  String description)
Adds an option that takes an argument value

Parameters:
optionID - the option ID
shortOptionName - the short option name, e.g. "h", which corresponds to "-h" in the command line arguments.
longOptionName - the long option name, e.g. "help", which corresponds to "--help" in the command line arguments.
description - the description of the option, which is used to generate the help message of this option

addOptionWithArgument

public void addOptionWithArgument(OptionID optionID,
                                  String shortOptionName,
                                  String longOptionName,
                                  String argumentName,
                                  String description,
                                  OptionHandler<OptionID> handler)
Adds an option that takes an argument value

Parameters:
optionID - the option ID
shortOptionName - the short option name, e.g. "h", which corresponds to "-h" in the command line arguments.
longOptionName - the long option name, e.g. "help", which corresponds to "--help" in the command line arguments.
description - the description of the option, which is used to generate the help message of this option
handler - the option argument handler invoked when the option is set

addOptionWithArgument

public void addOptionWithArgument(OptionID optionID,
                                  String shortOptionName,
                                  String longOptionName,
                                  String argumentName,
                                  String description,
                                  String defaultValue)
Adds an option that takes an argument value

Parameters:
optionID - the option ID
shortOptionName - the short option name, e.g. "h", which corresponds to "-h" in the command line arguments.
longOptionName - the long option name, e.g. "help", which corresponds to "--help" in the command line arguments.
description - the description of the option, which is used to generate the help message of this option
defaultValue - the default value of the option

addOptionWithArgument

public void addOptionWithArgument(OptionID optionID,
                                  String shortOptionName,
                                  String longOptionName,
                                  String argumentName,
                                  String description,
                                  String defaultValue,
                                  OptionHandler<OptionID> handler)
Adds an option that takes an argument value

Parameters:
optionID - the option ID
shortOptionName - the short option name, e.g. "h", which corresponds to "-h" in the command line arguments.
longOptionName - the long option name, e.g. "help", which corresponds to "--help" in the command line arguments.
description - the description of the option, which is used to generate the help message of this option
defaultValue - the default value of the option
handler - the option argument handler invoked when the option is set

addOptionGroup

public void addOptionGroup(OptionGroup<OptionID> optionGroup)
Adds an option group

Parameters:
optionGroup - the option group

parse

public void parse(String[] args)
           throws OptionParserException
Parses the command line arguments. After calling this method, you can retrieve the status of options via isSet(Comparable) or getValue(Comparable) methods, etc.

Parameters:
args - the command line arguments, which is given by the main(String[] args) method
Throws:
OptionParserException - when the given command line has invalid syntax or when incompatible options are set at the same time.

getArgument

public List<String> getArgument()
オプション関連以外のコマンドラインの引数のリストを得る

Returns:
オプション関連以外のコマンドライン引数のリスト

getArgument

public String getArgument(int index)
index番目のコマンドライン引数(オプション以外)

Parameters:
index -
Returns:
index番目(0 origin)のオプション関連以外のコマンドライン引数

getArgumentLength

public int getArgumentLength()
オプション以外のコマンドライン引数の数を返す

Returns:
引数の数

isSet

public boolean isSet(OptionID optionID)
              throws OptionParserException
特定のoptionがセットされているか調べる

Parameters:
optionID -
Returns:
コマンドラインでそのオプションが使われていればTrue。それ以外はfalse
Throws:
OptionParserException - optionIDに該当するオプションが設定されていない場合

getIntValue

public int getIntValue(OptionID optionID)
                throws OptionParserException
option引数(例:--width=100 の100) のint value

Parameters:
optionID -
Returns:
int値
Throws:
OptionParserException - 該当するoptionIDはない、あるいは引数がintに変換できない場合、あるいは引数を取れないオプションの場合

getDoubleValue

public double getDoubleValue(OptionID optionID)
                      throws OptionParserException
option引数の double値

Parameters:
optionID -
Returns:
option引数の double値
Throws:
OptionParserException - 該当するoptionIDはない、あるいは引数がdoubleに変換できない場合、あるいは引数を取れないオプションの場合

getFloatValue

public float getFloatValue(OptionID optionID)
                    throws OptionParserException
option引数のfloat値

Parameters:
optionID -
Returns:
option引数のfloat値
Throws:
OptionParserException - 該当するoptionIDはない、あるいは引数がfloatに変換できない場合、あるいは引数を取れないオプションの場合

getValue

public String getValue(OptionID optionID)
                throws OptionParserException
Gets the option argument value as a String

Parameters:
optionID - the target option that takes argument value
Returns:
the string value of the specified option argument
Throws:
OptionParserException - if the specified option ID is not found or if the option cannot take arguments

getValue

public String getValue(OptionID optionID,
                       String defaultValue)
                throws OptionParserException
Gets the option argument value as a String

Parameters:
optionID - the target option that takes argument value
defaultValue - the default value to be returned when this option is not present in the command line arguments
Returns:
the string value of the specified option argument
Throws:
OptionParserException - if the specified option ID is not found or if the option cannot take arguments

helpMessage

public String helpMessage()
Generates the help message for this option parser

Returns:
the help message that can be used as a list of command line options

setIgnoreUnknownOption

public void setIgnoreUnknownOption(boolean ignore)


Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.1 Japan License.