Skip to content

Commit 9583713

Browse files
committed
fix: check the option value is error
1 parent beedbf7 commit 9583713

File tree

6 files changed

+49
-7
lines changed

6 files changed

+49
-7
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,28 @@ foreach (Clog::getLevelNames() as $level) {
4949

5050
![clog-example](example/images/clog-example.png)
5151

52+
## Simple console app
53+
54+
```php
55+
use Toolkit\Cli\CliApp;
56+
57+
// run:
58+
// php example/mycmd
59+
// php example/mycmd -i abc --lon def ag1 ag2 ag3
60+
$cmd = CliApp::new('cmd1', 'this is my cli application');
61+
$cmd->addOpt('info', 'i', 'Output some information');
62+
$cmd->addOpt('long-option-name', 'lon', 'this is a long option for command');
63+
$cmd->addArg('arg1', 'this is first argument');
64+
65+
$cmd->setHandler(function (CliApp $cmd) {
66+
var_dump($cmd->getOpts(), $cmd->getArgs(), $cmd->getRemainArgs());
67+
});
68+
69+
$cmd->run();
70+
```
71+
72+
![clog-example](example/images/cli-app.png)
73+
5274
## Terminal control
5375

5476
examples:

example/cli-app.png

-67.5 KB
Binary file not shown.

example/images/cli-app.png

22.9 KB
Loading

src/Helper/FlagHelper.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,26 @@ public static function buildOptHelpName(array $names): string
3838
/**
3939
* check input is valid option value
4040
*
41-
* @param mixed $val
41+
* @param string|bool $val
4242
*
4343
* @return bool
4444
*/
45-
public static function isOptionValue(mixed $val): bool
45+
public static function isOptionValue(string|bool $val): bool
4646
{
4747
if ($val === false) {
4848
return false;
4949
}
5050

51-
// if is: '', 0 || is not option name
51+
// if is '', 0 || is not option name
5252
if (!$val || $val[0] !== '-') {
5353
return true;
5454
}
5555

56+
// is option name.
57+
if (ltrim($val, '-')) {
58+
return false;
59+
}
60+
5661
// ensure is option value.
5762
if (!str_contains($val, '=')) {
5863
return true;
@@ -189,7 +194,7 @@ public static function parseArgv(array $params, array $config = []): array
189194

190195
// long-opt: (--<opt>)
191196
if (str_starts_with($option, '-')) {
192-
$option = substr($option, 1);
197+
$option = $pn;
193198
$isLong = true;
194199

195200
// long-opt: value specified inline (--<opt>=<value>)
@@ -207,7 +212,6 @@ public static function parseArgv(array $params, array $config = []): array
207212

208213
// next elem is value. fix: allow empty string ''
209214
if ($value === true && !isset($boolOpts[$option]) && self::isOptionValue($nxt)) {
210-
// list(,$val) = each($params);
211215
$value = $nxt;
212216
next($params);
213217

test/ColorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
namespace Toolkit\CliTest;
1111

1212
use PHPUnit\Framework\TestCase;
13-
use Toolkit\Cli\Color;
1413
use Toolkit\Cli\Cli;
14+
use Toolkit\Cli\Color;
1515

1616
/**
1717
* Class ColorTest
@@ -23,6 +23,7 @@ class ColorTest extends TestCase
2323
public function testRender(): void
2424
{
2525
if (!Cli::isSupportColor()) {
26+
$this->assertFalse(Color::isShouldRenderColor());
2627
return;
2728
}
2829

test/Helper/FlagHelperTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PHPUnit\Framework\TestCase;
1313
use Toolkit\Cli\Helper\FlagHelper;
14+
use function printf;
1415

1516
/**
1617
* class FlagHelperTest
@@ -24,6 +25,17 @@ public function testIsValidArgName1(): void
2425
$this->assertFalse(FlagHelper::isValidName('/path/to'));
2526
}
2627

28+
public function testIsOptionValue(): void
29+
{
30+
$this->assertTrue(FlagHelper::isOptionValue('arg0'));
31+
$this->assertTrue(FlagHelper::isOptionValue('arg0-'));
32+
$this->assertTrue(FlagHelper::isOptionValue('-'));
33+
$this->assertTrue(FlagHelper::isOptionValue('--'));
34+
35+
$this->assertFalse(FlagHelper::isOptionValue('-d'));
36+
$this->assertFalse(FlagHelper::isOptionValue('--opt'));
37+
}
38+
2739
public function testIsValidName(): void
2840
{
2941
$tests = [
@@ -48,9 +60,12 @@ public function testIsValidName(): void
4860
$this->assertSame($ok, FlagHelper::isValidName((string)$name));
4961
}
5062
}
63+
5164
public function testParseArgv(): void
5265
{
53-
$rawArgv = explode(' ', 'git:tag --only-tag -d ../view arg0');
66+
$str = 'git:tag --only-tag -d ../view arg0';
67+
printf("parse: %s\n", $str);
68+
$rawArgv = explode(' ', $str);
5469

5570
[$args, $sOpts, $lOpts] = FlagHelper::parseArgv($rawArgv);
5671

0 commit comments

Comments
 (0)