Skip to content

Commit 5c5cf14

Browse files
committed
pre release v1.0.0
1 parent 4dcb100 commit 5c5cf14

File tree

6 files changed

+230
-23
lines changed

6 files changed

+230
-23
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
sudo: required
2-
31
language: php
42
php:
53
- '5.5'

README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
22
# CliArgs v1.0.0 for PHP >= 5.5
33

4-
##
5-
6-
WORK IN PROGRESS
7-
CLASS IN DEVELOPMENT
8-
94
## About
10-
Easy way to gets options from the command line argument list.
5+
Class **CliArgs** helps to get options from the command line argument list easy.
6+
7+
## Features
8+
- CliArgs uses **$GLOBAL['argv']** as base, and it does not use function **getopt()**.
9+
- It does not implement logic for 'required', it should be in your side.
10+
- It helps to get options easy from command line argument list.
11+
- It generates help info about options based on config.
12+
- Flexible configuration and data filtering.
1113

1214
## Note
1315
This class is not workable when [register_argc_argv](http://php.net/manual/en/ini.core.php#ini.register-argc-argv) is disabled.
@@ -38,7 +40,7 @@ $config = [
3840
// Text that will returned, if you request help
3941

4042
'filter' => 'int',
41-
// [optional], [string | array | function]
43+
// [optional], [string | array | callable]
4244
// Filter for the return value.
4345
// You can use next filters: flag, bool, int, float, help, json, <array>, <function>
4446

@@ -48,7 +50,7 @@ $config = [
4850
// 'json' - decode JSON data before return.
4951
// 'flag' - will return TRUE, if key is exists in command line argument list.
5052
// <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
53+
// <callable> - use function($value, $default) { ... } to process value by yourself
5254
// 'help' - use this filter if you want generate and return help about all config.
5355
]
5456
];
@@ -218,14 +220,14 @@ $CliArgs = new CliArgs($config);
218220
> example.php --foo Hello --bar World
219221
```
220222

221-
###### new CliArgs([array|null])
223+
##### new CliArgs([array|null])
222224
Constructor.
223225
```php
224226
$config = ['foo' => 'f', 'bar' => 'b'];
225227
$CliArgs = new CliArgs($config);
226228
```
227229

228-
###### getArgs(): array
230+
##### getArgs(): array
229231
Get all params.
230232
```php
231233
$argv = $CliArgs->getArgs();
@@ -236,15 +238,15 @@ print_r($argv);
236238
// )
237239
```
238240

239-
###### getArg(string $arg): mixed
241+
##### getArg(string $arg): mixed
240242
Get one param
241243
```php
242244
$arg = $CliArgs->getArg('foo');
243245
// or $CliArgs->getArg('f');
244246
echo $arg; // Hello
245247
```
246248

247-
###### isFlagExists(string $arg, string|null $alias): bool
249+
##### isFlagExists(string $arg, string|null $alias): bool
248250
Checks if the given key exists in the arguments console list.
249251
Returns true if $arg or $alias are exists
250252
```php
@@ -253,7 +255,7 @@ echo $CliArgs->isFlagExists('foo'); // true
253255
echo $CliArgs->isFlagExists('foo', 'f'); // true
254256
```
255257

256-
###### getArguments(): array
258+
##### getArguments(): array
257259
Get prepared ARGV
258260
```php
259261
print_r($CliArgs->getArguments());
@@ -280,7 +282,6 @@ and add dependency to your project:
280282

281283
./vendor/bin/phpunit
282284

283-
284285
## Something doesn't work
285286

286287
Feel free to fork project, fix bugs and finally request for pull

src/CliArgs/CliArgs.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,12 @@ protected function filterValue($filter, $value, $default = null)
192192
}
193193
return $default;
194194
}
195+
if (is_callable($filter)) {
196+
return call_user_func($filter, $value, $default);
197+
}
195198
if (is_array($filter)) {
196199
return in_array($value, $filter, true) ? $value : $default;
197200
}
198-
if (is_callable($filter)) {
199-
return $filter($value, $default);
200-
}
201201
return $default;
202202
}
203203

tests/Build/VersionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
use CliArgs\CliArgs;
1414

