Skip to content

Commit 9d8070d

Browse files
committed
rename showType to helpType, update readme
1 parent da2ea23 commit 9d8070d

File tree

10 files changed

+120
-50
lines changed

10 files changed

+120
-50
lines changed

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ public const DEFINE_ITEM = [
363363
'name' => '',
364364
'desc' => '',
365365
'type' => FlagType::STRING,
366-
'showType' => '', // use for show help
366+
'helpType' => '', // use for render help
367367
// 'index' => 0, // only for argument
368368
'required' => false,
369369
'default' => null,
@@ -374,6 +374,42 @@ public const DEFINE_ITEM = [
374374
];
375375
```
376376

377+
## Costom Settings
378+
379+
```php
380+
381+
/**
382+
* Stop parse option on found first argument.
383+
*
384+
* - Useful for support multi commands. eg: `top --opt ... sub --opt ...`
385+
*
386+
* @var bool
387+
*/
388+
protected $stopOnFistArg = true;
389+
390+
/**
391+
* Skip on found undefined option.
392+
*
393+
* - FALSE will throw FlagException error.
394+
* - TRUE will skip it and collect as raw arg, then continue parse next.
395+
*
396+
* @var bool
397+
*/
398+
protected $skipOnUndefined = false;
399+
400+
// -------------------- settings for parse argument --------------------
401+
402+
/**
403+
* @var bool
404+
*/
405+
protected $autoBindArgs = true;
406+
407+
/**
408+
* @var bool
409+
*/
410+
protected $strictCheckArgs = false;
411+
```
412+
377413
## Unit tests
378414

379415
```bash

src/Concern/HelperRenderTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ protected function buildOptsForHelp(array $optDefines, bool $hasShortOpt): array
300300

301301
// show type name.
302302
if ($this->showTypeOnHelp) {
303-
$typeName = $opt['showType'] ?: FlagType::getHelpName($opt['type']);
303+
$typeName = $opt['helpType'] ?: FlagType::getHelpName($opt['type']);
304304
$helpName .= $typeName ? " $typeName" : '';
305305
}
306306

src/Contract/ParserInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function hasShortOpts(): bool;
5555
* @param mixed $default
5656
* @param array $moreInfo
5757
*
58-
* @psalm-param array{alias: string, showType: string} $moreInfo
58+
* @psalm-param array{alias: string, helpType: string} $moreInfo
5959
*
6060
* @return self
6161
*/
@@ -79,7 +79,7 @@ public function addOpt(
7979
* @param mixed $default
8080
* @param array $moreInfo
8181
*
82-
* @psalm-param array{showType: string} $moreInfo
82+
* @psalm-param array{helpType: string} $moreInfo
8383
*
8484
* @return self
8585
*/

src/Flag/AbstractFlag.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ abstract class AbstractFlag implements ArrayAccess, FlagInterface
5757
/**
5858
* @var string
5959
*/
60-
protected $showType = '';
60+
protected $helpType = '';
6161

6262
/**
6363
* ENV var name. support read value from ENV var
@@ -347,7 +347,7 @@ public function toArray(): array
347347
'envVar' => $this->envVar,
348348
'required' => $this->required,
349349
'isArray' => $this->isArray(),
350-
'showType' => $this->getShowType(),
350+
'helpType' => $this->getHelpType(),
351351
];
352352
}
353353

@@ -388,21 +388,21 @@ public function setRequired(bool $required): void
388388
*
389389
* @return string
390390
*/
391-
public function getShowType(bool $useTypeOnEmpty = false): string
391+
public function getHelpType(bool $useTypeOnEmpty = false): string
392392
{
393393
if ($useTypeOnEmpty) {
394-
return $this->showType ?: $this->type;
394+
return $this->helpType ?: $this->type;
395395
}
396396

397-
return $this->showType;
397+
return $this->helpType;
398398
}
399399

400400
/**
401-
* @param string $showType
401+
* @param string $helpType
402402
*/
403-
public function setShowType(string $showType): void
403+
public function setHelpType(string $helpType): void
404404
{
405-
$this->showType = $showType;
405+
$this->helpType = $helpType;
406406
}
407407

408408
/**

src/Flags.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ protected function resetArguments(): void
653653
* @param mixed $default
654654
* @param array $moreInfo
655655
*
656-
* @psalm-param array{alias: string, showType: string} $moreInfo
656+
* @psalm-param array{alias: string, helpType: string} $moreInfo
657657
*
658658
* @return ParserInterface|self
659659
*/

src/FlagsParser.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ abstract class FlagsParser implements ParserInterface
5454
'name' => '',
5555
'desc' => '',
5656
'type' => FlagType::STRING,
57-
'showType' => '', // use for show help
57+
'helpType' => '', // use for render help
5858
// 'index' => 0, // only for argument
5959
'required' => false,
6060
'envVar' => '', // support read value from ENV var
@@ -183,28 +183,28 @@ abstract class FlagsParser implements ParserInterface
183183
// -------------------- settings for parse argument --------------------
184184

185185
/**
186-
* Has array argument
187-
*
188186
* @var bool
189187
*/
190-
protected $arrayArg = false;
188+
protected $autoBindArgs = true;
191189

192190
/**
193-
* Has optional argument
194-
*
195191
* @var bool
196192
*/
197-
protected $optionalArg = false;
193+
protected $strictCheckArgs = false;
198194

199195
/**
196+
* Has array argument
197+
*
200198
* @var bool
201199
*/
202-
protected $autoBindArgs = true;
200+
protected $arrayArg = false;
203201

204202
/**
203+
* Has optional argument
204+
*
205205
* @var bool
206206
*/
207-
protected $strictCheckArgs = false;
207+
protected $optionalArg = false;
208208

209209
/**
210210
* Class constructor.

src/SFlags.php

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use function current;
2020
use function explode;
2121
use function implode;
22-
use function is_int;
2322
use function is_string;
2423
use function next;
2524
use function sprintf;
@@ -143,15 +142,15 @@ public function isNotEmpty(): bool
143142
***************************************************************/
144143

145144
/**
146-
* @param string $name
147-
* @param string $shortcut
148-
* @param string $desc
149-
* @param string $type The argument data type. default is: string. {@see FlagType}
150-
* @param bool $required
151-
* @param mixed $default
152-
* @param array $moreInfo
145+
* @param string $name
146+
* @param string $shortcut
147+
* @param string $desc
148+
* @param string $type The argument data type. default is: string. {@see FlagType}
149+
* @param bool $required
150+
* @param mixed $default
151+
* @param array $moreInfo
153152
*
154-
* @psalm-param array{alias: string, showType: string} $moreInfo
153+
* @psalm-param array{alias: string, helpType: string} $moreInfo
155154
*
156155
* @return SFlags
157156
*/
@@ -174,23 +173,23 @@ public function addOpt(
174173
$define['default'] = $default;
175174
$define['shorts'] = $shortcut ? Str::explode($shortcut, ',') : [];
176175

177-
if (isset($moreInfo['showType'])) {
178-
$define['showType'] = $moreInfo['showType'];
176+
if (isset($moreInfo['helpType'])) {
177+
$define['helpType'] = $moreInfo['helpType'];
179178
}
180179

181180
$this->addOptDefine($define);
182181
return $this;
183182
}
184183

185184
/**
186-
* @param string $name
187-
* @param string $desc
188-
* @param string $type The argument data type. default is: string. {@see FlagType}
189-
* @param bool $required
190-
* @param mixed $default
191-
* @param array $moreInfo
185+
* @param string $name
186+
* @param string $desc
187+
* @param string $type The argument data type. default is: string. {@see FlagType}
188+
* @param bool $required
189+
* @param mixed $default
190+
* @param array $moreInfo
192191
*
193-
* @psalm-param array{alias: string, showType: string} $moreInfo
192+
* @psalm-param array{alias: string, helpType: string} $moreInfo
194193
*
195194
* @return SFlags
196195
*/
@@ -212,8 +211,8 @@ public function addArg(
212211
$define['required'] = $required;
213212
$define['default'] = $default;
214213

215-
if (isset($moreInfo['showType'])) {
216-
$define['showType'] = $moreInfo['showType'];
214+
if (isset($moreInfo['helpType'])) {
215+
$define['helpType'] = $moreInfo['helpType'];
217216
}
218217

219218
$this->addArgDefine($define);
@@ -485,7 +484,7 @@ private function parseSpecialShorts(string $shorts): void
485484

486485
/**
487486
* @param string $option
488-
* @param mixed $value
487+
* @param mixed $value
489488
*/
490489
protected function setOptValue(string $option, $value): void
491490
{
@@ -500,8 +499,8 @@ protected function setOptValue(string $option, $value): void
500499

501500
/**
502501
* @param string $name The option name
503-
* @param mixed $value
504-
* @param array $define {@see DEFINE_ITEM}
502+
* @param mixed $value
503+
* @param array $define {@see DEFINE_ITEM}
505504
*/
506505
protected function setRealOptValue(string $name, $value, array $define): void
507506
{
@@ -574,8 +573,8 @@ public function bindingArguments(): void
574573

575574
/**
576575
* @param mixed $value
577-
* @param int $index
578-
* @param bool $isArray
576+
* @param int $index
577+
* @param bool $isArray
579578
* @param array $define
580579
*/
581580
protected function collectArgValue($value, int $index, bool $isArray, array $define): void
@@ -753,7 +752,7 @@ public function hasOption(string $name): bool
753752
}
754753

755754
/**
756-
* @param string $name
755+
* @param string $name
757756
* @param null|mixed $default
758757
*
759758
* @return mixed
@@ -764,7 +763,7 @@ public function getOption(string $name, $default = null)
764763
}
765764

766765
/**
767-
* @param string $name
766+
* @param string $name
768767
* @param null|mixed $default
769768
*
770769
* @return mixed

src/Validator/NameValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class NameValidator extends RegexValidator
2121
*/
2222
public static function new(string $regex = self::DEFAULT_REGEX): parent
2323
{
24-
return new static($regex);
24+
return new self($regex);
2525
}
2626

2727
/**

test/FlagsParserTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,34 @@
1111
*/
1212
class FlagsParserTest extends BaseFlagsTestCase
1313
{
14+
public function testBasic(): void
15+
{
16+
$this->runTestsWithParsers(function (FlagsParser $fs) {
17+
$this->doCheckBasic($fs);
18+
});
19+
}
20+
21+
private function doCheckBasic(FlagsParser $fs): void
22+
{
23+
$this->assertTrue($fs->isEmpty());
24+
$this->assertFalse($fs->isNotEmpty());
25+
$this->assertFalse($fs->hasShortOpts());
26+
27+
$fs->setArgRules([
28+
'github' => 'an string argument'
29+
]);
30+
$this->assertFalse($fs->isEmpty());
31+
$this->assertTrue($fs->isNotEmpty());
32+
$this->assertFalse($fs->hasShortOpts());
33+
34+
$fs->setOptRules([
35+
'-n,--name' => 'an string option'
36+
]);
37+
$this->assertFalse($fs->isEmpty());
38+
$this->assertTrue($fs->isNotEmpty());
39+
$this->assertTrue($fs->hasShortOpts());
40+
}
41+
1442
public function testStopOnFirstArg(): void
1543
{
1644
$this->runTestsWithParsers(function (FlagsParser $fs) {

test/ValidatorTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ public function testNameValidator(): void
2525
{
2626
$v = NameValidator::new();
2727
$this->assertTrue($v('inhere', 'test'));
28+
$this->assertEmpty((string)$v);
29+
30+
$v->setRegex('');
31+
$this->assertTrue($v('inhere', 'test'));
32+
33+
$v = new NameValidator;
34+
$this->assertTrue($v('inhere', 'test'));
2835

2936
$this->expectException(FlagException::class);
3037
$this->expectExceptionMessage("flag 'test' value should match: " . NameValidator::DEFAULT_REGEX);

0 commit comments

Comments
 (0)