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 ArgsFormat

The format used to parse a RawArgs instance.

This class is a container for Webmozart\Console\Api\Args\Format\CommandName, Webmozart\Console\Api\Args\Format\CommandOption, Webmozart\Console\Api\Args\Format\Option and Webmozart\Console\Api\Args\Format\Argument objects. The format is used to interpret a given RawArgs instance.

You can pass the options and arguments to the constructor of the class:

$format = new ArgsFormat(array( new CommandName('server'), new CommandName('add'), new Argument('host', Argument::REQUIRED), new Option('port', 'p', Option::VALUE_OPTIONAL, null, 80),
));

The previous example configures a command that can be called like this:

$ console server add localhost
$ console server add localhost --port 8080

If the "add" command should be called via an option, change the format to:

$format = new ArgsFormat(array( new CommandName('server'), new CommandOption('add', 'a'), new Argument('host', Argument::REQUIRED), new Option('port', 'p', Option::VALUE_OPTIONAL, null, 80),
));

The command is then called like this:

$ console server --add localhost
$ console server --add localhost --port 8080

The format is immutable after its construction. This is necessary to maintain consistency when one format inherits from another. For example, adding a required argument to the base format of a format that already contains optional arguments is an illegal operation that cannot be prevented if the formats are mutable.

If you want to create a format stepwisely, use an Webmozart\Console\Api\Args\Format\ArgsFormatBuilder.

If multiple formats share a common set of options and arguments, extract these options and arguments into a base format and let the other formats inherit from this base format:

$baseFormat = new ArgsFormat(array( new Option('verbose', 'v'),
));

$format = new ArgsFormat(array( new CommandName('server'), new CommandName('add'), new Argument('host', Argument::REQUIRED), new Option('port', 'p', Option::VALUE_OPTIONAL, null, 80),
), $baseFormat);
Namespace: Webmozart\Console\Api\Args\Format
Author: Bernhard Schussek bschussek@gmail.com
Since: 1.0
Located at Api/Args/Format/ArgsFormat.php
Methods summary
public static Webmozart\Console\Api\Args\Format\ArgsFormatBuilder
# build( Webmozart\Console\Api\Args\Format\ArgsFormat $baseFormat = null )

Returns a format builder.

Returns a format builder.

You can optionally pass a base format. The built format inherits all the arguments and options defined in the base format.

Parameters

$baseFormat
The base format.

Returns

Webmozart\Console\Api\Args\Format\ArgsFormatBuilder
The created builder.
public
# __construct( array|Webmozart\Console\Api\Args\Format\ArgsFormatBuilder $elements = array(), Webmozart\Console\Api\Args\Format\ArgsFormat $baseFormat = null )

Creates a new format.

Creates a new format.

You can optionally pass a base format. The created format inherits all the arguments and options defined in the base format.

Parameters

$elements

The arguments and options or a builder instance.

$baseFormat
The format.
public Webmozart\Console\Api\Args\Format\ArgsFormat
# getBaseFormat( )

Returns the base format.

Returns the base format.

Returns

Webmozart\Console\Api\Args\Format\ArgsFormat
The base format.
public Webmozart\Console\Api\Args\Format\CommandName[]
# getCommandNames( boolean $includeBase = true )

Returns the command names.

Returns the command names.

Parameters

$includeBase

Whether to include command names in the base format in the result.

Returns

Webmozart\Console\Api\Args\Format\CommandName[]
The command names.
public boolean
# hasCommandNames( boolean $includeBase = true )

Returns whether the format contains any command names.

Returns whether the format contains any command names.

Parameters

$includeBase

Whether to consider command names in the base format.

Returns

boolean

Returns true if the format contains command names and false otherwise.

public Webmozart\Console\Api\Args\Format\CommandOption
# getCommandOption( string $name, boolean $includeBase = true )

Returns a command option by its long or short name.

Returns a command option by its long or short name.

Parameters

$name
The long or short option name.
$includeBase

Whether to include options in the base format in the search.

Returns

Webmozart\Console\Api\Args\Format\CommandOption
The command option.

Throws

Webmozart\Console\Api\Args\NoSuchOptionException

If the command option with the given name does not not exist.

public Webmozart\Console\Api\Args\Format\CommandOption[]
# getCommandOptions( boolean $includeBase = true )

Returns all command options of the format.

Returns all command options of the format.

Parameters

$includeBase

Whether to include options of the base format in the result.

Returns

Webmozart\Console\Api\Args\Format\CommandOption[]
The command options.
public boolean
# hasCommandOption( string $name, boolean $includeBase = true )

Returns whether the format contains a specific command option.

Returns whether the format contains a specific command option.

You can either pass the long or the short name of the command option.

Parameters

$name
The long or short option name.
$includeBase

