Skip to content

Commit 2966948

Browse files
committed
v3.0.0
1 parent a19b34f commit 2966948

File tree

10 files changed

+401
-129
lines changed

10 files changed

+401
-129
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## CHANGELOG
2+
3+
### v3.0.0 (2019-02-02)
4+
Major:
5+
- Removed method `isFlagOrAliasExists`, please use isFlagExist
6+
- Renamed method `isFlagExists` to `isFlagExist`, changed second parameter of the method.
7+
- Will be thrown `CongifErrorException` if it is passed wrong config to constructor.
8+
- Method `getArg($key)` returns `true` or `false` when filter is `flag`.
9+
10+
Patch:
11+
- Fixed default default arguments of default params

README.md

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
2-
# CliArgs v2.1.0 for PHP >= 5.5
2+
# CliArgs v3.0.0 for PHP >= 5.5
33

44
## About
55
Class **CliArgs** helps to get options from the command line argument list easy.
@@ -76,7 +76,7 @@ $config = [
7676
// 'float' - cast to float before return.
7777
// 'bool' - cast to bool before return. Yes, true, 1 = TRUE, other = FALSE
7878
// 'json' - decode JSON data before return.
79-
// 'flag' - will return TRUE, if key is exists in command line argument list.
79+
// 'flag' - will return TRUE, if key is exists in command line argument list, otherwise - FALSE
8080
// <array> - use array for enums. Example use ['a', 'b', 'c'] to get only one of these.
8181
// <callable> - use function($value, $default) { ... } to process value by yourself
8282
]
@@ -87,7 +87,7 @@ $CliArgs = new CliArgs($config);
8787

8888
Examples of config:
8989

90-
0
90+
Example 1
9191
```php
9292

9393
// Simple configs
@@ -113,7 +113,7 @@ $config4 = [
113113
];
114114
```
115115

