Skip to content

Commit 6bf883c

Browse files
committed
update: add an util methods, update readme
1 parent 00be077 commit 6bf883c

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ $app->add('test2', function (FlagsParser $fs) {
317317
]
318318
]);
319319

320+
// fn - required php 7.4+
320321
$app->add('show-err', fn() => throw new RuntimeException('test show exception'));
321322

322323
$app->run();

README.zh-CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,6 @@ $arrArg = $fs->getArg(1); // array{"arr0", "arr1"}
351351
// get value by name
352352
$arrArg = $fs->getArg('arrArg'); // array{"arr0", "arr1"}
353353
```
354-
355354
-----------
356355

357356
## 创建简单的独立命令或应用程序
@@ -445,6 +444,7 @@ $app->add('test2', function (FlagsParser $fs) {
445444
]
446445
]);
447446

447+
// fn - required php 7.4+
448448
$app->add('show-err', fn() => throw new RuntimeException('test show exception'));
449449

450450
$app->run();

src/FlagUtil.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Toolkit\PFlag;
44

5+
use function array_keys;
56
use function array_map;
67
use function array_shift;
78
use function basename;
@@ -11,7 +12,9 @@
1112
use function is_numeric;
1213
use function ltrim;
1314
use function preg_match;
15+
use function str_replace;
1416
use function strlen;
17+
use function trim;
1518

1619
/**
1720
* class FlagUtil
@@ -157,4 +160,42 @@ public static function escapeToken(string $token): string
157160
{
158161
return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
159162
}
163+
164+
/**
165+
* Align command option names.
166+
*
167+
* @param array $options
168+
*
169+
* @return array
170+
*/
171+
public static function alignOptions(array $options): array
172+
{
173+
if (!$options) {
174+
return [];
175+
}
176+
177+
// check has short option. e.g '-h, --help'
178+
$nameString = implode('|', array_keys($options));
179+
if (preg_match('/\|-\w/', $nameString) !== 1) {
180+
return $options;
181+
}
182+
183+
$formatted = [];
184+
foreach ($options as $name => $des) {
185+
if (!$name = trim($name, ', ')) {
186+
continue;
187+
}
188+
189+
// start with '--', padding length equals to '-h, '
190+
if (isset($name[1]) && $name[1] === '-') {
191+
$name = ' ' . $name;
192+
} else {
193+
$name = str_replace([',-'], [', -'], $name);
194+
}
195+
196+
$formatted[$name] = $des;
197+
}
198+
199+
return $formatted;
200+
}
160201
}

test/CliAppTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Toolkit\PFlag\CliApp;
66
use Toolkit\Stdlib\Obj\DataObject;
7+
use const PHP_VERSION_ID;
78

89
/**
910
* class CliAppTest
@@ -15,11 +16,21 @@ class CliAppTest extends BaseFlagsTestCase
1516
private function initApp(CliApp $app): DataObject
1617
{
1718
$buf = DataObject::new();
18-
$app->add('test1', fn() => $buf->set('key', 'in test1'));
19+
20+
if (PHP_VERSION_ID > 70400) {
21+
$app->add('test1', fn() => $buf->set('key', 'in test1'));
22+
} else {
23+
$app->add('test1', function() use($buf) {
24+
$buf->set('key', 'in test2');
25+
});
26+
}
27+
1928
$app->addCommands([
2029
'test2' => [
2130
'desc' => 'desc for test2 command',
22-
'handler' => fn() => $buf->set('key', 'in test2'),
31+
'handler' => function() use($buf) {
32+
$buf->set('key', 'in test2');
33+
},
2334
'options' => [
2435
'opt1' => 'string;a string opt1 for command test2',
2536
'opt2' => 'int;a int opt2 for command test2',

0 commit comments

Comments
 (0)