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

  • Args

Interfaces

  • ArgsParser
  • RawArgs

Exceptions

  • CannotAddArgumentException
  • CannotAddOptionException
  • CannotParseArgsException
  • NoSuchArgumentException
  • NoSuchOptionException
  • Overview
  • Namespace
  • Class

Class Args

The parsed console arguments.

The parsed arguments provide access to the options and arguments passed via the command line. Usually you can construct an Webmozart\Console\Api\Args\Args instance by parsing a Webmozart\Console\Api\Args\RawArgs instance with an Webmozart\Console\Api\Args\ArgsParser:

$format = ArgsFormat::build() ->addCommandName(new CommandName('server')) ->addCommandName(new CommandName('add')) ->addOption(new Option('port', 'p', Option::VALUE_REQUIRED | Option::INTEGER)) ->addArgument(new Argument('host', Argument::REQUIRED)) ->getFormat();

$args = $parser->parseArgs($rawArgs, $format);

The Webmozart\Console\Api\Args\Format\ArgsFormat defines which rules the console arguments must adhere to.

You can also create Webmozart\Console\Api\Args\Args instances manually. This is especially useful in tests:

$format = ArgsFormat::build() ->addOption(new Option('port', 'p', Option::VALUE_REQUIRED | Option::INTEGER)) ->addArgument(new Argument('host', Argument::REQUIRED)) ->getFormat();

$args = new Args($format);
$args->setOption('port', 80);
$args->setArgument('host', 'localhost');
Namespace: Webmozart\Console\Api\Args
Author: Bernhard Schussek bschussek@gmail.com
Since: 1.0
See: Webmozart\Console\Api\Args\RawArgs, Webmozart\Console\Api\Args\Format\ArgsFormat, Webmozart\Console\Api\Args\ArgsParser
Located at Api/Args/Args.php
Methods summary
public
# __construct( Webmozart\Console\Api\Args\Format\ArgsFormat $format, Webmozart\Console\Api\Args\RawArgs $rawArgs = null )

Creates the console arguments.

Creates the console arguments.

Parameters

$format

The format that the arguments and options must adhere to.

$rawArgs
The raw console arguments.
public string|null
# getScriptName( )

Returns the PHP script as it was called on the console.

Returns the PHP script as it was called on the console.

Returns

string|null

The script name or null if no script name is available.

public Webmozart\Console\Api\Args\Format\CommandName[]
# getCommandNames( )

Returns the command names as array.

Returns the command names as array.

Returns

Webmozart\Console\Api\Args\Format\CommandName[]
The command names.
public Webmozart\Console\Api\Args\Format\CommandOption[]
# getCommandOptions( )

Returns the command options as array.

Returns the command options as array.

Returns

Webmozart\Console\Api\Args\Format\CommandOption[]
The command options.
public mixed
# getOption( string $name )

Returns an option.

Returns an option.

If the option accepts a value, the value set for that option is returned. If no value was set with Webmozart\Console\Api\Args\Args::setOption(), the default value of the option is returned.j

If the option accepts no value, the method returns true if the option was set and false otherwise.

Parameters

$name
The long or short option name.

Returns

mixed

The option value or true/false for options without values.

Throws

Webmozart\Console\Api\Args\NoSuchOptionException
If the option does not exist.
public array
# getOptions( boolean $includeDefaults = true )

Returns all options.

Returns all options.

By default, this method also includes the default values set for options with values. You can disable this behavior by passing false for $includeDefaults.

Parameters

$includeDefaults

Whether to return the default values for options that were not set.

Returns

array

The option values and true/false for options without values.

See

Webmozart\Console\Api\Args\Args::getOption()
public static
# setOption( string $name, mixed $value = true )

Sets an option.

Sets an option.

For options with values, you can pass the value in the second argument. The value is converted to the type defined by the argument format.

For options without values, you can omit the second argument. Optionally, you can pass true/false explicitly to enable/disable the option.

