Skip to content

Commit a6b4b05

Browse files
committed
Refactored config & updated readme
1 parent 17ff846 commit a6b4b05

File tree

4 files changed

+477
-22
lines changed

4 files changed

+477
-22
lines changed

README.md

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,209 @@ CLASS IN DEVELOPMENT
99
## About
1010
Easy 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

src/CliArgs/CliArgs.php

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,33 +54,38 @@ public function __construct(array $config = null)
5454
*/
5555
protected function setConfig(array $config = null)
5656
{
57-
$this->config = $config;
5857
$this->cache = [];
5958
if (!$config) {
59+
$this->config = null;
6060
$this->aliases = null;
6161
return;
6262
}
63-
$this->aliases = [];
64-
foreach ($config as $key => $cfg) {
65-
$this->aliases[$key] = &$config[$key];
66-
$config[$key]['key'] = $key;
6763

68-
if (isset($cfg['alias'])) {
69-
$this->aliases[$cfg['alias']] = &$config[$key];
70-
} else {
71-
$config[$key]['alias'] = null;
72-
}
73-
if (!array_key_exists('default', $cfg)) {
74-
$config[$key]['default'] = null;
75-
}
76-
if (!isset($cfg['help'])) {
77-
$config[$key]['help'] = null;
64+
$newConfig = [];
65+
foreach ($config as $key => $cfg) {
66+
if (is_int($key) && is_string($cfg)) {
67+
$key = $cfg;
68+
$cfg = [];
69+
} elseif (is_string($key) && is_string($cfg)) {
70+
$cfg = ['alias' => $cfg];
7871
}
79-
if (!isset($cfg['filter'])) {
80-
$config[$key]['filter'] = null;
72+
$newConfig[$key] = [
73+
'key' => $key,
74+
'alias' => isset($cfg['alias']) ? $cfg['alias'] : null,
75+
'default' => array_key_exists('default', $cfg) ? $cfg['default'] : null,
76+
'help' => isset($cfg['help']) ? $cfg['help'] : null,
77+
'filter' => isset($cfg['filter']) ? $cfg['filter'] : null,
78+
];
79+
}
80+
$this->config = $newConfig;
81+
82+
$this->aliases = [];
83+
foreach ($this->config as $key => $cfg) {
84+
$this->aliases[$key] = &$this->config[$key];
85+
if ($cfg['alias']) {
86+
$this->aliases[$cfg['alias']] = &$this->config[$key];
8187
}
8288
}
83-
$this->config = $config;
8489
}
8590

8691
/**

src/run.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@
5151
$CliArgs = new CliArgs($config);
5252

5353
//var_dump($CliArgs->isArgExists('help'));
54-
var_dump($CliArgs->getArgs());
54+
var_dump($CliArgs->getArg());

0 commit comments

Comments
 (0)