dev-master
9999999-devConsole arguments parser and rules.
BSD-2-Clause
The Requires
- php >=5.3.0
console cli getopt
1.0.0
1.0.0.0Console arguments parser and rules.
BSD-2-Clause
The Requires
- php >=5.3.0
console cli getopt
Wallogit.com
2017 © Pedro Peláez
Console arguments parser and rules.
This implementation was freely inspired by a python module called getargs by Vinod Vijayarajan and a perl CPAN module called Getopt::Simple by Ron Savage, (*1)
This class implements a Command Line Parser that your cli applications can use to parse command line arguments found in $_SERVER['argv'] or a user defined array., (*2)
It gives more flexibility and error checking than Console_Getopt. It also performs some arguments validation and is capable to return a formatted help text to the user, based on the configuration it is given., (*3)
The class provides the following capabilities:, (*4)
The constructor will return a new Console_Getargs2 object built using the given configuration options. If the configuration or the command line options contain errors, the returned object will in fact be an Exception explaining the cause of the error., (*5)
Factory expects an array as parameter., (*6)
The format for this array is:, (*7)
array(
longname => array('short' => Short option name,
'max' => Maximum arguments for option,
'min' => Minimum arguments for option,
'default' => Default option argument,
'desc' => Option description)
)
If an option can be invoked by more than one name, they have to be defined by using | as a separator. For example: name1|name2 This works both in long and short names., (*8)
max/min are the most/least number of arguments an option accepts., (*9)
The 'defaults' field is optional and is used to specify default arguments to an option. These will be assigned to the option if it is not used in the command line. Default arguments can be:, (*10)
Default argument(s) are mandatory for 'default-if-set' options., (*11)
If max is 0 (option is just a switch), min is ignored. If max is -1, then the option can have an unlimited number of arguments greater or equal to min., (*12)
If max == min == 1, the option is treated as a single argument option., (*13)
If max >= 1 and min == 0, the option is treated as a 'default-if-set' option. This implies that it will get the default argument only if the option is used in the command line without any value. (Note: defaults must be specified for 'default-if-set' options), (*14)
If the option is not in the command line, the defaults are not applied. If an argument for the option is specified on the command line, then the given argument is assigned to the option. Thus:, (*15)
require_once 'Console/Getargs2.php';
try {
$args = new Console_Getargs2($config);
} catch (Console_GetArgs2_UserException $e) {
echo $e->getHelp();
} catch (Console_GetArgs2_Exception $e) {
echo $e->getMessage();
}
echo 'Verbose: '.$args['verbose']."\n";
if (isset($args['bs'])) {
echo 'Block-size: '.(is_array($args['bs']) ? implode(', ', $args['bs'])."\n" : $args['bs']."\n");
} else {
echo "Block-size: undefined\n";
}
echo 'Files: '.(isset($args['file']) ? implode(', ', $args['file'])."\n" : "undefined\n");
if (isset($args['n'])) {
echo 'Nodes: '.(is_array($args['n']) ? implode(', ', $args['n'])."\n" : $args['n']."\n");
} else {
echo "Nodes: undefined\n";
}
echo 'Log: '.$args['log']."\n";
echo 'Debug: '.(isset($args['d']) ? "YES\n" : "NO\n");
If you don't want to require any option name for a set of arguments, or if you would like any "leftover" arguments assigned by default, you can create an option named CONSOLE_GETARGS_PARAMS that will grab any arguments that cannot be assigned to another option. The rules for CONSOLE_GETARGS_PARAMS are still the same. If you specify that two values must be passed then two values must be passed. See the example script for a complete example., (*16)
More examples : https://github.com/pear/Console_Getargs/tree/master/examples, (*17)
Console arguments parser and rules.
BSD-2-Clause
console cli getopt
Console arguments parser and rules.
BSD-2-Clause
console cli getopt