Skip to content

Commit 39bce62

Browse files
committed
update: add more tests for parse flags
1 parent 005c0e0 commit 39bce62

File tree

11 files changed

+275
-90
lines changed

11 files changed

+275
-90
lines changed

README.md

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ $flags = ['--name', 'inhere', '--age', '99', '--tag', 'php', '-t', 'go', '--tag'
203203

204204
$optRules = [
205205
'name', // string
206-
'age' => 'int;;required', // set required
206+
'age' => 'int;an int option;required', // set required
207207
'tag,t' => FlagType::ARRAY,
208208
'f' => FlagType::BOOL,
209209
];
@@ -251,7 +251,7 @@ $scriptFile = array_shift($rawFlags);
251251
$optRules = [
252252
// some option rules
253253
'name', // string
254-
'age' => 'int;;required', // set required
254+
'age' => 'int;an int option;required', // set required
255255
'tag,t' => FlagType::ARRAY,
256256
'f' => FlagType::BOOL,
257257
];
@@ -374,7 +374,9 @@ public const DEFINE_ITEM = [
374374
];
375375
```
376376

377-
## Costom Settings
377+
## Costom settings
378+
379+
### Settings for parse
378380

379381
```php
380382
// -------------------- settings for parse option --------------------
@@ -401,14 +403,67 @@ public const DEFINE_ITEM = [
401403
// -------------------- settings for parse argument --------------------
402404

403405
/**
406+
* Whether auto bind remaining args after option parsed
407+
*
404408
* @var bool
405409
*/
406410
protected $autoBindArgs = true;
407411

408412
/**
413+
* Strict match args number.
414+
* if exist unbind args, will throw FlagException
415+
*
409416
* @var bool
410417
*/
411-
protected $strictCheckArgs = false;
418+
protected $strictMatchArgs = false;
419+
420+
```
421+
422+
### Setting for render help
423+
424+
support some settings for render help
425+
426+
```php
427+
428+
// -------------------- settings for built-in render help --------------------
429+
430+
/**
431+
* Auto render help on provide '-h', '--help'
432+
*
433+
* @var bool
434+
*/
435+
protected $autoRenderHelp = true;
436+
437+
/**
438+
* Show flag data type on render help.
439+
*
440+
* if False:
441+
*
442+
* -o, --opt Option desc
443+
*
444+
* if True:
445+
*
446+
* -o, --opt STRING Option desc
447+
*
448+
* @var bool
449+
*/
450+
protected $showTypeOnHelp = true;
451+
452+
/**
453+
* Will call it on before print help message
454+
*
455+
* @var callable
456+
*/
457+
private $beforePrintHelp;
458+
459+
```
460+
461+
- custom help message renderer
462+
463+
```php
464+
$fs->setHelpRenderer(function (\Toolkit\PFlag\FlagsParser $fs) {
465+
// render help messages
466+
});
412467
```
413468

414469
## Unit tests

src/Concern/HelperRenderTrait.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,13 @@
2929
*/
3030
trait HelperRenderTrait
3131
{
32-
3332
/**
3433
* Custom help renderer.
3534
*
3635
* @var callable
3736
*/
3837
protected $helpRenderer;
3938

40-
/**
41-
* Auto render help on provide '-h', '--help'
42-
*
43-
* @var bool
44-
*/
45-
protected $autoRenderHelp = true;
46-
47-
// -------------------- settings for built-in render help --------------------
48-
4939
/**
5040
* @var string|array|null
5141
*/
@@ -56,14 +46,33 @@ trait HelperRenderTrait
5646
*/
5747
protected $exampleHelp = '';
5848

49+
// -------------------- settings for built-in render help --------------------
50+
51+
/**
52+
* Auto render help on provide '-h', '--help'
53+
*
54+
* @var bool
55+
*/
56+
protected $autoRenderHelp = true;
57+
5958
/**
60-
* Show flag data type on render help
59+
* Show flag data type on render help.
60+
*
61+
* if False:
62+
*
63+
* -o, --opt Option desc
64+
*
65+
* if True:
66+
*
67+
* -o, --opt STRING Option desc
6168
*
6269
* @var bool
6370
*/
6471
protected $showTypeOnHelp = true;
6572

6673
/**
74+
* Will call it on before print help message
75+
*
6776
* @var callable
6877
*/
6978
private $beforePrintHelp;
@@ -383,7 +392,9 @@ public function getMoreHelp()
383392
*/
384393
public function setMoreHelp($moreHelp): void
385394
{
386-
$this->moreHelp = $moreHelp;
395+
if ($moreHelp) {
396+
$this->moreHelp = $moreHelp;
397+
}
387398
}
388399

389400
/**
@@ -407,7 +418,9 @@ public function setExample($example): void
407418
*/
408419
public function setExampleHelp($exampleHelp): void
409420
{
410-
$this->exampleHelp = $exampleHelp;
421+
if ($exampleHelp) {
422+
$this->exampleHelp = $exampleHelp;
423+
}
411424
}
412425

413426
/**

src/Concern/RuleParserTrait.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -255,18 +255,6 @@ protected function parseRule($rule, string $name = '', int $index = 0, bool $isO
255255
$item['index'] = $index;
256256
}
257257

258-
$nameMark = $name ? "(name: $name)" : "(#$index)";
259-
260-
// check type
261-
if (!FlagType::isValid($type = $item['type'])) {
262-
throw new FlagException("cannot define invalid flag type: $type$nameMark");
263-
}
264-
265-
// validator must be callable
266-
if (!empty($item['validator']) && !is_callable($item['validator'])) {
267-
throw new InvalidArgumentException("validator must be callable. flag: $nameMark");
268-
}
269-
270258
$item['name'] = $name;
271259
return $item;
272260
}

src/Contract/FlagInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public function getValue();
5252
*/
5353
public function setValue($value): void;
5454

55+
/**
56+
* @param mixed $value
57+
*/
58+
public function setTrustedValue($value): void;
59+
5560
/**
5661
* @return bool
5762
*/

src/Contract/ParserInterface.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ public function hasArg($nameOrIndex): bool;
141141
*/
142142
public function getArg($nameOrIndex, $default = null);
143143

144+
/**
145+
* @param string|int $nameOrIndex
146+
* @param mixed $value
147+
*
148+
* @return mixed
149+
*/
150+
// TODO public function setArg($nameOrIndex, $value): void;
151+
144152
/**
145153
* @return array
146154
* @psalm-return array<string, mixed>
@@ -176,4 +184,14 @@ public function isLocked(): bool;
176184
public function lock(): void;
177185

178186
public function unlock(): void;
187+
188+
/**
189+
* @param mixed $value
190+
*/
191+
// TODO public function setTrustedOpt(string $name, $value): void;
192+
193+
/**
194+
* @param mixed $value
195+
*/
196+
// TODO public function setTrustedArg(string $name, $value): void;
179197
}

src/Flag/AbstractFlag.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ public function getValue()
183183
return $this->value;
184184
}
185185

186+
/**
187+
* @param mixed $value
188+
*/
189+
public function setTrustedValue($value): void
190+
{
191+
$this->value = $value;
192+
}
193+
186194
/**
187195
* @param mixed $value
188196
*/
@@ -274,8 +282,9 @@ public function setType(string $type): void
274282
}
275283

276284
if (!FlagType::isValid($type)) {
285+
$kind = $this->getKind();
277286
$name = $this->getNameMark();
278-
throw new FlagException("cannot define invalid flag type: $type $name");
287+
throw new FlagException("invalid flag type '$type', $kind: $name");
279288
}
280289

281290
$this->type = $type;
@@ -295,8 +304,7 @@ public function getName(): string
295304
public function setName(string $name): void
296305
{
297306
if (!FlagHelper::isValidName($name)) {
298-
$kind = $this->getKind();
299-
throw new FlagException("invalid flag $kind name: " . $name);
307+
throw new FlagException("invalid flag option name: $name");
300308
}
301309

302310
$this->name = $name;

src/Flag/Argument.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace Toolkit\PFlag\Flag;
1111

12+
use Toolkit\Cli\Helper\FlagHelper;
13+
use Toolkit\PFlag\Exception\FlagException;
1214
use Toolkit\PFlag\FlagsParser;
1315
use Toolkit\Stdlib\Str;
1416
use function sprintf;
@@ -34,7 +36,12 @@ class Argument extends AbstractFlag
3436
public function setName(string $name): void
3537
{
3638
if ($name) {
37-
parent::setName($name);
39+
if (!FlagHelper::isValidName($name)) {
40+
$mark = sprintf('#%d(%s)', $this->index, $name);
41+
throw new FlagException("invalid flag argument name: $mark");
42+
}
43+
44+
$this->name = $name;
3845
}
3946
}
4047

src/Flags.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ public function bindingArguments(): self
406406
}
407407
}
408408

409-
if ($this->strictCheckArgs && $args) {
409+
if ($this->strictMatchArgs && $args) {
410410
throw new FlagException(sprintf('unknown arguments (error: "%s").', implode(' ', $args)));
411411
}
412412

src/FlagsParser.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ abstract class FlagsParser implements ParserInterface
147147
*
148148
* @var bool
149149
*/
150-
protected $delayValidate = false;
150+
// protected $delayValidate = false;
151151

152152
// -------------------- settings for parse option --------------------
153153

@@ -183,14 +183,19 @@ abstract class FlagsParser implements ParserInterface
183183
// -------------------- settings for parse argument --------------------
184184

185185
/**
186+
* Whether auto bind remaining args after option parsed
187+
*
186188
* @var bool
187189
*/
188190
protected $autoBindArgs = true;
189191

190192
/**
193+
* Strict match args number.
194+
* if exist unbind args, will throw FlagException
195+
*
191196
* @var bool
192197
*/
193-
protected $strictCheckArgs = false;
198+
protected $strictMatchArgs = false;
194199

195200
/**
196201
* Has array argument
@@ -371,10 +376,18 @@ public function getDesc(): string
371376

372377
/**
373378
* @param string $desc
379+
* @param bool $setOnEmpty only set on desc is empty
374380
*/
375-
public function setDesc(string $desc): void
381+
public function setDesc(string $desc, bool $setOnEmpty = false): void
376382
{
377-
$this->desc = $desc;
383+
// only set on desc is empty
384+
if ($setOnEmpty && $this->desc) {
385+
return;
386+
}
387+
388+
if ($desc) {
389+
$this->desc = $desc;
390+
}
378391
}
379392

380393
/**
@@ -510,17 +523,17 @@ public function setAutoBindArgs(bool $autoBindArgs): void
510523
/**
511524
* @return bool
512525
*/
513-
public function isStrictCheckArgs(): bool
526+
public function isStrictMatchArgs(): bool
514527
{
515-
return $this->strictCheckArgs;
528+
return $this->strictMatchArgs;
516529
}
517530

518531
/**
519-
* @param bool $strictCheckArgs
532+
* @param bool $strictMatchArgs
520533
*/
521-
public function setStrictCheckArgs(bool $strictCheckArgs): void
534+
public function setStrictMatchArgs(bool $strictMatchArgs): void
522535
{
523-
$this->strictCheckArgs = $strictCheckArgs;
536+
$this->strictMatchArgs = $strictMatchArgs;
524537
}
525538

526539
/**

0 commit comments

Comments
 (0)