116-
1
116+
Example 2
117117
```php
118118
$config = [
119119
'help' => [
@@ -136,15 +136,15 @@ $CliArgs = new CliArgs($config);
136136
```
137137
Show help
138138
> some-script.php --help
139-
<?php if ($CliArgs->isFlagExists('help', 'h')) echo $CliArgs->getHelp('help'); ?>
139+
<?php if ($CliArgs->isFlagExist('help', 'h')) echo $CliArgs->getHelp('help'); ?>
140140
141141
Show help only for param data
142142
> some-script.php --help data
143-
<?php if ($CliArgs->isFlagExists('help', 'h')) echo $CliArgs->getHelp('help'); ?>
143+
<?php if ($CliArgs->isFlagExist('help', 'h')) echo $CliArgs->getHelp('help'); ?>
144144
145145
Show help for all params data
146146
> some-script.php --help data
147-
<?php if ($CliArgs->isFlagExists('help', 'h')) echo $CliArgs->getHelp(); ?>
147+
<?php if ($CliArgs->isFlagExist('help', 'h')) echo $CliArgs->getHelp(); ?>
148148
149149
All the same:
150150
> some-script.php --data='{"foo":"bar"}' --user-id=42
@@ -162,7 +162,7 @@ or
162162
print_r($CliArgs->getArg('u'));
163163
```
164164

165-
2
165+
Example 3
166166
```php
167167
$config = [
168168
'flag' => [
@@ -185,15 +185,15 @@ or
185185
> some-script.php --any="any value"
186186
187187
<?php
188-
print_r($CliArgs->isFlagExists('flag', 'f'));
188+
print_r($CliArgs->isFlagExist('flag', 'f'));
189189
print_r($CliArgs->getArg('data'));
190190
print_r($CliArgs->getArg('d'));
191191
print_r($CliArgs->getArg('user-id'));
192192
print_r($CliArgs->getArg('u'));
193193
194194
```
195195

196-
3
196+
Example 4
197197
```php
198198
$config = [
199199
'name' => [
@@ -251,15 +251,15 @@ $CliArgs = new CliArgs($config);
251251
> example.php --foo Hello --bar World
252252
```
253253

254-
##### new CliArgs([array|null])
255-
Constructor.
254+
##### new CliArgs(array|null $config = null)
255+
Constructor. If config contents wrong aliases then ConfigErrorException will be thrown.
256256
```php
257257
$config = ['foo' => 'f', 'bar' => 'b'];
258258
$CliArgs = new CliArgs($config);
259259
```
260260

261261
##### getArgs(): array
262-
Get all params.
262+
The method returns all passed arguments which is specified in config.
263263
```php
264264
$argv = $CliArgs->getArgs();
265265
print_r($argv);
@@ -269,38 +269,26 @@ print_r($argv);
269269
// )
270270
```
271271

272-
##### getArg(string $arg): mixed
273-
Get one param
272+
##### getArg(string $key): mixed | null
273+
Returns value for argument by key. If argument is not set, then it will return default value or `null`
274274
```php
275275
$arg = $CliArgs->getArg('foo');
276276
// or $CliArgs->getArg('f');
277277
echo $arg; // Hello
278278
```
279279

280-
##### isFlagOrAliasExists(string $arg): bool
281-
Checks if the given key (or alias) exists in the arguments console list.
282-
Returns true if $arg or his alias are exists
280+
##### isFlagExist(string $key, boolean $checkAlias = true): bool
281+
Return `true` if key exists, otherwise the method returns `false`
282+
If `$checkAlias` is `true`, then the method will check key and alias, and will return `true` if key or alias exists.
283+
If `$checkAlias` is `false`, then the method will check only key and will return `true` only if key exists.
283284
```php
284-
echo $CliArgs->isFlagOrAliasExists('f'); // true
285-
echo $CliArgs->isFlagOrAliasExists('foo'); // true
286-
287-
$CliArgs->isFlagExists('foo', 'f') === $CliArgs->isFlagOrAliasExists('f');
288-
```
289-
290-
##### isFlagExists(string $arg, string|null $alias): bool
291-
Checks if the given key exists in the arguments console list.
292-
Check of key or alias is independent.
293-
Returns true if $arg or $alias are exists
294-
```php
295-
echo $CliArgs->isFlagExists('f'); // false
296-
echo $CliArgs->isFlagExists('foo'); // true
297-
echo $CliArgs->isFlagExists('foo', 'f'); // true
298-
299-
$CliArgs->isFlagExists('foo', 'f') === $CliArgs->isFlagOrAliasExists('f');
285+
echo $CliArgs->isFlagExist('f'); // false
286+
echo $CliArgs->isFlagExist('foo'); // true
287+
echo $CliArgs->isFlagExist('foo', 'f'); // true
300288
```
301289

302290
##### getArguments(): array
303-
Get prepared ARGV
291+
Get prepared arguments from ARGV
304292
```php
305293
print_r($CliArgs->getArguments());
306294
// array(
@@ -335,4 +323,4 @@ and add dependency to your project:
335323

336324
## Something doesn't work
337325

338-
Feel free to fork project, fix bugs and finally request for pull
326+
Feel free to fork project, fix bugs and finally request for pull (do not forget write tests please)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cheprasov/php-cli-args",
3-
"version": "2.1.0",
3+
"version": "3.0.0",
44
"description": "Easy way to gets options from the command line argument list",
55
"homepage": "http://github.com/cheprasov/php-cli-args",
66
"minimum-stability": "stable",

src/CliArgs/CliArgs.php

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
*/
1111
namespace CliArgs;
1212

13+
use CliArgs\Exception\ConfigErrorException;
14+
1315
class CliArgs
1416
{
15-
const VERSION = '2.1.0';
17+
const VERSION = '3.0.0';
1618

1719
const FILTER_BOOL = 'bool';
1820
const FILTER_FLAG = 'flag';
@@ -42,6 +44,7 @@ class CliArgs
4244

4345
/**
4446
* @param array $config
47+
* @throws ConfigErrorException
4548
*/
4649
public function __construct(array $config = null)
4750
{
@@ -50,6 +53,7 @@ public function __construct(array $config = null)
5053

5154
/**
5255
* @param array|null $config
56+
* @throws ConfigErrorException
5357
*/
5458
protected function setConfig(array $config = null)
5559
{
@@ -71,7 +75,7 @@ protected function setConfig(array $config = null)
7175
$newConfig[$key] = [
7276
'key' => $key,
7377
'alias' => isset($cfg['alias']) ? $cfg['alias'] : null,
74-
'default' => array_key_exists('default', $cfg) ? $cfg['default'] : null,
78+
'default' => isset($cfg['default']) ? $cfg['default'] : null,
7579
'help' => isset($cfg['help']) ? $cfg['help'] : null,
7680
'filter' => isset($cfg['filter']) ? $cfg['filter'] : null,
7781
];
@@ -80,8 +84,14 @@ protected function setConfig(array $config = null)
8084

8185
$this->aliases = [];
8286
foreach ($this->config as $key => $cfg) {
87+
if (isset($this->aliases[$key])) {
88+
throw new ConfigErrorException("Key or alias `{$key}` is already defined");
89+
}
8390
$this->aliases[$key] = &$this->config[$key];
8491
if ($cfg['alias']) {
92+
if (isset($this->aliases[$cfg['alias']])) {
93+
throw new ConfigErrorException("Key or alias `{$cfg['alias']}` is already defined");
94+
}
8595
$this->aliases[$cfg['alias']] = &$this->config[$key];
8696
}
8797
}
@@ -101,61 +111,71 @@ public function getArguments()
101111

102112
/**
103113
* Checks if the given key exists in the arguments console list. Returns true if $arg or $alias are exists
104-
* @param string $arg
105-
* @param string|null $alias
114+
* @param string $key
115+
* @param boolean $checkAlias
106116
* @return bool
107117
*/
108-
public function isFlagExists($arg, $alias = null)
118+
public function isFlagExist($key, $checkAlias = true)
109119
{
110-
return array_key_exists($arg, $this->getArguments()) || $alias && array_key_exists($alias, $this->getArguments());
120+
$arguments = $this->getArguments();
121+
if (array_key_exists($key, $arguments)) {
122+
return true;
123+
}
124+
125+
if ($checkAlias && ($alias = $this->getAlias($key))) {
126+
return array_key_exists($alias, $arguments);
127+
}
128+
129+
return false;
111130
}
112131

113132
/**
114-
* Checks if the given key (or alias) exists in the arguments console list.
115-
* @param string $arg
116-
* @return bool
133+
* @param string $key
134+
* @return string|null
117135
*/
118-
public function isFlagOrAliasExists($arg)
136+
protected function getAlias($key)
119137
{
120-
if (!isset($this->aliases[$arg])) {
121-
return false;
138+
if (!isset($this->aliases[$key])) {
139+
return null;
122140
}
123-
$key = $this->aliases[$arg]['key'];
124-
if (isset($this->aliases[$arg]['alias'])) {
125-
$alias = $this->aliases[$arg]['alias'];
126-
} else {
127-
$alias = null;
141+
$cfg = $this->aliases[$key];
142+
if ($cfg['key'] === $key) {
143+
return $cfg['alias'];
128144
}
129-
return $this->isFlagExists($key, $alias);
145+
return $cfg['key'];
130146
}
131147

132148
/**
133149
* Get one param
134-
* @param string $arg
150+
* @param string $key
135151
* @return mixed
136152
*/
137-
public function getArg($arg)
153+
public function getArg($key)
138154
{
139-
if (!$cfg = $this->getArgFromConfig($arg)) {
155+
if (!$cfg = $this->getArgConfig($key)) {
140156
return null;
141157
}
142-
if (array_key_exists($arg, $this->cache)) {
158+
if (array_key_exists($key, $this->cache)) {
143159
return $this->cache[$cfg['key']];
144160
}
145161
$arguments = $this->getArguments();
146162

147-
if ($this->isFlagExists($cfg['key'])) {
163+
if ($this->isFlagExist($cfg['key'], false)) {
148164
$value = $arguments[$cfg['key']];
149-
} elseif ($this->isFlagExists($cfg['alias'])) {
165+
} elseif ($cfg['alias'] && $this->isFlagExist($cfg['alias'], false)) {
150166
$value = $arguments[$cfg['alias']];
151167
} elseif (isset($cfg['default'])) {
152-
return $cfg['default'];
168+
$value = $cfg['default'];
153169
} else {
154-
return null;
170+
$value = null;
155171
}
156172

157-
if ($cfg['filter'] && $cfg['default'] !== $value || $cfg['filter'] === self::FILTER_FLAG) {
158-
$value = $this->filterValue($cfg['filter'], $value, $cfg['default'] ?: null);
173+
if ($cfg['filter']) {
174+
if ($cfg['filter'] === self::FILTER_FLAG) {
175+
$value = $this->isFlagExist($key);
176+
} elseif ($cfg['default'] !== $value) {
177+
$value = $this->filterValue($cfg['filter'], $value, $cfg['default']);
178+
}
159179
}
160180

161181
$this->cache[$cfg['key']] = $value;
@@ -171,7 +191,7 @@ public function getArgs()
171191
{
172192
$args = [];
173193
$arguments = $this->getArguments();
174-
foreach ($arguments as $key => $arg) {
194+
foreach ($arguments as $key => $value) {
175195
if (!isset($this->aliases[$key])) {
176196
continue;
177197
}
@@ -191,9 +211,6 @@ protected function filterValue($filter, $value, $default = null)
191211
{
192212
if (is_string($filter)) {
193213
switch ($filter) {
194-
case self::FILTER_FLAG:
195-
return true;
196-
197214
case self::FILTER_BOOL:
198215
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
199216

@@ -218,13 +235,13 @@ protected function filterValue($filter, $value, $default = null)
218235
}
219236

220237
/**
221-
* @param $arg
238+
* @param $key
222239
* @return null
223240
*/
224-
protected function getArgFromConfig($arg)
241+
protected function getArgConfig($key)
225242
{
226-
if (isset($this->aliases[$arg])) {
227-
return $this->aliases[$arg];
243+
if (isset($this->aliases[$key])) {
244+
return $this->aliases[$key];
228245
}
229246
return null;
230247
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* This file is part of CliArgs.
4+
* git: https://github.com/cheprasov/php-cli-args
5+
*
6+
* (C) Alexander Cheprasov <acheprasov84@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
namespace CliArgs\Exception;
12+
13+
class ConfigErrorException extends Exception {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* This file is part of CliArgs.
4+
* git: https://github.com/cheprasov/php-cli-args
5+
*
6+
* (C) Alexander Cheprasov <acheprasov84@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
namespace CliArgs\Exception;
12+
13+
class Exception extends \Exception {}

0 commit comments

Comments
 (0)