Skip to content

Commit b83477f

Browse files
committed
fix: validator tips not display on render help
1 parent 6246541 commit b83477f

File tree

5 files changed

+55
-14
lines changed

5 files changed

+55
-14
lines changed

src/Concern/HelperRenderTrait.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,12 @@ protected function formatDesc(Argument|Option|array $define): array
216216

217217
// validator limit
218218
if (!empty($define['validator'])) {
219+
/** @see ValidatorInterface */
219220
$v = $define['validator'];
220221

221-
/** @see ValidatorInterface */
222222
if (is_object($v) && method_exists($v, '__toString')) {
223223
$limit = (string)$v;
224-
$desc .= $limit ? ' ' . $limit : '';
224+
$desc .= $limit ? "\n" . $limit : '';
225225
}
226226
}
227227

src/Flag/AbstractFlag.php

+20-11
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ abstract class AbstractFlag implements ArrayAccess, FlagInterface
102102
* @param string $name
103103
* @param string $desc
104104
* @param string $type
105-
* @param bool $required
105+
* @param bool $required
106106
* @param mixed|null $default
107107
*
108108
* @return static
@@ -121,7 +121,7 @@ public static function new(
121121
* Create by array define
122122
*
123123
* @param string $name
124-
* @param array $define
124+
* @param array $define
125125
*
126126
* @return static
127127
*/
@@ -142,7 +142,7 @@ public static function newByArray(string $name, array $define): static
142142
* @param string $name
143143
* @param string $desc
144144
* @param string $type
145-
* @param bool $required
145+
* @param bool $required
146146
* @param mixed|null $default The default value
147147
* - for Flag::ARG_OPTIONAL mode only
148148
* - must be null for Flag::OPT_BOOLEAN
@@ -354,14 +354,15 @@ public function setDesc(string $desc): void
354354
public function toArray(): array
355355
{
356356
return [
357-
'name' => $this->name,
358-
'desc' => $this->desc,
359-
'type' => $this->type,
360-
'default' => $this->default,
361-
'envVar' => $this->envVar,
362-
'required' => $this->required,
363-
'isArray' => $this->isArray(),
364-
'helpType' => $this->getHelpType(),
357+
'name' => $this->name,
358+
'desc' => $this->desc,
359+
'type' => $this->type,
360+
'default' => $this->default,
361+
'envVar' => $this->envVar,
362+
'required' => $this->required,
363+
'validator' => $this->validator,
364+
'isArray' => $this->isArray(),
365+
'helpType' => $this->getHelpType(),
365366
];
366367
}
367368

@@ -431,6 +432,14 @@ public function setValidator(?callable $validator): void
431432
}
432433
}
433434

435+
/**
436+
* @return callable|ValidatorInterface|null
437+
*/
438+
public function getValidator(): callable|ValidatorInterface|null
439+
{
440+
return $this->validator;
441+
}
442+
434443
/**
435444
* @return string
436445
*/

src/FlagsParser.php

+8
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,14 @@ public function displayHelp(): void
370370
Cli::println($this->buildHelp());
371371
}
372372

373+
/**
374+
* @return string
375+
*/
376+
public function toString(): string
377+
{
378+
return $this->buildHelp();
379+
}
380+
373381
/**
374382
* @return string
375383
*/

src/Validator/EmptyValidator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ public function checkInput(mixed $value, string $name): bool
5050
*/
5151
public function __toString(): string
5252
{
53-
return 'Not empty';
53+
return '';
5454
}
5555
}

test/FlagsParserTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Toolkit\PFlag\Exception\FlagException;
1414
use Toolkit\PFlag\FlagsParser;
1515
use Toolkit\PFlag\FlagType;
16+
use Toolkit\PFlag\Validator\EnumValidator;
1617
use function get_class;
1718

1819
/**
@@ -479,4 +480,27 @@ public function doTestParse_optValueIsKV(FlagsParser $fs): void
479480
$this->assertEquals('vars', $fs->resolveAlias('var'));
480481
$this->assertEquals(['key0=val0', 'port=3445'], $fs->getOpt('vars'));
481482
}
483+
484+
public function testRenderHelp_withValidator(): void
485+
{
486+
foreach ($this->createParsers() as $fs) {
487+
$this->doTestRenderHelp_withValidator($fs);
488+
}
489+
}
490+
491+
public function doTestRenderHelp_withValidator(FlagsParser $fs): void
492+
{
493+
$fs->addOptsByRules([
494+
'env, e' => [
495+
'type' => FlagType::STRING,
496+
'required' => true,
497+
'desc' => 'the env name, eg: qa',
498+
'validator' => $v1 = new EnumValidator(['testing', 'qa']),
499+
],
500+
]);
501+
502+
$str = $fs->toString();
503+
$this->assertStringContainsString('Allow: testing,qa', $str);
504+
$this->assertStringContainsString((string)$v1, $str);
505+
}
482506
}

0 commit comments

Comments
 (0)