Parameters

$name
The long or short option name.
$value
The value to set for the option.

Returns

static
The current instance.

Throws

Webmozart\Console\Api\Args\NoSuchOptionException
If the option does not exist.
public static
# addOptions( array $options )

Sets the values of multiple options.

Sets the values of multiple options.

The existing options are preserved.

Parameters

$options

The options indexed by their long or short names and their values.

Returns

static
The current instance.

See

Webmozart\Console\Api\Args\Args::setOption()
public static
# setOptions( array $options )

Sets the values of multiple options.

Sets the values of multiple options.

The existing options are unset.

Parameters

$options

The options indexed by their long or short names and their values.

Returns

static
The current instance.

See

Webmozart\Console\Api\Args\Args::setOption()
public boolean
# isOptionSet( string $name )

Returns whether an option is set.

Returns whether an option is set.

Parameters

$name
The long or short option name.

Returns

boolean
Returns true if the option is set and false otherwise.
public boolean
# isOptionDefined( string $name )

Returns whether an option is defined in the format.

Returns whether an option is defined in the format.

Parameters

$name
The long or short option name.

Returns

boolean
Returns true if the option exists and false otherwise.
public mixed
# getArgument( string|integer $name )

Returns the value of an argument.

Returns the value of an argument.

If the argument is not set, the default value configured in the argument format is returned.

Parameters

$name

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

Returns

mixed
The value of the argument.

Throws

Webmozart\Console\Api\Args\NoSuchArgumentException
If the argument does not exist.
public array
# getArguments( boolean $includeDefaults = true )

Returns the values of all arguments.

Returns the values of all arguments.

By default, this method also includes the default values of unset arguments. You can disable this behavior by passing false for $includeDefaults.

Parameters

$includeDefaults

Whether to return the default values for arguments that were not set.

Returns

array
The argument values.

See

Webmozart\Console\Api\Args\Args::getArgument()
public static
# setArgument( string|integer $name, mixed $value )

Sets the value of an argument.

Sets the value of an argument.

The value is converted to the type defined by the argument format.

Parameters

$name

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

$value
The value of the argument.

Returns

static
The current instance.

Throws

Webmozart\Console\Api\Args\NoSuchArgumentException
If the argument does not exist.
public static
# addArguments( array $arguments )

Sets the values of multiple arguments.

Sets the values of multiple arguments.

The existing arguments are preserved.

Parameters

$arguments

The argument values indexed by the argument names or their 0-based positions in the argument list.

Returns

static
The current instance.

See

Webmozart\Console\Api\Args\Args::setArgument()
public static
# setArguments( array $arguments )

Sets the values of multiple arguments.

Sets the values of multiple arguments.

The existing arguments are unset.

Parameters

$arguments

The argument values indexed by the argument names or their 0-based positions in the argument list.

Returns

static
The current instance.

See

Webmozart\Console\Api\Args\Args::setArgument()
public boolean
# isArgumentSet( string|integer $name )

Returns whether an argument is set.

Returns whether an argument is set.

Parameters

$name

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

Returns

boolean
Returns true if the argument is set and false otherwise.
public boolean
# isArgumentDefined( string|integer $name )

Returns whether an argument is defined in the format.

Returns whether an argument is defined in the format.

Parameters

$name

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

Returns

boolean
Returns true if the argument exists and false otherwise.
public Webmozart\Console\Api\Args\Format\ArgsFormat
# getFormat( )

Returns the format of the console arguments.

Returns the format of the console arguments.

Returns

Webmozart\Console\Api\Args\Format\ArgsFormat
The format.
public Webmozart\Console\Api\Args\RawArgs
# getRawArgs( )

Returns the raw console arguments.

Returns the raw console arguments.

Returns

Webmozart\Console\Api\Args\RawArgs
The raw arguments.
Webmozart Console API API documentation generated by ApiGen