com.compomics.util.general
Class CommandLineParser

java.lang.Object
  extended by com.compomics.util.general.CommandLineParser

public class CommandLineParser
extends java.lang.Object

This class provides a generic interface for the parsing of command-line arguments, options and flags.
Arguments are 'stand-alone' Strings, options are preceded by '--' without spaces (like in general GNU practice) and flags are indicated by '-' without spaces.
If there are options that in turn take arguments, these can be specified via a specific constructor.

Author:
Lennart Martens

Constructor Summary
CommandLineParser(java.lang.String[] aCommandLine)
          The constructor requires the caller to provide it with a command line arguments String[] that will be the basis of the parsing.
CommandLineParser(java.lang.String[] aCommandLine, java.lang.String[] aOptionArgs)
           This constructor requests the command-line String[] as well as a String[] with a list of options which in turn take a parameter.
 
Method Summary
 java.lang.String[] getFlags()
          This method will report on all flags that have been found, or return an empty String[] if none were present.
 java.lang.String getOptionParameter(java.lang.String aOption)
          This method will report the option parameter for an option that can take a parameter itself.
 java.lang.String[] getOptions()
          This method will report on all options that have been found, or return an empty String[] if none were present.
 java.lang.String[] getParameters()
          This method will report on all parameters that have been found, or return an empty String[] if none were present.
 boolean hasArguments()
          This method returns 'true' if any arguments are present, 'false' otherwise.
 boolean hasFlag(java.lang.String aFlag)
          This method test whether the specified flag was set on the commandline.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommandLineParser

public CommandLineParser(java.lang.String[] aCommandLine)
The constructor requires the caller to provide it with a command line arguments String[] that will be the basis of the parsing.

Parameters:
aCommandLine - String[] with the command-line arguments.

CommandLineParser

public CommandLineParser(java.lang.String[] aCommandLine,
                         java.lang.String[] aOptionArgs)
This constructor requests the command-line String[] as well as a String[] with a list of options which in turn take a parameter.

Parameters:
aCommandLine - String[] with the command-line arguments.
aOptionArgs - String[] with the options that take parameters themselves.
Method Detail

hasArguments

public boolean hasArguments()
This method returns 'true' if any arguments are present, 'false' otherwise.
You are well advised to call this method first, before attempting to retrieve any parameters, as 'null' might be returned.

Returns:
boolean 'true' if arguments are present, 'false' otherwise.

getFlags

public java.lang.String[] getFlags()
This method will report on all flags that have been found, or return an empty String[] if none were present.

Returns:
String[] with the flags or an empty array if none were found.

getOptions

public java.lang.String[] getOptions()
This method will report on all options that have been found, or return an empty String[] if none were present.

Returns:
String[] with the options or an empty array if none were found.

getParameters

public java.lang.String[] getParameters()
This method will report on all parameters that have been found, or return an empty String[] if none were present.

Returns:
String[] with the parameters or an empty array if none were found.

getOptionParameter

public java.lang.String getOptionParameter(java.lang.String aOption)
This method will report the option parameter for an option that can take a parameter itself. This option has to be specified in the constructor that allows you to pass a String[] with all the options that take a parameter themselves.
Note! If no parameter was present, or the option specified is not an option flagged as taking a parameter, 'null' is returned!

Parameters:
aOption - String with the option for which the parameter is to be retrieved.
Returns:
String with the parameter, if found, 'null' otherwise.

hasFlag

public boolean hasFlag(java.lang.String aFlag)
This method test whether the specified flag was set on the commandline.

Parameters:
aFlag - String with the flag to check. Comparison is case-sensitive!
Returns:
boolean 'true' if the flag was set, 'false' otherwise.


Copyright © 2013. All Rights Reserved.