15-
class VersionTest extends \PHPUnit_Framework_TestCase {
16-
15+
class VersionTest extends \PHPUnit_Framework_TestCase
16+
{
1717
public function test_version() {
1818
chdir(__DIR__.'/../../');
1919
$composer = json_decode(file_get_contents('./composer.json'), true);

tests/Functional/CliArgsTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
use CliArgs\CliArgs;
1414

15-
class CliArgsTest extends \PHPUnit_Framework_TestCase {
16-
15+
class CliArgsTest extends \PHPUnit_Framework_TestCase
16+
{
1717
public function providerTestFilterHelp()
1818
{
1919
return [
@@ -105,6 +105,7 @@ public function providerTestFilterHelp()
105105
}
106106

107107
/**
108+
* @see \CliArgs\CliArgs::getArg
108109
* @dataProvider providerTestFilterHelp
109110
* @param array $argv
110111
* @param array $config
@@ -385,6 +386,7 @@ public function providerTestFilters()
385386
}
386387

387388
/**
389+
* @see \CliArgs\CliArgs::getArg
388390
* @dataProvider providerTestFilters
389391
* @param array $argv
390392
* @param array $config
@@ -433,6 +435,7 @@ public function providerTestGetArguments()
433435
}
434436

435437
/**
438+
* @see \CliArgs\CliArgs::getArguments
436439
* @dataProvider providerTestGetArguments
437440
* @param array $argv
438441
* @param array $arguments
@@ -487,6 +490,7 @@ public function providerTestGetArg()
487490
}
488491

489492
/**
493+
* @see \CliArgs\CliArgs::getArg
490494
* @dataProvider providerTestGetArg
491495
* @param array $argv
492496
* @param array|null $config
@@ -546,6 +550,7 @@ public function providerTestGetArgs()
546550
}
547551

548552
/**
553+
* @see \CliArgs\CliArgs::getArgs
549554
* @dataProvider providerTestGetArgs
550555
* @param array $argv
551556
* @param array|null $config
@@ -600,6 +605,7 @@ public function providerTestIsFlagExists()
600605
}
601606

602607
/**
608+
* @see \CliArgs\CliArgs::isFlagExists
603609
* @dataProvider providerTestIsFlagExists
604610
* @param array $argv
605611
* @param string $arg

tests/Unit/CliArgsTest.php

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
<?php
2+
/**
3+
* This file is part of CliArgs.
4+
* git: https://github.com/cheprasov/php-cli-args
5+
*
6+
* (C) Alexander Cheprasov <cheprasov.84@ya.ru>
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 Test\Unit;
12+
13+
use CliArgs\CliArgs;
14+
15+
class CliArgsTest extends \PHPUnit_Framework_TestCase
16+
{
17+
public function providerTestConfig()
18+
{
19+
return [
20+
[[], null],
21+
[
22+
['foo', 'bar'],
23+
[
24+
'foo' => ['key' => 'foo', 'alias' => null, 'default' => null, 'help' => null, 'filter' => null],
25+
'bar' => ['key' => 'bar', 'alias' => null, 'default' => null, 'help' => null, 'filter' => null],
26+
]
27+
],
28+
[
29+
['foo' => 'f', 'bar' => 'b', 'help'],
30+
[
31+
'foo' => ['key' => 'foo', 'alias' => 'f', 'default' => null, 'help' => null, 'filter' => null],
32+
'bar' => ['key' => 'bar', 'alias' => 'b', 'default' => null, 'help' => null, 'filter' => null],
33+
'help' => ['key' => 'help', 'alias' => null, 'default' => null, 'help' => null, 'filter' => null],
34+
]
35+
],
36+
[
37+
[
38+
'help' => [],
39+
'foo' => [
40+
'alias' => 'f',
41+
'default' => 'bar',
42+
'help' => 'Some help text',
43+
'filter' => ['a', 'b', 'c']
44+
]
45+
],
46+
[
47+
'help' => ['key' => 'help', 'alias' => null, 'default' => null, 'help' => null, 'filter' => null],
48+
'foo' => ['key' => 'foo', 'alias' => 'f', 'default' => 'bar', 'help' => 'Some help text', 'filter' => ['a', 'b', 'c']],
49+
]
50+
],
51+
];
52+
}
53+
54+
/**
55+
* @see \CliArgs\CliArgs::setConfig
56+
* @dataProvider providerTestConfig
57+
* @param $config
58+
* @param $expect
59+
*/
60+
public function testConfig($config, $expect)
61+
{
62+
$CliArgs = new CliArgs();
63+
$Reflection = new \ReflectionClass($CliArgs);
64+
$Method = $Reflection->getMethod('setConfig');
65+
$Method->setAccessible(true);
66+
$Method->invoke($CliArgs, $config);
67+
$Property = $Reflection->getProperty('config');
68+
$Property->setAccessible(true);
69+
$result = $Property->getValue($CliArgs);
70+
$this->assertEquals($expect, $result);
71+
}
72+
73+
public function providerTestParseArray()
74+
{
75+
return [
76+
[
77+
[__FILE__, '-f', 'b', '--foo', 'bar'],
78+
null,
79+
[__FILE__, 'f' => 'b', 'foo' => 'bar']
80+
],
81+
[
82+
['a', 'b', 'c', '-foo', '--bar'],
83+
false,
84+
['a', 'b', 'c', 'f' => false, 'o' => false, 'bar' => false]
85+
],
86+
[
87+
['-f', '-b', 'foo', '--foo=123'],
88+
0,
89+
['f' => 0, 'b' => 'foo', 'foo' => '123']
90+
],
91+
[
92+
['-f=1', '-b'],
93+
0,
94+
['f' => 0, '=' => 0, 1 => 0, 'b' => 0]
95+
],
96+
[
97+
['a', 'b', 'c'],
98+
null,
99+
['a', 'b', 'c']
100+
],
101+
[
102+
['-abc'],
103+
10,
104+
['a' => 10, 'b' => 10, 'c' => 10]
105+
],
106+
];
107+
}
108+
109+
/**
110+
* @see \CliArgs\CliArgs::parseArray
111+
* @dataProvider providerTestParseArray
112+
* @param array $argv
113+
* @param mixed $default
114+
* @param mixed $expect
115+
*/
116+
public function testParseArray($argv, $default, $expect)
117+
{
118+
$CliArgs = new CliArgs();
119+
$Reflection = new \ReflectionClass($CliArgs);
120+
$Method = $Reflection->getMethod('parseArray');
121+
$Method->setAccessible(true);
122+
$result = $Method->invoke($CliArgs, $argv, $default);
123+
$this->assertEquals($expect, $result);
124+
}
125+
126+
public function providerTestFilterValue()
127+
{
128+
return [
129+
['int', '042', 0, 42],
130+
['int', '42a', 0, 42],
131+
['int', '-42', 0, -42],
132+
['int', null, 0, 0],
133+
134+
['float', '04.2', 0.0, 4.2],
135+
['float', '4.2e2', 0.0, 420.0],
136+
['float', '-4.2e2', 0.0, -420.0],
137+
['float', null, 0.0, 0.0],
138+
139+
['bool', 'true', false, true],
140+
['bool', 'True', false, true],
141+
['bool', 'TRUE', false, true],
142+
['bool', 'Yes', false, true],
143+
['bool', 'YES', false, true],
144+
['bool', '1', false, true],
145+
['bool', null, null, null],
146+
['bool', null, false, false],
147+
['bool', 'No', false, false],
148+
['bool', 'False', null, false],
149+
['bool', 'false', null, false],
150+
['bool', '0', null, false],
151+
152+
['flag', null, null, true],
153+
['flag', null, false, true],
154+
['flag', true, null, true],
155+
156+
['json', '"123"', null, '123'],
157+
['json', '[1, 2, "3"]', null, [1, 2, '3']],
158+
['json', '{"foo":"bar", "baz":42}', null, ['foo' => 'bar', 'baz' => 42]],
159+
['json', 'bad', null, null],
160+
['json', 'bad', false, false],
161+
162+
[['1', '2', '3'], 1, false, false],
163+
[['1', '2', '3'], '1', false, '1'],
164+
[['1', '2', '3'], '3', false, '3'],
165+
[['1', '2', '3'], '5', false, false],
166+
[['foo', 'bar', 'baz'], 'bar', false, 'bar'],
167+
[['foo', 'bar', 'baz'], 'bazz', 'foo', 'foo'],
168+
169+
[function($a){ return $a * 2;}, '21', null, 42],
170+
[function($a, $default){ return $a + $default;}, '32', 10, 42],
171+
[[static::class,'strtotitle'], 'alexander', null, 'Alexander'],
172+
[[self::class,'strtotitle'], 'alexander cheprasov', null, 'Alexander Cheprasov'],
173+
];
174+
}
175+
176+
/**
177+
* @param string $str
178+
* @return string
179+
*/
180+
public static function strtotitle($str)
181+
{
182+
return mb_convert_case($str, MB_CASE_TITLE, 'UTF-8');
183+
}
184+
185+
/**
186+
* @see \CliArgs\CliArgs::filterValue
187+
* @dataProvider providerTestFilterValue
188+
* @param string|array|callable $filter
189+
* @param string $value
190+
* @param mixed $default
191+
* @param mixed $expect
192+
*/
193+
public function testFilterValue($filter, $value, $default, $expect)
194+
{
195+
$CliArgs = new CliArgs();
196+
$Reflection = new \ReflectionClass($CliArgs);
197+
$Method = $Reflection->getMethod('filterValue');
198+
$Method->setAccessible(true);
199+
$result = $Method->invoke($CliArgs, $filter, $value, $default);
200+
$this->assertEquals($expect, $result);
201+
}
202+
}

0 commit comments

Comments
 (0)