roast
Class CommandLine
java.lang.Object
|
+--roast.CommandLine
- public class CommandLine
- extends java.lang.Object
A CommandLine object parses an array of Unix-style
command-line arguments according to a user-supplied specification.
define terms: flag, flag argument, suffix argument
State variables
errorMessage: String
flagArgs: set of (flag: String, arg: String)
suffixArgs: array of String
Constructor Summary |
CommandLine(FlagSpec[] flagSpec,
java.lang.String[] args)
Parses an array of command line flags and arguments. |
Method Summary |
java.lang.String |
getErrorMessage()
Gets the error message if the command line was not successfully
parsed. |
java.lang.String |
getFlagArg(java.lang.String flagName)
Gets value of named flag. |
java.lang.String[] |
getSuffixArgs()
Gets suffix arguments arguments present in args. |
boolean |
isArgPresent(java.lang.String flagName)
Determines whether the named flag is present in args. |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
DUPLICATEFLAG
public static final java.lang.String DUPLICATEFLAG
MISSINGFLAGARG
public static final java.lang.String MISSINGFLAGARG
INVALIDFLAGARG
public static final java.lang.String INVALIDFLAGARG
INVALIDFLAG
public static final java.lang.String INVALIDFLAG
REQUIREDFLAGMISSING
public static final java.lang.String REQUIREDFLAGMISSING
CommandLine
public CommandLine(FlagSpec[] flagSpec,
java.lang.String[] args)
throws ParameterException
- Parses an array of command line flags and arguments.
- Precondition
-
ArgType does not throw an exception
- Exceptions
-
if flagSpec or args contains a null entry
or args has an entry that is ""
or flagSpec has two entries with the same flag name then
throw ParameterException
- Transitions
-
i = 0
errorMessage = ""
while i < 0..args.length && errorMessage == ""
if args[i] has a leading "-" // flags
if flagSpec has an entry F such that F.name == args[i]
if flagArgs has an entry with name args[i]
errorMessage = DUPLICATEFLAG+args[i]
else if F.argRequired
if i == args.length-1
errorMessage = MISSINGFLAGARG+args[i]
else if !F.argType.isValid(args[i+1])
errorMessage = INVALIDFLAGARG+args[i+1]
else
add (args[i],args[i+1]) to flagArgs
i = i+2
else
add (args[i],null) to flagArgs
i = i+1
else
errorMessage = INVALIDFLAG+args[i]
else // begin suffix arguments
if there is a required flag F in flagSpec not in flagArgs
errorMessage = REQUIREDFLAGMISSING+F.name
else
copy args[i..args.length-1] to suffixArgs
getErrorMessage
public java.lang.String getErrorMessage()
- Gets the error message if the command line was not successfully
parsed.
- Output
-
errorMessage or null if no error occurred
isArgPresent
public boolean isArgPresent(java.lang.String flagName)
- Determines whether the named flag is present in args.
- Output
-
exists (flagName,x) in flagArgs, where x is any value (may be null)
getFlagArg
public java.lang.String getFlagArg(java.lang.String flagName)
- Gets value of named flag.
- Output
-
arg such that (flagName, arg) is in flagArgs
null if an error occurred during parsing
getSuffixArgs
public java.lang.String[] getSuffixArgs()
- Gets suffix arguments arguments present in args.
- Output
-
suffixArgs, may contain zero elements
null if an error occurred during parsing