Zeta Components Manual :: Docs For Class ezcConsoleInput
ConsoleTools::ezcConsoleInput
Class ezcConsoleInput
The ezcConsoleInput class handles the given options and arguments on the console.
This class allows the complete handling of options and arguments submitted to a console based application.
The next example demonstrate how to capture the console options:
- // Register simple parameter -h/--help
- // Register complex parameter -f/--file
- 'f',
- 'file',
- null,
- false,
- 'Process a file.',
- 'Processes a single file.'
- );
- // Manipulate parameter -f/--file after registration
- $file->multiple = true;
- // Register another complex parameter that depends on -f and excludes -h
- 'd',
- 'dir',
- null,
- true,
- 'Process a directory.',
- 'Processes a complete directory.',
- );
- // Register an alias for this parameter
- // Process registered parameters and handle errors
- try
- {
- }
- {
- echo $e->getMessage();
- exit( 1 );
- }
- // Process a single parameter
- if ( $file->value === false )
- {
- echo "Parameter -{$file->short}/--{$file->long} was not submitted.\n";
- }
- elseif ( $file->value === true )
- {
- echo "Parameter -{$file->short}/--{$file->long} was submitted without value.\n";
- }
- else
- {
- echo "Parameter -{$file->short}/--{$file->long} was submitted with value '".var_export($file->value, true)."'.\n";
- }
- // Process all parameters at once:
- {
- switch ( true )
- {
- case $val === false:
- echo "Parameter $paramShort was not submitted.\n";
- break;
- case $val === true:
- echo "Parameter $paramShort was submitted without a value.\n";
- break;
- break;
- default:
- echo "Parameter $paramShort was submitted with value: '$val'.\n";
- break;
- }
- }
Source for this file: /ConsoleTools/src/input.php
Version: | //autogen// |
Constants
TYPE_INT
= 2
|
Option takes an integer value. |
TYPE_NONE
= 1
|
Option does not carry a value. |
TYPE_STRING
= 3
|
Option takes a string value. |
Properties
ezcConsoleArguments | read/write |
$argumentDefinition
Optional argument definition. |
Member Variables
protected array(string=>mixed) |
$properties
= array()
Collection of properties. |
Method Summary
public ezcConsoleInput |
__construct(
)
Creates an input handler. |
protected bool |
argumentsAllowed(
)
Returns if arguments are allowed with the current option submition. |
protected string |
createOptionSynopsis(
$option
, &$usedOptions
, [ $depth
= 0] , $usedOptions
)
Returns the synopsis string for a single option and its dependencies. |
public array(string) |
getArguments(
)
Returns arguments provided to the program. |
public array(array(string)) |
getHelp(
[ $long
= false] , [ $params
= array()] , [ $paramGrouping
= null] )
Get help information for your options. |
public ezcConsoleTable |
getHelpTable(
$table
, [ $long
= false] , [ $params
= array()] , [ $paramGrouping
= null] )
Get help information for your options as a table. |
public string |
getHelpText(
$programDesc
, [ $width
= 80] , [ $long
= false] , [ $params
= null] , [ $paramGrouping
= null] )
Returns a standard help output for your program. |
public ezcConsoleOption |
getOption(
$name
)
Returns the definition object for the option with the name $name. |
public array(string=>ezcConsoleOption) |
getOptions(
)
Returns an array of all registered options. |
public array(string=>mixed) |
getOptionValues(
[ $longnames
= false] )
Returns the values of all submitted options. |
public string |
getSynopsis(
[ $optionNames
= null] )
Returns the synopsis string for the program. |
public bool |
hasOption(
$name
)
Returns true if an option with the given name exists, otherwise false. |
public bool |
helpOptionSet(
)
Returns if a help option was set. |
public void |
process(
[ $args
= null] )
Process the input parameters. |
public void |
registerAlias(
$short
, $long
, $option
)
Registers an alias for an option. |
public ezcConsoleOption |
registerOption(
$option
)
Registers the new option $option. |
public void |
registerOptionString(
$optionDef
)
Registers options according to a string specification. |
public void |
reset(
)
Resets all option and argument values. |
public void |
unregisterAlias(
$short
, $long
)
Removes an alias to an option. |
public void |
unregisterOption(
$option
)
Removes an option. |
Methods
__construct
Creates an input handler.
argumentsAllowed
Returns if arguments are allowed with the current option submition.
createOptionSynopsis
Returns the synopsis string for a single option and its dependencies.
This method returns a part of the program synopsis, specifically for a certain parameter. The method recursively adds depending parameters up to the 2nd depth level to the synopsis. The second parameter is used to store the short names of all options that have already been used in the synopsis (to avoid adding an option twice). The 3rd parameter determines the actual deps in the option dependency recursion to terminate that after 2 recursions.
Parameters:
Name | Type | Description |
---|---|---|
$option |
ezcConsoleOption | The option to include. |
$usedOptions |
array(string) | Array of used option short names. |
$depth |
int | Current recursion depth. |
&$usedOptions |
getArguments
Returns arguments provided to the program.
This method returns all arguments provided to a program in an int indexed array. Arguments are sorted in the way they are submitted to the program. You can disable arguments through the 'arguments' flag of a parameter, if you want to disallow arguments.
Arguments are either the last part of the program call (if the last parameter is not a 'multiple' one) or divided via the '--' method which is commonly used on Unix (if the last parameter accepts multiple values this is required).
getHelp
Get help information for your options.
This method returns an array of help information for your options, indexed by int. Each help info has 2 fields:
- => The options names ("<short> / <long>")
- => The help text (depending on the $long parameter)
If using the second options, you can filter the options shown in the help output (e.g. to show short help for related options). Provide as simple number indexed array of short and/or long values to set a filter.
The $paramGrouping option can be used to group options in the help output. The structure of this array parameter is as follows:
- array(
- 'First section' => array(
- 'input',
- 'output'
- 'overwrite',
- ),
- 'Second section' => array(
- 'v',
- 'h',
- ),
- )
As can be seen, short option names are possible as well as long ones. The key of the first array level is the name of the section, which is assigned to an array of options to group under this section. The $params parameter still influences if an option is displayed at all.
Parameters:
Name | Type | Description |
---|---|---|
$long |
bool | |
$params |
array(string) | |
$paramGrouping |
array(string=>array(string)) |
getHelpTable
Get help information for your options as a table.
This method provides the information returned by ezcConsoleInput::getHelp() in a table.
The $paramGrouping option can be used to group options in the help output. The structure of this array parameter is as follows:
- array(
- 'First section' => array(
- 'input',
- 'output'
- 'overwrite',
- ),
- 'Second section' => array(
- 'v',
- 'h',
- ),
- )
As can be seen, short option names are possible as well as long ones. The key of the first array level is the name of the section, which is assigned to an array of options to group under this section. The $params parameter still influences if an option as displayed at all.
Parameters:
Name | Type | Description |
---|---|---|
$table |
ezcConsoleTable | The table object to fill. |
$long |
bool | Set this to true for getting the long help version. |
$params |
array(string) | Set of option names to generate help for, default is all. |
$paramGrouping |
array(string=>array(string)) |
getHelpText
Returns a standard help output for your program.
This method generates a help text as it's commonly known from Unix command line programs. The output will contain the synopsis, your provided program description and the selected parameter help as also provided by ezcConsoleInput::getHelp(). The returned string can directly be printed to the console.
The $paramGrouping option can be used to group options in the help output. The structure of this array parameter is as follows:
- array(
- 'First section' => array(
- 'input',
- 'output'
- 'overwrite',
- ),
- 'Second section' => array(
- 'v',
- 'h',
- ),
- )
As can be seen, short option names are possible as well as long ones. The key of the first array level is the name of the section, which is assigned to an array of options to group under this section. The $params parameter still influences if an option as displayed at all.
Parameters:
Name | Type | Description |
---|---|---|
$programDesc |
string | The description of your program. |
$width |
int | The width to adjust the output text to. |
$long |
bool | Set this to true for getting the long help version. |
$params |
array(string) | Set of option names to generate help for, default is all. |
$paramGrouping |
array(string=>array(string)) |
getOption
Returns the definition object for the option with the name $name.
This method receives the long or short name of an option and returns the ezcConsoleOption object.
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | Short or long name of the option (without - or --). |
Exceptions:
Type | Description |
---|---|
ezcConsoleOptionNotExistsException |
If requesting a not registered parameter. |
getOptions
Returns an array of all registered options.
Returns an array of all registered options in the following format:
- array(
- ...
- );
getOptionValues
Returns the values of all submitted options.
Returns an array of all values submitted to the options. The array is indexed by the parameters short name (excluding the '-' prefix). The array does not contain any parameter, which value is 'false' (meaning: the parameter was not submitted).
Parameters:
Name | Type | Description |
---|---|---|
$longnames |
bool | Wheather to use longnames for indexing. |
getSynopsis
Returns the synopsis string for the program.
This gives you a synopsis definition for the options and arguments defined with this instance of ezcConsoleInput. You can filter the options named in the synopsis by submitting their short names in an array as the parameter of this method. If the parameter $optionNames is set, only those options are listed in the synopsis.
Parameters:
Name | Type | Description |
---|---|---|
$optionNames |
array(string) |
hasOption
Returns true if an option with the given name exists, otherwise false.
Checks if an option with the given name is registered.
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | Short or long name of the option. |
helpOptionSet
Returns if a help option was set.
This method returns if an option was submitted, which was defined to be a help option, using the isHelpOption flag.
process
Process the input parameters.
Actually process the input options and arguments according to the actual settings.
Per default this method uses $argc and $argv for processing. You can override this setting with your own input, if necessary, using the parameters of this method. (Attention, first argument is always the pro gram name itself!)
All exceptions thrown by this method contain an additional attribute "option" which specifies the parameter on which the error occurred.
Parameters:
Name | Type | Description |
---|---|---|
$args |
array(string) | The arguments |
Exceptions:
Type | Description |
---|---|
ezcConsoleOptionNotExistsException |
If an option that was submitted does not exist. |
ezcConsoleOptionArgumentsViolationException |
If arguments are passed although a parameter disallowed them. |
ezcConsoleOptionDependencyViolationException |
If a dependency rule was violated. |
ezcConsoleOptionExclusionViolationException |
If an exclusion rule was violated. |
ezcConsoleOptionTypeViolationException |
If the type of a submitted value violates the options type rule. |
registerAlias
Registers an alias for an option.
Registers a new alias for an existing option. Aliases can be used as if they were a normal option.
The alias is registered with the short option name $short and the long option name $long. The alias references to the existing option $option.
Parameters:
Name | Type | Description |
---|---|---|
$short |
string | |
$long |
string | |
$option |
ezcConsoleOption |
Exceptions:
Type | Description |
---|---|
ezcConsoleOptionNotExistsException |
If the referenced option is not registered. |
ezcConsoleOptionAlreadyRegisteredException |
If another option/alias has taken the provided short or long name. |
registerOption
Registers the new option $option.
This method adds the new option $option to your option collection. If already an option with the assigned short or long value exists, an exception will be thrown.
Parameters:
Name | Type | Description |
---|---|---|
$option |
ezcConsoleOption |
registerOptionString
Registers options according to a string specification.
Accepts a string to define parameters and registers all parameters as options accordingly. String definition, specified in $optionDef, looks like this:
- [s:|size:][u:|user:][a:|all:]
This string registers 3 parameters: -s / --size -u / --user -a / --all
Parameters:
Name | Type | Description |
---|---|---|
$optionDef |
string |
Exceptions:
Type | Description |
---|---|
ezcConsoleOptionStringNotWellformedException |
If provided string does not have the correct format. |
reset
Resets all option and argument values.
This method is called automatically by process(), if this method is called twice or more, and may also be used to manually reset the values of all registered {@ezcConsoleOption} and ezcConsoleArgument objects.
unregisterAlias
Removes an alias to an option.
This function removes an alias with the short name $short and long name $long.
Parameters:
Name | Type | Description |
---|---|---|
$short |
string | |
$long |
string |
Exceptions:
Type | Description |
---|---|
ezcConsoleOptionNoAliasException |
If the requested short/long name belongs to a real parameter instead. |
unregisterOption
Removes an option.
This function removes an option. All dependencies to that specific option are removed completely from every other registered option.
Parameters:
Name | Type | Description |
---|---|---|
$option |
ezcConsoleOption | The option object to unregister. |
Exceptions:
Type | Description |
---|---|
ezcConsoleOptionNotExistsException |
If requesting a not registered option. |