Skip to content

Commit

Permalink
fix: validator tips not display on render help
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 10, 2022
1 parent 6246541 commit b83477f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/Concern/HelperRenderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ protected function formatDesc(Argument|Option|array $define): array

// validator limit
if (!empty($define['validator'])) {
/** @see ValidatorInterface */
$v = $define['validator'];

/** @see ValidatorInterface */
if (is_object($v) && method_exists($v, '__toString')) {
$limit = (string)$v;
$desc .= $limit ? ' ' . $limit : '';
$desc .= $limit ? "\n" . $limit : '';
}
}

Expand Down
31 changes: 20 additions & 11 deletions src/Flag/AbstractFlag.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ abstract class AbstractFlag implements ArrayAccess, FlagInterface
* @param string $name
* @param string $desc
* @param string $type
* @param bool $required
* @param bool $required
* @param mixed|null $default
*
* @return static
Expand All @@ -121,7 +121,7 @@ public static function new(
* Create by array define
*
* @param string $name
* @param array $define
* @param array $define
*
* @return static
*/
Expand All @@ -142,7 +142,7 @@ public static function newByArray(string $name, array $define): static
* @param string $name
* @param string $desc
* @param string $type
* @param bool $required
* @param bool $required
* @param mixed|null $default The default value
* - for Flag::ARG_OPTIONAL mode only
* - must be null for Flag::OPT_BOOLEAN
Expand Down Expand Up @@ -354,14 +354,15 @@ public function setDesc(string $desc): void
public function toArray(): array
{
return [
'name' => $this->name,
'desc' => $this->desc,
'type' => $this->type,
'default' => $this->default,
'envVar' => $this->envVar,
'required' => $this->required,
'isArray' => $this->isArray(),
'helpType' => $this->getHelpType(),
'name' => $this->name,
'desc' => $this->desc,
'type' => $this->type,
'default' => $this->default,
'envVar' => $this->envVar,
'required' => $this->required,
'validator' => $this->validator,
'isArray' => $this->isArray(),
'helpType' => $this->getHelpType(),
];
}

Expand Down Expand Up @@ -431,6 +432,14 @@ public function setValidator(?callable $validator): void
}
}

/**
* @return callable|ValidatorInterface|null
*/
public function getValidator(): callable|ValidatorInterface|null
{
return $this->validator;
}

/**
* @return string
*/
Expand Down
8 changes: 8 additions & 0 deletions src/FlagsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,14 @@ public function displayHelp(): void
Cli::println($this->buildHelp());
}

/**
* @return string
*/
public function toString(): string
{
return $this->buildHelp();
}

/**
* @return string
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/EmptyValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ public function checkInput(mixed $value, string $name): bool
*/
public function __toString(): string
{
return 'Not empty';
return '';
}
}
24 changes: 24 additions & 0 deletions test/FlagsParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Toolkit\PFlag\Exception\FlagException;
use Toolkit\PFlag\FlagsParser;
use Toolkit\PFlag\FlagType;
use Toolkit\PFlag\Validator\EnumValidator;
use function get_class;

/**
Expand Down Expand Up @@ -479,4 +480,27 @@ public function doTestParse_optValueIsKV(FlagsParser $fs): void
$this->assertEquals('vars', $fs->resolveAlias('var'));
$this->assertEquals(['key0=val0', 'port=3445'], $fs->getOpt('vars'));
}

public function testRenderHelp_withValidator(): void
{
foreach ($this->createParsers() as $fs) {
$this->doTestRenderHelp_withValidator($fs);
}
}

public function doTestRenderHelp_withValidator(FlagsParser $fs): void
{
$fs->addOptsByRules([
'env, e' => [
'type' => FlagType::STRING,
'required' => true,
'desc' => 'the env name, eg: qa',
'validator' => $v1 = new EnumValidator(['testing', 'qa']),
],
]);

$str = $fs->toString();
$this->assertStringContainsString('Allow: testing,qa', $str);
$this->assertStringContainsString((string)$v1, $str);
}
}

0 comments on commit b83477f

Please sign in to comment.