Skip to content

Commit c86195b

Browse files
committed
v2.0.0
1 parent 9045806 commit c86195b

File tree

5 files changed

+120
-109
lines changed

5 files changed

+120
-109
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2015 Alexander Cheprasov <[email protected]>
1+
Copyright (c) Alexander Cheprasov <[email protected]>
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-5
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 v1.0.0 for PHP >= 5.5
2+
# CliArgs v2.0.0 for PHP >= 5.5
33

44
## About
55
Class **CliArgs** helps to get options from the command line argument list easy.
@@ -79,7 +79,6 @@ $config = [
7979
// 'flag' - will return TRUE, if key is exists in command line argument list.
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
82-
// 'help' - use this filter if you want generate and return help about all config.
8382
]
8483
];
8584

@@ -119,7 +118,6 @@ $config4 = [
119118
$config = [
120119
'help' => [
121120
'alias' => 'h',
122-
'filter' => 'help',
123121
'help' => 'Show help about all options',
124122
],
125123
'data' => [
@@ -138,11 +136,15 @@ $CliArgs = new CliArgs($config);
138136
```
139137
Show help
140138
> some-script.php --help
141-
<?php if ($CliArgs->isFlagExists('help', 'h')) echo $CliArgs->getArg('help'); ?>
139+
<?php if ($CliArgs->isFlagExists('help', 'h')) echo $CliArgs->getHelp('help'); ?>
142140
143141
Show help only for param data
144142
> some-script.php --help data
145-
<?php if ($CliArgs->isFlagExists('help', 'h')) echo $CliArgs->getArg('help'); ?>
143+
<?php if ($CliArgs->isFlagExists('help', 'h')) echo $CliArgs->getHelp('help'); ?>
144+
145+
Show help for all params data
146+
> some-script.php --help data
147+
<?php if ($CliArgs->isFlagExists('help', 'h')) echo $CliArgs->getHelp(); ?>
146148
147149
All the same:
148150
> some-script.php --data='{"foo":"bar"}' --user-id=42
@@ -295,6 +297,13 @@ print_r($CliArgs->getArguments());
295297
// )
296298
```
297299

300+
##### geHelp([string $value = null]): string
301+
Get help
302+
```php
303+
echo $CliArgs->getHelp(); // Get help for all params
304+
echo $CliArgs->getHelp('help'); // Get help for secified params: --help data
305+
```
306+
298307
## Installation
299308

300309
### Composer

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cheprasov/php-cli-args",
3-
"version": "1.0.0",
3+
"version": "2.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

+13-11
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212

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

1717
const FILTER_BOOL = 'bool';
1818
const FILTER_FLAG = 'flag';
1919
const FILTER_FLOAT = 'float';
2020
const FILTER_INT = 'int';
2121
const FILTER_JSON = 'json';
22-
const FILTER_HELP = 'help';
2322

2423
/**
2524
* @var array|null
@@ -89,6 +88,7 @@ protected function setConfig(array $config = null)
8988
}
9089

9190
/**
91+
* Get prepared ARGV
9292
* @return array|null
9393
*/
9494
public function getArguments()
@@ -100,6 +100,7 @@ public function getArguments()
100100
}
101101

102102
/**
103+
* Checks if the given key exists in the arguments console list. Returns true if $arg or $alias are exists
103104
* @param string $arg
104105
* @param string|null $alias
105106
* @return bool
@@ -110,6 +111,7 @@ public function isFlagExists($arg, $alias = null)
110111
}
111112

112113
/**
114+
* Get one param
113115
* @param string $arg
114116
* @return mixed
115117
*/
@@ -133,10 +135,7 @@ public function getArg($arg)
133135
return null;
134136
}
135137

136-
if ($cfg['filter'] && $cfg['default'] !== $value
137-
|| $cfg['filter'] === self::FILTER_FLAG
138-
|| $cfg['filter'] === self::FILTER_HELP
139-
) {
138+
if ($cfg['filter'] && $cfg['default'] !== $value || $cfg['filter'] === self::FILTER_FLAG) {
140139
$value = $this->filterValue($cfg['filter'], $value, $cfg['default'] ?: null);
141140
}
142141

@@ -146,6 +145,7 @@ public function getArg($arg)
146145
}
147146

148147
/**
148+
* Get all params.
149149
* @return mixed[]
150150
*/
151151
public function getArgs()
@@ -186,9 +186,6 @@ protected function filterValue($filter, $value, $default = null)
186186

187187
case self::FILTER_JSON:
188188
return json_decode($value, true);
189-
190-
case self::FILTER_HELP:
191-
return $this->getHelp($value);
192189
}
193190
return $default;
194191
}
@@ -214,11 +211,16 @@ protected function getArgFromConfig($arg)
214211
}
215212

216213
/**
217-
* @param mixed $value
214+
* Get help about
215+
* @param string $value
218216
* @return string mixed
219217
*/
220-
protected function getHelp($value = null)
218+
public function getHelp($value = null)
221219
{
220+
if ($value) {
221+
$value = $this->getArg($value);
222+
}
223+
222224
$breakTitle = PHP_EOL . str_repeat(' ', 4);
223225
$breakInfo = PHP_EOL . str_repeat(' ', 8);
224226
$help = [];

0 commit comments

Comments
 (0)