Skip to content

Commit bd85d93

Browse files
committed
Refactored & Composer & tests
1 parent 9616698 commit bd85d93

File tree

11 files changed

+634
-69
lines changed

11 files changed

+634
-69
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
.idea/
2+
vendor/
3+
composer.lock
4+

.travis.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
sudo: required
2+
3+
language: php
4+
php:
5+
- '5.5'
6+
- '5.6'
7+
- '7.0'
8+
- '7.1'
9+
- hhvm
10+
11+
matrix:
12+
allow_failures:
13+
- php: hhvm
14+
15+
install:
16+
- composer install

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2015 Alexander Cheprasov <cheprasov.84@ya.ru>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
2+
# CliArgs v1.0.0 for PHP >= 5.5
3+
4+
## About
5+
Easy way to gets options from the command line argument list.
6+
7+
## Usage
8+
9+
### Config
10+
11+
```php
12+
13+
```
14+
15+
### Create a new instance
16+
```php
17+
18+
```
19+
20+
## Installation
21+
22+
### Composer
23+
24+
Download composer:
25+
26+
wget -nc http://getcomposer.org/composer.phar
27+
28+
and add dependency to your project:
29+
30+
php composer.phar require cheprasov/php-cli-args
31+
32+
## Running tests
33+
34+
./vendor/bin/phpunit
35+
36+
37+
## Something doesn't work
38+
39+
Feel free to fork project, fix bugs and finally request for pull

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "cheprasov/php-cli-args",
3+
"version": "1.0.0",
4+
"description": "Easy way to gets options from the command line argument list",
5+
"homepage": "http://github.com/cheprasov/php-cli-args",
6+
"minimum-stability": "stable",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Alexander Cheprasov",
11+
"email": "cheprasov.84@ya.ru"
12+
}
13+
],
14+
"autoload": {
15+
"psr-0": {
16+
"CliArgs\\": "src"
17+
}
18+
},
19+
"require": {
20+
"php": ">=5.5"
21+
},
22+
"require-dev": {
23+
"phpunit/phpunit": "4.8.*"
24+
}
25+
}

phpunit.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
backupGlobals = "false"
4+
backupStaticAttributes = "false"
5+
colors = "true"
6+
convertErrorsToExceptions = "true"
7+
convertNoticesToExceptions = "true"
8+
convertWarningsToExceptions = "true"
9+
processIsolation = "false"
10+
stopOnFailure = "true"
11+
stopOnError = "true"
12+
stopOnIncomplete = "false"
13+
stopOnSkipped = "false"
14+
bootstrap = "vendor/autoload.php"
15+
syntaxCheck = "true"
16+
verbose = "true"
17+
beStrictAboutTestsThatDoNotTestAnything = "true"
18+
checkForUnintentionallyCoveredCode = "false"
19+
beStrictAboutOutputDuringTests = "false"
20+
beStrictAboutTestSize = "true"
21+
>
22+
<testsuites>
23+
<testsuite name="All tests">
24+
<directory suffix="Test.php" >./tests/</directory>
25+
</testsuite>
26+
</testsuites>
27+
28+
</phpunit>

src/CliArgs/CliArgs.php

Lines changed: 91 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* This file is part of RedisClient.
3+
* This file is part of CliArgs.
44
* git: https://github.com/cheprasov/php-cli-args
55
*
66
* (C) Alexander Cheprasov <cheprasov.84@ya.ru>
@@ -12,9 +12,12 @@
1212

