Fully Qualified Name: | Laminas\Console\Getopt |
Getopt is a class to parse options for command-line applications.
Terminology: Argument: an element of the argv array. This may be part of an option, or it may be a non-option command-line argument. Flag: the letter or word set off by a '-' or '--'. Example: in '--output filename', '--output' is the flag. Parameter: the additional argument that is associated with the option. Example: in '--output filename', the 'filename' is the parameter. Option: the combination of a flag and its parameter, if any. Example: in '--output filename', the whole thing is the option.
The following features are supported:
The format for specifying options uses a PHP associative array. The key is has the format of a list of pipe-separated flag names, followed by an optional '=' to indicate a required parameter or '-' to indicate an optional parameter. Following that, the type of parameter may be specified as 's' for string, 'w' for word, or 'i' for integer.
Examples:
The values in the associative array are strings that are used as brief descriptions of the options when printing a usage message.
The simpler format for specifying options used by PHP's getopt() function is also supported. This is similar to GNU getopt and shell getopt format.
Example: 'abc:' means options '-a', '-b', and '-c' are legal, and the latter requires a string parameter.
Name | Description | Defined By |
---|---|---|
__construct() | The constructor takes one to three parameters. | Getopt |
__get() | Return the state of the option seen on the command line of the current application invocation. This function returns true, or the parameter to the option, if any. If the option was not given, this function returns null. | Getopt |
__isset() | Test whether a given option has been seen. | Getopt |
__set() | Set the value for a given option. | Getopt |
__toString() | Return the current set of options and parameters seen as a string. | Getopt |
__unset() | Unset an option. | Getopt |
addArguments() | Define additional command-line arguments. | Getopt |
addRules() | Define additional option rules. | Getopt |
getArguments() | Getopt | |
getOption() | Return the state of the option seen on the command line of the current application invocation. | Getopt |
getOptions() | Return a list of options that have been seen in the current argv. | Getopt |
getRemainingArgs() | Return the arguments from the command-line following all options found. | Getopt |
getUsageMessage() | Return a useful option reference, formatted for display in an error message. | Getopt |
parse() | Parse command-line arguments and find both long and short options. | Getopt |
setAliases() | Define aliases for options. | Getopt |
setArguments() | Define full set of command-line arguments. | Getopt |
setHelp() | Define help messages for options. | Getopt |
setOption() | Define one configuration option as a key/value pair. | Getopt |
setOptionCallback() | Getopt | |
setOptions() | Define multiple configuration options from an associative array. | Getopt |
toArray() | Return the current set of options and parameters seen as an array of canonical options and parameters. | Getopt |
toJson() | Return the current set of options and parameters seen in Json format. | Getopt |
toString() | Return the current set of options and parameters seen as a string. | Getopt |
toXml() | Return the current set of options and parameters seen in XML format. | Getopt |
The constructor takes one to three parameters.
The first parameter is $rules, which may be a string for gnu-style format, or a structured array for Laminas-style format.
The second parameter is $argv, and it is optional. If not specified, $argv is inferred from the global argv.
The third parameter is an array of configuration parameters to control the behavior of this instance of Getopt; it is optional.
Parameter Name | Type | Description |
---|---|---|
$rules | array | |
$argv | array | |
$getoptConfig | array |
Returns:
Return the state of the option seen on the command line of the current application invocation. This function returns true, or the parameter to the option, if any. If the option was not given, this function returns null.
The magic __get method works in the context of naming the option as a virtual member of this class.
Parameter Name | Type | Description |
---|---|---|
$key | string |
Returns: string
Test whether a given option has been seen.
Parameter Name | Type | Description |
---|---|---|
$key | string |
Returns: bool
Set the value for a given option.
Parameter Name | Type | Description |
---|---|---|
$key | string | |
$value | string |
Returns:
Return the current set of options and parameters seen as a string.
Returns: string
Unset an option.
Parameter Name | Type | Description |
---|---|---|
$key | string |
Returns:
Define additional command-line arguments.
These are appended to those defined when the constructor was called.
Parameter Name | Type | Description |
---|---|---|
$argv | array |
Returns: self
Define additional option rules.
These are appended to the rules defined when the constructor was called.
Parameter Name | Type | Description |
---|---|---|
$rules | array |
Returns: self
Returns: void
Return the state of the option seen on the command line of the current application invocation.
This function returns true, or the parameter value to the option, if any. If the option was not given, this function returns false.
Parameter Name | Type | Description |
---|---|---|
$flag | string |
Returns: mixed
Return a list of options that have been seen in the current argv.
Returns: array
Return the arguments from the command-line following all options found.
Returns: array
Return a useful option reference, formatted for display in an error message.
Note that this usage information is provided in most Exceptions generated by this class.
Returns: string
Parse command-line arguments and find both long and short options.
Also find option parameters, and remaining arguments after all options have been parsed.
Returns: self
Define aliases for options.
The parameter $aliasMap is an associative array mapping option name (short or long) to an alias.
Parameter Name | Type | Description |
---|---|---|
$aliasMap | array |
Returns: self
Define full set of command-line arguments.
These replace any currently defined.
Parameter Name | Type | Description |
---|---|---|
$argv | array |
Returns: self
Define help messages for options.
The parameter $helpMap is an associative array mapping option name (short or long) to the help string.
Parameter Name | Type | Description |
---|---|---|
$helpMap | array |
Returns: self
Define one configuration option as a key/value pair.
These are not program options, but properties to configure the behavior of Laminas\Console\Getopt.
Parameter Name | Type | Description |
---|---|---|
$configKey | string | |
$configValue | string |
Returns: self
Parameter Name | Type | Description |
---|---|---|
$option | string | The |
$callback | callable | The |
Returns: self
Define multiple configuration options from an associative array.
These are not program options, but properties to configure the behavior of Laminas\Console\Getopt.
Parameter Name | Type | Description |
---|---|---|
$getoptConfig | array |
Returns: self
Return the current set of options and parameters seen as an array of canonical options and parameters.
Clusters have been expanded, and option aliases have been mapped to their primary option names.
Returns: array
Return the current set of options and parameters seen in Json format.
Returns: string
Return the current set of options and parameters seen as a string.
Returns: string
Return the current set of options and parameters seen in XML format.
Returns: string