Whether to include options in the base format in the search.

Returns

boolean

Returns true if the command option with the given name could be found and false otherwise.

public boolean
# hasCommandOptions( boolean $includeBase = true )

Returns whether the format contains command options.

Returns whether the format contains command options.

Parameters

$includeBase

Whether to include options in the base format in the search.

Returns

boolean

Returns true if the format contains command options and false otherwise.

public Webmozart\Console\Api\Args\Format\Argument
# getArgument( string|integer $name, boolean $includeBase = true )

Returns an argument by its name or position.

Returns an argument by its name or position.

You can either pass the name of the argument or the 0-based position of the argument.

Parameters

$name

The argument name or its 0-based position in the argument list.

$includeBase

Whether to include arguments in the base format in the search.

Returns

Webmozart\Console\Api\Args\Format\Argument
The argument.

Throws

Webmozart\Console\Api\Args\NoSuchArgumentException

If the argument with the given name or position does not exist.

public Webmozart\Console\Api\Args\Format\Argument[]
# getArguments( boolean $includeBase = true )

Returns all arguments of the format.

Returns all arguments of the format.

Parameters

$includeBase

Whether to include arguments of the base format in the result.

Returns

Webmozart\Console\Api\Args\Format\Argument[]
The arguments.
public boolean
# hasArgument( string|integer $name, boolean $includeBase = true )

Returns whether the format contains a specific argument.

Returns whether the format contains a specific argument.

You can either pass the name of the argument or the 0-based position of the argument.

Parameters

$name

The argument name or its 0-based position in the argument list.

$includeBase

Whether to include arguments in the base format in the search.

Returns

boolean

Returns true if the argument with the given name or position could be found and false otherwise.

public boolean
# hasMultiValuedArgument( boolean $includeBase = true )

Returns whether the format contains a multi-valued argument.

Returns whether the format contains a multi-valued argument.

Parameters

$includeBase

Whether to include arguments in the base format in the search.

Returns

boolean

Returns true if the format contains a multi-valued argument and false otherwise.

public boolean
# hasOptionalArgument( boolean $includeBase = true )

Returns whether the format contains an optional argument.

Returns whether the format contains an optional argument.

Parameters

$includeBase

Whether to include arguments in the base format in the search.

Returns

boolean

Returns true if the format contains an optional argument and false otherwise.

public boolean
# hasRequiredArgument( boolean $includeBase = true )

Returns whether the format contains a required argument.

Returns whether the format contains a required argument.

Parameters

$includeBase

Whether to include arguments in the base format in the search.

Returns

boolean

Returns true if the format contains a required argument and false otherwise.

public boolean
# hasArguments( boolean $includeBase = true )

Returns whether the format contains any argument.

Returns whether the format contains any argument.

Parameters

$includeBase

Whether to include arguments in the base format in the search.

Returns

boolean

Returns true if the format contains any argument and false otherwise.

public integer
# getNumberOfArguments( boolean $includeBase = true )

Returns the number of arguments.

Returns the number of arguments.

Parameters

$includeBase

Whether to include arguments in the base format in the result.

Returns

integer
The number of arguments.
public integer
# getNumberOfRequiredArguments( boolean $includeBase = true )

Returns the number of required arguments.

Returns the number of required arguments.

Parameters

$includeBase

Whether to include arguments in the base format in the result.

Returns

integer
The number of required arguments.
public Webmozart\Console\Api\Args\Format\Option
# getOption( string $name, boolean $includeBase = true )

Returns an option by its long or short name.

Returns an option by its long or short name.

Parameters

$name
The long or short option name.
$includeBase

Whether to include options in the base format in the search.

Returns

Webmozart\Console\Api\Args\Format\Option
The option.

Throws

Webmozart\Console\Api\Args\NoSuchOptionException

If the option with the given name does not not exist.

public Webmozart\Console\Api\Args\Format\Option[]
# getOptions( boolean $includeBase = true )

Returns all options of the format.

Returns all options of the format.

Parameters

$includeBase

Whether to include options of the base format in the result.

Returns

Webmozart\Console\Api\Args\Format\Option[]
The options.
public boolean
# hasOption( string $name, boolean $includeBase = true )

Returns whether the format contains a specific option.

Returns whether the format contains a specific option.

You can either pass the long or the short name of the option.

Parameters

$name
The long or short option name.
$includeBase

Whether to include options in the base format in the search.

Returns

boolean

Returns true if the option with the given name could be found and false otherwise.

public boolean
# hasOptions( boolean $includeBase = true )

Returns whether the format contains options.

Returns whether the format contains options.

Parameters

$includeBase

Whether to include options in the base format in the search.

Returns

boolean

Returns true if the format contains options and false otherwise.

Webmozart Console API API documentation generated by ApiGen