Package com.martiansoftware.jsap
Class Switch
- java.lang.Object
-
- com.martiansoftware.jsap.Parameter
-
- com.martiansoftware.jsap.Switch
-
- All Implemented Interfaces:
Flagged
public class Switch extends Parameter implements Flagged
A Switch is a parameter whose presence alone is significant; another commonly used term for a Switch is "Flag". Switches use aBooleanStringParserinternally, so their results can be obtained from a JSAPResult using the getBoolean() methods.An example of a command line using a Switch is "dosomething -v", where "-v" might mean "verbose."
- Author:
- Marty Lamb
- See Also:
Flagged,BooleanStringParser
-
-
Constructor Summary
Constructors Constructor Description Switch(java.lang.String id)Creates a new Switch with the specified unique ID.Switch(java.lang.String id, char shortFlag, java.lang.String longFlag)A shortcut constructor that creates a new Switch and configures all of its settings.Switch(java.lang.String id, char shortFlag, java.lang.String longFlag, java.lang.String help)A shortcut constructor that creates a new Switch and configures all of its settings, including help.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringgetLongFlag()Returns the long flag for this Switch.chargetShortFlag()Returns the short flag for this Switch.java.lang.CharactergetShortFlagCharacter()Returns the short flag for this Switch.java.lang.StringgetSyntax()Returns usage instructions for this Switch.protected java.util.Listparse(java.lang.String arg)Creates a new BooleanStringParser to which it delegates the parsing of the specified argument.SwitchsetDefault(java.lang.String defaultValue)Sets a default value for this parameter.SwitchsetDefault(java.lang.String[] defaultValues)Sets one or more default values for this parameter.SwitchsetLongFlag(java.lang.String longFlag)Sets the long flag for this Switch.SwitchsetShortFlag(char shortFlag)Sets the short flag for this Switch.-
Methods inherited from class com.martiansoftware.jsap.Parameter
_setDefault, _setDefault, _setUsageName, addDefault, enforceParameterLock, getDefault, getHelp, getID, getUsage, getUsageName, locked, setHelp, setLocked
-
-
-
-
Constructor Detail
-
Switch
public Switch(java.lang.String id)
Creates a new Switch with the specified unique ID.- Parameters:
id- the unique ID for this Switch.
-
Switch
public Switch(java.lang.String id, char shortFlag, java.lang.String longFlag, java.lang.String help)A shortcut constructor that creates a new Switch and configures all of its settings, including help.- Parameters:
id- the unique ID for this Switch.shortFlag- the short flag for this Switch (may be set to JSAP.NO_SHORTFLAG for none).longFlag- the long flag for this Switch (may be set to JSAP.NO_LONGFLAG for none).help- the help text for this Switch (may be set toJSAP.NO_HELPfor none).
-
Switch
public Switch(java.lang.String id, char shortFlag, java.lang.String longFlag)A shortcut constructor that creates a new Switch and configures all of its settings.- Parameters:
id- the unique ID for this Switch.shortFlag- the short flag for this Switch (may be set to JSAP.NO_SHORTFLAG for none).longFlag- the long flag for this Switch (may be set to JSAP.NO_LONGFLAG for none).
-
-
Method Detail
-
setShortFlag
public Switch setShortFlag(char shortFlag)
Sets the short flag for this Switch. To use no short flag at all, set the value to JSAP.NO_SHORTFLAG.- Parameters:
shortFlag- the short flag for this Switch.- Returns:
- the modified Switch
-
getShortFlag
public char getShortFlag()
Returns the short flag for this Switch. If this Switch has no short flag, the return value will be equal to JSAP.NO_SHORTFLAG.- Specified by:
getShortFlagin interfaceFlagged- Returns:
- the short flag for this Switch. If this Switch has no short flag, the return value will be equal to JSAP.NO_SHORTFLAG.
-
getShortFlagCharacter
public java.lang.Character getShortFlagCharacter()
Returns the short flag for this Switch. If this Switch has no short flag, the return value will be null.- Specified by:
getShortFlagCharacterin interfaceFlagged- Returns:
- the short flag for this Switch. If this Switch has no short flag, the return value will be null.
-
setLongFlag
public Switch setLongFlag(java.lang.String longFlag)
Sets the long flag for this Switch. To use no long flag at all, set the value to JSAP.NO_LONGFLAG.- Parameters:
longFlag- the long flag for this Switch.- Returns:
- the modified Switch
-
getLongFlag
public java.lang.String getLongFlag()
Returns the long flag for this Switch. If this Switch has no long flag, the return value will be equal to JSAP.NO_LONGFLAG.- Specified by:
getLongFlagin interfaceFlagged- Returns:
- the long flag for this FlaggedOption. If this FlaggedOption has no long flag, the return value will be equal to JSAP.NO_LONGFLAG.
-
parse
protected java.util.List parse(java.lang.String arg) throws ParseExceptionCreates a new BooleanStringParser to which it delegates the parsing of the specified argument. The result is always a single Boolean.- Specified by:
parsein classParameter- Parameters:
arg- the argument to parse.- Returns:
- an ArrayList containing a single Boolean.
- Throws:
ParseException- if the specified parameter cannot be parsed.
-
getSyntax
public java.lang.String getSyntax()
Returns usage instructions for this Switch.
-
setDefault
public Switch setDefault(java.lang.String defaultValue)
Sets a default value for this parameter. The default is specified as a String, and is parsed as a single value specified on the command line. In other words, default values for "list" parameters or parameters allowing multiple declarations should be set using setDefault(String[]), as JSAP would otherwise treat the entire list of values as a single value.- Parameters:
defaultValue- the default value for this parameter.- See Also:
setDefault(String)
-
setDefault
public Switch setDefault(java.lang.String[] defaultValues)
Sets one or more default values for this parameter. This method should be used whenever a parameter has more than one default value.- Parameters:
defaultValues- the default values for this parameter.- See Also:
setDefault(String)
-
-