1313
class CliArgs
1414
{
15-
const FILTER_INT = 'int';
16-
const FILTER_FLOAT = 'float';
15+
const VERSION = '1.0.0';
16+
1717
const FILTER_BOOL = 'bool';
18+
const FILTER_FLAG = 'flag';
19+
const FILTER_FLOAT = 'float';
20+
const FILTER_INT = 'int';
1821
const FILTER_JSON = 'json';
1922
const FILTER_HELP = 'help';
2023

@@ -49,7 +52,7 @@ public function __construct(array $config = null)
4952
/**
5053
* @param array|null $config
5154
*/
52-
public function setConfig(array $config = null)
55+
protected function setConfig(array $config = null)
5356
{
5457
$this->config = $config;
5558
$this->cache = [];
@@ -60,11 +63,24 @@ public function setConfig(array $config = null)
6063
$this->aliases = [];
6164
foreach ($config as $key => $cfg) {
6265
$this->aliases[$key] = &$config[$key];
63-
if (isset($cfg['short'])) {
64-
$this->aliases[$cfg['short']] = &$config[$key];
66+
$config[$key]['key'] = $key;
67+
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;
78+
}
79+
if (!isset($cfg['filter'])) {
80+
$config[$key]['filter'] = null;
6581
}
66-
$config[$key]['long'] = $key;
6782
}
83+
$this->config = $config;
6884
}
6985

7086
/**
@@ -78,6 +94,16 @@ public function getArguments()
7894
return $this->arguments;
7995
}
8096

97+
/**
98+
* @param string $arg
99+
* @param string|null $alias
100+
* @return bool
101+
*/
102+
public function isFlagExists($arg, $alias = null)
103+
{
104+
return array_key_exists($arg, $this->getArguments()) || $alias && array_key_exists($alias, $this->getArguments());
105+
}
106+
81107
/**
82108
* @param string $arg
83109
* @return mixed
@@ -88,34 +114,49 @@ public function getArg($arg)
88114
return null;
89115
}
90116
if (array_key_exists($arg, $this->cache)) {
91-
return $this->cache[$arg];
117+
return $this->cache[$cfg['key']];
92118
}
93119
$arguments = $this->getArguments();
94120

95-
if (isset($cfg['long']) && isset($arguments[$cfg['long']])) {
96-
$value = $arguments[$cfg['long']];
97-
} elseif (isset($cfg['short']) && isset($arguments[$cfg['short']])) {
98-
$value = $arguments[$cfg['short']];
99-
} elseif (isset($cfg['default'])) {
121+
if ($this->isFlagExists($cfg['key'])) {
122+
$value = $arguments[$cfg['key']];
123+
} elseif ($this->isFlagExists($cfg['alias'])) {
124+
$value = $arguments[$cfg['alias']];
125+
} elseif ($cfg['default']) {
100126
return $cfg['default'];
101127
} else {
102128
return null;
103129
}
104130

105-
if (isset($cfg['filter'])) {
106-
$value = $this->filterValue($cfg['filter'], $value, isset($cfg['default']) ? $cfg['default'] : null);
131+
if ($cfg['filter'] && $cfg['default'] !== $value
132+
|| $cfg['filter'] === self::FILTER_FLAG
133+
|| $cfg['filter'] === self::FILTER_HELP
134+
) {
135+
$value = $this->filterValue($cfg['filter'], $value, $cfg['default'] ?: null);
107136
}
108137

109-
if (isset($cfg['long'])) {
110-
$this->cache[$cfg['long']] = $value;
111-
}
112-
if (isset($cfg['short'])) {
113-
$this->cache[$cfg['short']] = $value;
114-
}
138+
$this->cache[$cfg['key']] = $value;
115139

116140
return $value;
117141
}
118142

143+
/**
144+
* @return mixed[]
145+
*/
146+
public function getArgs()
147+
{
148+
$args = [];
149+
$arguments = $this->getArguments();
150+
foreach ($arguments as $key => $arg) {
151+
if (!isset($this->aliases[$key])) {
152+
continue;
153+
}
154+
$args[$key] = $this->getArg($key);
155+
}
156+
157+
return $args;
158+
}
159+
119160
/**
120161
* @param mixed $filter
121162
* @param mixed $value
@@ -126,26 +167,23 @@ protected function filterValue($filter, $value, $default = null)
126167
{
127168
if (is_string($filter)) {
128169
switch ($filter) {
129-
case self::FILTER_BOOL:
170+
case self::FILTER_FLAG:
171+
return true;
172+
173+
case self::FILTER_BOOL:
130174
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
131175

132-
case self::FILTER_INT:
176+
case self::FILTER_INT:
133177
return (int)$value;
134178

135-
case self::FILTER_FLOAT:
179+
case self::FILTER_FLOAT:
136180
return (float)$value;
137181

138-
case self::FILTER_JSON:
182+
case self::FILTER_JSON:
139183
return json_decode($value, true);
140184

141185
case self::FILTER_HELP:
142186
return $this->getHelp($value);
143-
144-
default:
145-
if (preg_match($filter, $value)
146-
&& preg_last_error() == PREG_NO_ERROR) {
147-
return $value;
148-
}
149187
}
150188
return $default;
151189
}
@@ -174,16 +212,30 @@ protected function getArgFromConfig($arg)
174212
* @param mixed $value
175213
* @return string mixed
176214
*/
177-
protected function getHelp($value)
215+
protected function getHelp($value = null)
178216
{
179-
$lines = [];
180-
foreach ($this->config as $key => $cfg) {
181-
$lines[] = [
182-
'--' . $key . (isset($cfg['short']) ? ' or -' . $cfg['short'] : ''),
183-
isset($cfg['help']) ? $cfg['help'] : '',
184-
];
217+
$breakTitle = PHP_EOL . str_repeat(' ', 4);
218+
$breakInfo = PHP_EOL . str_repeat(' ', 8);
219+
$help = [];
220+
foreach ($this->config as $cfg) {
221+
if ($value && ($cfg['key'] !== $value && (!$cfg['alias'] || $cfg['alias'] !== $value))) {
222+
continue;
223+
}
224+
$title = [];
225+
if ($cfg['key']) {
226+
$title[] = (1 === strlen($cfg['key']) ? '-' : '--') . $cfg['key'];
227+
}
228+
if ($cfg['alias']) {
229+
$title[] = (1 === strlen($cfg['alias']) ? '-' : '--') . $cfg['alias'];
230+
}
231+
$line = implode(' ', $title);
232+
if ($cfg['help']) {
233+
$line .= $breakInfo . wordwrap($cfg['help'], 75, $breakInfo);
234+
}
235+
$help[] = $breakTitle . $line;
185236
}
186-
return $value;
237+
238+
return 'HELP:' . PHP_EOL . (implode(PHP_EOL, $help) ?: 'Key is not found') . PHP_EOL;
187239
}
188240

189241
/**

src/autoloader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* This file is part of RedisClient.
3+
* This file is part of CliArgs.
44
* git: https://github.com/cheprasov/php-cli-args
55
*
66
* (C) Alexander Cheprasov <cheprasov.84@ya.ru>

0 commit comments

Comments
 (0)