Overview

Namespaces

  • Webmozart
    • Console
      • Adapter
      • Api
        • Application
        • Args
          • Format
        • Command
        • Config
        • Event
        • Formatter
        • IO
        • Resolver
      • Args
      • Config
      • Formatter
      • Handler
        • Help
      • IO
        • InputStream
        • OutputStream
      • Process
      • Resolver
      • UI
        • Alignment
        • Component
        • Help
        • Layout
        • Style
      • Util

Classes

  • AbstractOption
  • ArgsFormat
  • ArgsFormatBuilder
  • Argument
  • CommandName
  • CommandOption
  • Option

Exceptions

  • InvalidValueException
  • Overview
  • Namespace
  • Class

Class Option

An input option.

Args options are passed after the command name(s). Each option has a long name that is prefixed by two dashes ("--") and optionally a short name that is prefixed by one dash only ("-"). The long name must have at least two characters, the short name must contain a single letter only.

In the example below, "--verbose" and "-v" are the long and short names of the same option:

$ console server --verbose
$ console server -v

The long and short names are passed to the constructor of this class. The leading dashes can be omitted:

$option = new Option('verbose', 'v');

If an option accepts a value, you must pass one of the flags VALUE_REQUIRED, VALUE_OPTIONAL or Webmozart\Console\Api\Args\Format\Option::MULTI_VALUED to the constructor:

$option = new Option('format', 'f', Option::VALUE_REQUIRED);
  • The flag VALUE_REQUIRED indicates that a value must always be passed. * The flag VALUE_OPTIONAL indicates that a value may optionally be passed. If no value is passed, the default value passed to the constructor is returned, which defaults to null. * The flag Webmozart\Console\Api\Args\Format\Option::MULTI_VALUED indicates that the option can be passed multiple times with different values. The passed values are returned to the application as array. The value of a multi-valued option is always required.
Webmozart\Console\Api\Args\Format\AbstractOption
Extended by Webmozart\Console\Api\Args\Format\Option
Namespace: Webmozart\Console\Api\Args\Format
Author: Bernhard Schussek bschussek@gmail.com
Since: 1.0
Located at Api/Args/Format/Option.php
Methods summary
public
# __construct( string $longName, string|null $shortName = null, integer $flags = 0, string $description = null, mixed $defaultValue = null, string $valueName = '...' )

Creates a new option.

Creates a new option.

Parameters

$longName
The long option name.
$shortName
The short option name.
$flags

A bitwise combination of the option flag constants.

$description
A human-readable description of the option.
$defaultValue

The default value (must be null for VALUE_REQUIRED or VALUE_NONE).

$valueName

The name of the value to be used in usage examples of the option.

Throws

Webmozart\Console\Api\Args\Format\InvalidValueException
If the default value is invalid.

Overrides

Webmozart\Console\Api\Args\Format\AbstractOption::__construct()
public boolean
# acceptsValue( )

Returns whether the option accepts a value.

Returns whether the option accepts a value.

Returns

boolean

Returns true if a value flag other than VALUE_NONE was passed to the constructor.

public mixed
# parseValue( mixed $value )

Parses an option value.

Parses an option value.

Pass one of the flags Webmozart\Console\Api\Args\Format\Option::STRING, Webmozart\Console\Api\Args\Format\Option::BOOLEAN, Webmozart\Console\Api\Args\Format\Option::INTEGER and Webmozart\Console\Api\Args\Format\Option::FLOAT to the constructor to configure the result of this method. You can optionally combine the flags with Webmozart\Console\Api\Args\Format\Option::NULLABLE to support the conversion of "null" to null.

Parameters

$value
The value to parse.

Returns

mixed
The parsed value.

Throws

Webmozart\Console\Api\Args\Format\InvalidValueException
public boolean
# isValueRequired( )

Returns whether the option requires a value.

Returns whether the option requires a value.

Returns

boolean

Returns true if the flag VALUE_REQUIRED was passed to the constructor.

public boolean
# isValueOptional( )

Returns whether the option takes an optional value.

Returns whether the option takes an optional value.

Returns

boolean

Returns true if the flag VALUE_OPTIONAL was passed to the constructor.

public boolean
# isMultiValued( )

Returns whether the option accepts multiple values.

Returns whether the option accepts multiple values.

Returns

boolean

Returns true if the flag Webmozart\Console\Api\Args\Format\Option::MULTI_VALUED was passed to the constructor.

public
# setDefaultValue( mixed $defaultValue = null )

Sets the default value of the option.

Sets the default value of the option.

If the option does not accept a value, this method throws an exception.

If the option is multi-valued, the passed value must be an array or null.

Parameters

$defaultValue
The default value.

Throws

Webmozart\Console\Api\Args\Format\InvalidValueException
If the default value is invalid.
public mixed
# getDefaultValue( )

Returns the default value of the option.

Returns the default value of the option.

Returns

mixed
The default value.
public string
# getValueName( )

Returns the name of the option value.

Returns the name of the option value.

This name can be used as placeholder of the value when displaying the option's usage.

Returns

string
The name of the option value.
Methods inherited from Webmozart\Console\Api\Args\Format\AbstractOption
getDescription(), getLongName(), getShortName(), isLongNamePreferred(), isShortNamePreferred()
Constants summary
integer NO_VALUE

Flag: The option has no value.

Flag: The option has no value.

# 4
integer REQUIRED_VALUE

Flag: The option has a required value.

Flag: The option has a required value.

# 8
integer OPTIONAL_VALUE

Flag: The option has an optional value.

Flag: The option has an optional value.

# 16
integer MULTI_VALUED

Flag: The option can be stated multiple times with different values.

Flag: The option can be stated multiple times with different values.

# 32
integer STRING

Flag: The option value is parsed as string.

Flag: The option value is parsed as string.

# 128
integer BOOLEAN

Flag: The option value is parsed as boolean.

Flag: The option value is parsed as boolean.

# 256
integer INTEGER

Flag: The option value is parsed as integer.

Flag: The option value is parsed as integer.

# 512
integer FLOAT

Flag: The option value is parsed as float.

Flag: The option value is parsed as float.

# 1024
integer NULLABLE

Flag: The option value "null" should be parsed as null.

Flag: The option value "null" should be parsed as null.

# 2048
Constants inherited from Webmozart\Console\Api\Args\Format\AbstractOption
PREFER_LONG_NAME, PREFER_SHORT_NAME
Properties inherited from Webmozart\Console\Api\Args\Format\AbstractOption
$flags
Webmozart Console API API documentation generated by ApiGen