@@ -9,17 +9,209 @@ CLASS IN DEVELOPMENT
99## About
1010Easy way to gets options from the command line argument list.
1111
12+ ## Note
13+ This class is not workable when [ register_argc_argv] ( http://php.net/manual/en/ini.core.php#ini.register-argc-argv ) is disabled.
14+
1215## Usage
1316
1417### Config
1518
19+ Note: all params from cli that you want to use should be specified in config, otherwise they will be ignored.
20+ ``` php
21+ $config = [
22+ // You should specify key as name of option from the command line argument list.
23+ // Example, name <param-name > for --param-name option
24+ 'param-name' => [
25+
26+ 'alias' => 'p',
27+ // [optional], [string]
28+ // Alias helps to have short or long name for this key.
29+ // Example, name <p > for -p option
30+
31+ 'default' => false,
32+ // [optional], [mixed], [default = null]
33+ // Default value will returned if param is not setted
34+ // or params has not value.
35+
36+ 'help' => 'p',
37+ // [optional], [string]
38+ // Text that will returned, if you request help
39+
40+ 'filter' => 'int',
41+ // [optional], [string | array | function]
42+ // Filter for the return value.
43+ // You can use next filters: flag, bool, int, float, help, json, <array >, <function >
44+
45+ // 'int' - cast to integer before return.
46+ // 'float' - cast to float before return.
47+ // 'bool' - cast to float before return. Yes, true, 1 = TRUE, other = FALSE
48+ // 'json' - decode JSON data before return.
49+ // 'flag' - will return TRUE, if key is exists in command line argument list.
50+ // <array > - use array for enums. Example use ['a', 'b', 'c'] to get only one of these.
51+ // <function > - use function($value, $default) { ... } to process value by yourself
52+ // 'help' - use this filter if you want generate and return help about all config.
53+ ]
54+ ];
55+
56+ $CliArgs = new CliArgs($config);
57+ ```
58+
59+ Examples of config:
60+
61+ #0
1662``` php
1763
64+ // Simple configs
65+
66+ // The config1 and config2 are equal
67+ $config1 = ['foo', 'bar', 'a'];
68+ $config2 = [
69+ 'foo' => [],
70+ 'bar' => [],
71+ 'a' => [],
72+ ];
73+
74+ // The config3 and config4 are equal
75+ $config = ['foo' => 'f', 'bar' => 'b', 'a'];
76+ $config = [
77+ 'foo' => [
78+ 'alias' => 'f',
79+ ],
80+ 'bar' => [
81+ 'alias' => 'b',
82+ ],
83+ 'a' => [],
84+ ];
85+ ```
86+
87+ #1
88+ ``` php
89+ $config = [
90+ 'help' => [
91+ 'alias' => 'h',
92+ 'filter' => 'help',
93+ 'help' => 'Show help about all options',
94+ ],
95+ 'data' => [
96+ 'alias' => 'd',
97+ 'filter' => 'json',
98+ 'help' => 'Some description about this param',
99+ ],
100+ 'user-id' => [
101+ 'alias' => 'u',
102+ 'filter' => 'int',
103+ 'help' => 'Some description about this param',
104+ ]
105+ ];
106+ $CliArgs = new CliArgs($config);
18107```
108+ ```
109+ // Show help
110+ > some-script.php --help
111+ // <?php if ($CliArgs->isFlagExists('help', 'h')) echo $CliArgs->getArg('help'); ?>
112+
113+ // Show help only for param data
114+ // > some-script.php --help data
115+ // <?php if ($CliArgs->isFlagExists('help', 'h')) echo $CliArgs->getArg('help'); ?>
116+
117+ // All the same:
118+ // > some-script.php --data='{"foo":"bar"}' --user-id=42
119+ // or
120+ // > some-script.php --data '{"foo":"bar"}' --user-id 42
121+ // or
122+ // > some-script.php -d '{"foo":"bar"}' --user-id 42
123+ // or
124+ // > some-script.php -d '{"foo":"bar"}' -u 42
125+
126+ <?php
127+ print_r($CliArgs->getArg('data'));
128+ print_r($CliArgs->getArg('d'));
129+ print_r($CliArgs->getArg('user-id'));
130+ print_r($CliArgs->getArg('u'));
131+ ```
132+
133+ #2
134+ ``` php
135+ $config = [
136+ 'flag' => [
137+ 'alias' => 'f',
138+ 'filter' => 'flag',
139+ ],
140+ 'id' => [
141+ 'filter' => 'int',
142+ ],
143+ 'any' => [],
144+ ];
145+ ```
146+ ```
147+ > some-script.php --flag
148+
149+ > some-script.php -f
19150
151+ > some-script.php -f --id=42 --any="any value"
152+
153+ > some-script.php --any="any value"
154+
155+ <?php
156+ print_r($CliArgs->isFlagExists('flag', 'f'));
157+ print_r($CliArgs->getArg('data'));
158+ print_r($CliArgs->getArg('d'));
159+ print_r($CliArgs->getArg('user-id'));
160+ print_r($CliArgs->getArg('u'));
161+
162+ ```
163+
164+ #3
165+ ``` php
166+ $config = [
167+ 'name' => [
168+ 'alias' => 'n',
169+ 'filter' => function($name, $default) {
170+ return $name ? mb_convert_case($name, MB_CASE_TITLE, 'UTF-8') : $defult;
171+ },
172+ 'default' => 'No name',
173+ ],
174+ 'sex' => [
175+ 'alias' => 's',
176+ 'filter' => ['m', 'f'],
177+ 'default' => null,
178+ ],
179+ 'city' => [
180+ 'alias' => 'c',
181+ 'filter' => function($city) {
182+ // ... some checks of city
183+ },
184+ ],
185+ 'email' => [
186+ 'alias' => 'e',
187+ 'filter' => function($city) {
188+ // ... some checks of email
189+ },
190+ ]
191+ ];
192+ ```
193+ ```
194+ > some-script.php --name alexander
195+
196+ > some-script.php -f
197+
198+ > some-script.php -f --id=42 --any="any value"
199+
200+ > some-script.php --any="any value"
201+
202+ <?php
203+ print_r($CliArgs->getArg('name'));
204+ print_r($CliArgs->('d'));
205+ print_r($CliArgs->getArg('user-id'));
206+ print_r($CliArgs->getArg('u'));
207+ ```
20208### Create a new instance
21209``` php
22210
211+ // simple config
212+ $config = ['foo' => 'f', 'bar' => 'b'];
213+ $CliArgs = new CliArgs($config);
214+
23215```
24216
25217## Installation
0 commit comments