Skip to content

Commit 86826b9

Browse files
authored
Merge pull request #6 from cheprasov/v210
v210
2 parents 9852f74 + 038cb50 commit 86826b9

File tree

9 files changed

+113
-10
lines changed

9 files changed

+113
-10
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ php:
44
- '5.6'
55
- '7.0'
66
- '7.1'
7+
- '7.2'
78
- hhvm
89

910
matrix:

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) Alexander Cheprasov <cheprasov.84@ya.ru>
1+
Copyright (c) Alexander Cheprasov <acheprasov84@gmail.com>
22

33
Permission is hereby granted, free of charge, to any person obtaining
44
a copy of this software and associated documentation files (the

README.md

+14-1
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.0.0 for PHP >= 5.5
2+
# CliArgs v2.1.0 for PHP >= 5.5
33

44
## About
55
Class **CliArgs** helps to get options from the command line argument list easy.
@@ -277,13 +277,26 @@ $arg = $CliArgs->getArg('foo');
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
283+
```php
284+
echo $CliArgs->isFlagOrAliasExists('f'); // true
285+
echo $CliArgs->isFlagOrAliasExists('foo'); // true
286+
287+
$CliArgs->isFlagExists('foo', 'f') === $CliArgs->isFlagOrAliasExists('f');
288+
```
289+
280290
##### isFlagExists(string $arg, string|null $alias): bool
281291
Checks if the given key exists in the arguments console list.
292+
Check of key or alias is independent.
282293
Returns true if $arg or $alias are exists
283294
```php
284295
echo $CliArgs->isFlagExists('f'); // false
285296
echo $CliArgs->isFlagExists('foo'); // true
286297
echo $CliArgs->isFlagExists('foo', 'f'); // true
298+
299+
$CliArgs->isFlagExists('foo', 'f') === $CliArgs->isFlagOrAliasExists('f');
287300
```
288301

289302
##### getArguments(): array

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "cheprasov/php-cli-args",
3-
"version": "2.0.0",
3+
"version": "2.1.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",
77
"license": "MIT",
88
"authors": [
99
{
1010
"name": "Alexander Cheprasov",
11-
"email": "cheprasov.84@ya.ru"
11+
"email": "acheprasov84@gmail.com"
1212
}
1313
],
1414
"autoload": {

src/CliArgs/CliArgs.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* This file is part of CliArgs.
44
* git: https://github.com/cheprasov/php-cli-args
55
*
6-
* (C) Alexander Cheprasov <cheprasov.84@ya.ru>
6+
* (C) Alexander Cheprasov <acheprasov84@gmail.com>
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
@@ -12,7 +12,7 @@
1212

1313
class CliArgs
1414
{
15-
const VERSION = '2.0.0';
15+
const VERSION = '2.1.0';
1616

1717
const FILTER_BOOL = 'bool';
1818
const FILTER_FLAG = 'flag';
@@ -110,6 +110,25 @@ public function isFlagExists($arg, $alias = null)
110110
return array_key_exists($arg, $this->getArguments()) || $alias && array_key_exists($alias, $this->getArguments());
111111
}
112112

113+
/**
114+
* Checks if the given key (or alias) exists in the arguments console list.
115+
* @param string $arg
116+
* @return bool
117+
*/
118+
public function isFlagOrAliasExists($arg)
119+
{
120+
if (!isset($this->aliases[$arg])) {
121+
return false;
122+
}
123+
$key = $this->aliases[$arg]['key'];
124+
if (isset($this->aliases[$arg]['alias'])) {
125+
$alias = $this->aliases[$arg]['alias'];
126+
} else {
127+
$alias = null;
128+
}
129+
return $this->isFlagExists($key, $alias);
130+
}
131+
113132
/**
114133
* Get one param
115134
* @param string $arg

src/autoloader.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
* This file is part of CliArgs.
44
* git: https://github.com/cheprasov/php-cli-args
55
*
6-
* (C) Alexander Cheprasov <cheprasov.84@ya.ru>
6+
* (C) Alexander Cheprasov <acheprasov84@gmail.com>
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace CliArgs;
1213

1314
spl_autoload_register(function($class) {

tests/Build/VersionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* This file is part of CliArgs.
44
* git: https://github.com/cheprasov/php-cli-args
55
*
6-
* (C) Alexander Cheprasov <cheprasov.84@ya.ru>
6+
* (C) Alexander Cheprasov <acheprasov84@gmail.com>
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.

tests/Functional/CliArgsTest.php

+70-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* This file is part of CliArgs.
44
* git: https://github.com/cheprasov/php-cli-args
55
*
6-
* (C) Alexander Cheprasov <cheprasov.84@ya.ru>
6+
* (C) Alexander Cheprasov <acheprasov84@gmail.com>
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
@@ -618,4 +618,73 @@ public function testIsFlagExists($argv, $arg, $alias, $expect)
618618
$CliArgs = new CliArgs();
619619
$this->assertEquals($expect, $CliArgs->isFlagExists($arg, $alias));
620620
}
621+
622+
public function providerTestIsFlagOrAliasExists()
623+
{
624+
return [
625+
__LINE__ => [
626+
[__FILE__],
627+
'foo',
628+
false
629+
],
630+
__LINE__ => [
631+
[__FILE__, '--bar'],
632+
'foo',
633+
false
634+
],
635+
__LINE__ => [
636+
[__FILE__, 'foo'],
637+
'foo',
638+
false
639+
],
640+
__LINE__ => [
641+
[__FILE__, '--foo', 'bar'],
642+
'foo',
643+
true
644+
],
645+
__LINE__ => [
646+
[__FILE__, '--foo', 'bar'],
647+
'foo',
648+
true
649+
],
650+
__LINE__ => [
651+
[__FILE__, '-f'],
652+
'foo',
653+
true
654+
],
655+
__LINE__ => [
656+
[__FILE__, '-f', 'bar'],
657+
'f',
658+
true
659+
],
660+
__LINE__ => [
661+
[__FILE__, '-age', '12'],
662+
'a',
663+
true
664+
],
665+
__LINE__ => [
666+
[__FILE__, '-a'],
667+
'age',
668+
true
669+
],
670+
];
671+
}
672+
673+
/**
674+
* @see \CliArgs\CliArgs::isFlagOrAliasExists
675+
* @dataProvider providerTestIsFlagOrAliasExists
676+
* @param array $argv
677+
* @param string $arg
678+
* @param bool $expect
679+
*/
680+
public function testIsFlagOrAliasExists($argv, $arg, $expect)
681+
{
682+
$GLOBALS['argv'] = $argv;
683+
$CliArgs = new CliArgs([
684+
'f' => 'foo',
685+
'b' => 'bar',
686+
'a' => 'age',
687+
]);
688+
$this->assertEquals($expect, $CliArgs->isFlagOrAliasExists($arg));
689+
}
621690
}

tests/Unit/CliArgsTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* This file is part of CliArgs.
44
* git: https://github.com/cheprasov/php-cli-args
55
*
6-
* (C) Alexander Cheprasov <cheprasov.84@ya.ru>
6+
* (C) Alexander Cheprasov <acheprasov84@gmail.com>
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.

0 commit comments

Comments
 (0)