Parser

class Parser

A parser of CLI options

Copyright
2016 Appertly contributors
License
MIT

Instance Properties

Protected
NameTypeSummary
$optionsOptionSet

Constructors / Destructors

Public
ReturnsSignature and Description
void

Creates a new Parser

Instance Methods

Public
ReturnsSignature and Description
Command
parse(Traversable<string> $arguments)

Parses a list of arguments into a proper CLI command

Protected
ReturnsSignature and Description
void
addOption(string $label, mixed $value, Option $option, Map<string,mixed> $options)

Safely adds an option to the map, taking into account multiplicity.

void
parseLong(string $arg, Vector<string> $args, Map<string,mixed> $options)

Parses a long option (e.g. --dir)

void
parseShort(string $arg, Vector<string> $args, Map<string,mixed> $options)

Parses a short option (e.g. -d)

Property Details

$options

protected OptionSet $options

Method Details

__construct

public function __construct(OptionSet $options)

Creates a new Parser

Parameters
NameTypeDescription
$optionsOptionSet

The options to parse

addOption

protected function addOption(string $label, mixed $value, Option $option, Map<string,mixed> $options)

Safely adds an option to the map, taking into account multiplicity.

Parameters
NameTypeDescription
$labelstring

The label

$valuemixed

The value

$optionOption

The Option object

$optionsMap<string,mixed>

The map to store values

parse

public function parse(Traversable<string> $arguments)

Parses a list of arguments into a proper CLI command

Parameters
NameTypeDescription
$argumentsTraversable<string>

The arguments

Returns
TypeDescription
Command

A parsed command

Throws
TypeConditions
InvalidArgumentException

if the arguments parameter is empty

UnexpectedValueException

if an unknown option is used or a required value is not supplied

parseLong

protected function parseLong(string $arg, Vector<string> $args, Map<string,mixed> $options)

Parses a long option (e.g. --dir)

Parameters
NameTypeDescription
$argstring

The string argument

$argsVector<string>

All remaining arguments

$optionsMap<string,mixed>

The map to store values

parseShort

protected function parseShort(string $arg, Vector<string> $args, Map<string,mixed> $options)

Parses a short option (e.g. -d)

Parameters
NameTypeDescription
$argstring

The string argument

$argsVector<string>

All remaining arguments

$optionsMap<string,mixed>

The map to store values