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 a BooleanStringParser internally, 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:
  • Constructor Details

    • Switch

      public Switch(String id)
      Creates a new Switch with the specified unique ID.
      Parameters:
      id - the unique ID for this Switch.
    • Switch

      public Switch(String id, char shortFlag, String longFlag, 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 to JSAP.NO_HELPfor none).
    • Switch

      public Switch(String id, char shortFlag, 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 Details

    • 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:
      getShortFlag in interface Flagged
      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 Character getShortFlagCharacter()
      Returns the short flag for this Switch. If this Switch has no short flag, the return value will be null.
      Specified by:
      getShortFlagCharacter in interface Flagged
      Returns:
      the short flag for this Switch. If this Switch has no short flag, the return value will be null.
    • setLongFlag

      public Switch setLongFlag(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 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:
      getLongFlag in interface Flagged
      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 List parse(String arg) throws ParseException
      Creates a new BooleanStringParser to which it delegates the parsing of the specified argument. The result is always a single Boolean.
      Specified by:
      parse in class Parameter
      Parameters:
      arg - the argument to parse.
      Returns:
      an ArrayList containing a single Boolean.
      Throws:
      ParseException - if the specified parameter cannot be parsed.
    • getSyntax

      public String getSyntax()
      Returns usage instructions for this Switch.
      Specified by:
      getSyntax in class Parameter
      Returns:
      usage instructions for this Switch based upon its current configuration.
    • setDefault

      public Switch setDefault(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

      public Switch setDefault(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: