Skip to content

Commit 9729205

Browse files
committed
Update README.md
1 parent ca008ca commit 9729205

File tree

1 file changed

+100
-3
lines changed

1 file changed

+100
-3
lines changed

README.md

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,101 @@
1-
Console_GetArgs2
2-
================
1+
Command-line arguments parsing class
2+
====================================
33

4-
Port of PEAR Console_GetArgs to PHP 5 with ArrayAccess and Exceptions
4+
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
5+
6+
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.
7+
8+
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.
9+
10+
The class provides the following capabilities:
11+
12+
- Each command line option can take an arbitrary number of arguments.
13+
- Makes the distinction between switches (options without arguments) and options that require arguments.
14+
- Recognizes 'single-argument-options' and 'default-if-set' options.
15+
- Switches and options with arguments can be interleaved in the command line.
16+
- You can specify the maximum and minimum number of arguments an option can take. Use -1 if you don't want to specify an upper bound.
17+
- Specify the default arguments to an option
18+
- Short options can be more than one letter in length.
19+
- A given option may be invoked by multiple names (aliases).
20+
- Understands by default the --help, -h options
21+
- Can return a formatted help text
22+
- Arguments may be specified using the '=' syntax also.
23+
- Short option names may be concatenated (-dvw 100 == -d -v -w 100)
24+
- Can define a default option that will take any arguments added without an option name
25+
- Can pass in a user defined array of arguments instead of using $_SERVER['argv']
26+
27+
Usage
28+
-----
29+
30+
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.
31+
32+
Factory expects an array as parameter.
33+
34+
The format for this array is:
35+
36+
```php
37+
array(
38+
longname => array('short' => Short option name,
39+
'max' => Maximum arguments for option,
40+
'min' => Minimum arguments for option,
41+
'default' => Default option argument,
42+
'desc' => Option description)
43+
)
44+
```
45+
46+
If an option can be invoked by more than one name, they have to be defined by using | as a separator.
47+
For example: name1|name2
48+
This works both in long and short names.
49+
50+
max/min are the most/least number of arguments an option accepts.
51+
52+
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.
53+
Default arguments can be:
54+
55+
- a single value for options that require a single argument,
56+
- an array of values for options with more than one possible arguments.
57+
58+
Default argument(s) are mandatory for 'default-if-set' options.
59+
60+
If max is 0 (option is just a switch), min is ignored.
61+
If max is -1, then the option can have an unlimited number of arguments greater or equal to min.
62+
63+
If max == min == 1, the option is treated as a single argument option.
64+
65+
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)
66+
67+
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.
68+
Thus:
69+
70+
- a --debug in the command line would cause debug = 'default argument'
71+
- a --debug 2 in the command line would result in debug = 2
72+
if not used in the command line, debug will not be defined.
73+
74+
### Example 1
75+
76+
```php
77+
require_once 'Console/Getargs2.php';
78+
try {
79+
$args = new Console_Getargs2($config);
80+
} catch (Console_GetArgs2_UserException $e) {
81+
echo $e->getHelp();
82+
} catch (Console_GetArgs2_Exception $e) {
83+
echo $e->getMessage();
84+
}
85+
echo 'Verbose: '.$args['verbose']."\n";
86+
if (isset($args['bs'])) {
87+
echo 'Block-size: '.(is_array($args['bs']) ? implode(', ', $args['bs'])."\n" : $args['bs']."\n");
88+
} else {
89+
echo "Block-size: undefined\n";
90+
}
91+
echo 'Files: '.(isset($args['file']) ? implode(', ', $args['file'])."\n" : "undefined\n");
92+
if (isset($args['n'])) {
93+
echo 'Nodes: '.(is_array($args['n'])) ? implode(', ', $args['n'])."\n" : $args['n']."\n");
94+
} else {
95+
echo "Nodes: undefined\n";
96+
}
97+
echo 'Log: '.$args['log']."\n";
98+
echo 'Debug: '.(isset($args['d'])) ? "YES\n" : "NO\n");
99+
```
100+
101+
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.

0 commit comments

Comments
 (0)