Skip to content

Commit 8bf74c1

Browse files
committed
fix: not format value type on bind arg value in the SFlags
1 parent f6e0e5e commit 8bf74c1

File tree

6 files changed

+68
-10
lines changed

6 files changed

+68
-10
lines changed

src/Contract/ParserInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ public function hasInputArg($nameOrIndex): bool;
169169
*/
170170
public function getArgIndex($nameOrIndex): int;
171171

172+
/**
173+
* @param string|int $nameOrIndex
174+
*
175+
* @return array
176+
*/
177+
public function getArgDefine($nameOrIndex): array;
178+
172179
/**
173180
* Get an argument value by name
174181
*

src/Flag/Argument.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ public function setName(string $name): void
4545
}
4646
}
4747

48+
public function toArray(): array
49+
{
50+
$info = parent::toArray();
51+
52+
$info['index'] = $this->index;
53+
return $info;
54+
}
55+
4856
/**
4957
* @return string
5058
*/

src/Flags.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -632,17 +632,24 @@ public function getArg($nameOrIndex, $default = null)
632632
*/
633633
public function getArgument($nameOrIndex): ?Argument
634634
{
635-
if (is_string($nameOrIndex)) {
636-
if (!isset($this->name2index[$nameOrIndex])) {
637-
return null;
638-
}
635+
$index = $this->getArgIndex($nameOrIndex);
639636

640-
$index = $this->name2index[$nameOrIndex];
641-
} else {
642-
$index = (int)$nameOrIndex;
637+
return $this->arguments[$index] ?? null;
638+
}
639+
640+
/**
641+
* @param string|int $nameOrIndex
642+
*
643+
* @return array
644+
*/
645+
public function getArgDefine($nameOrIndex): array
646+
{
647+
$index = $this->getArgIndex($nameOrIndex);
648+
if ($index < 0) {
649+
throw new FlagException("flag argument '$nameOrIndex' is undefined");
643650
}
644651

645-
return $this->arguments[$index] ?? null;
652+
return $this->arguments[$index]->toArray();
646653
}
647654

648655
/**

src/SFlags.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,8 @@ public function bindingArguments(): void
584584
*/
585585
protected function collectArgValue($value, int $index, bool $isArray, array $define): void
586586
{
587+
$value = FlagType::fmtBasicTypeValue($define['type'], $value);
588+
587589
// has validator
588590
if ($cb = $define['validator']) {
589591
$name = $define['name'] ?: "#$index";
@@ -1005,7 +1007,21 @@ public function hasDefineArg($nameOrIndex): bool
10051007
{
10061008
$index = $this->getArgIndex($nameOrIndex);
10071009

1008-
return isset($this->argDefines[$index]);
1010+
return $index > -1;
1011+
}
1012+
1013+
/**
1014+
* @param string|int $nameOrIndex
1015+
* @return array
1016+
*/
1017+
public function getArgDefine($nameOrIndex): array
1018+
{
1019+
$index = $this->getArgIndex($nameOrIndex);
1020+
if ($index < 0) {
1021+
throw new FlagException("flag argument '$nameOrIndex' is undefined");
1022+
}
1023+
1024+
return $this->argDefines[$index];
10091025
}
10101026

10111027
/**

test/BaseFlagsTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected function bindingOptsAndArgs(FlagsParser $fs): void
7777
's' => 'string;an string option only short name',
7878
];
7979
$argRules = [
80-
'intarg' => 'int;an int argument',
80+
'int-arg' => 'int;an int argument',
8181
'str-arg' => "an string argument,\nand has multi line desc",
8282
];
8383

test/FlagsParserTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ private function doCheckBasic(FlagsParser $fs): void
3333
$this->assertFalse($fs->hasArg('not-exist'));
3434
$this->assertTrue($fs->isNotEmpty());
3535
$this->assertFalse($fs->hasShortOpts());
36+
$this->assertNotEmpty($fs->getArgDefine('github'));
3637

3738
$fs->setOptRules([
3839
'-n,--name' => 'an string option'
@@ -41,9 +42,28 @@ private function doCheckBasic(FlagsParser $fs): void
4142
$this->assertTrue($fs->isNotEmpty());
4243
$this->assertTrue($fs->hasShortOpts());
4344
$this->assertTrue($fs->hasOpt('name'));
45+
$this->assertFalse($fs->hasInputOpt('name'));
4446
$this->assertFalse($fs->hasOpt('not-exist'));
4547
}
4648

49+
public function testGetOptAndGetArg(): void
50+
{
51+
$this->runTestsWithParsers(function (FlagsParser $fs) {
52+
$this->bindingOptsAndArgs($fs);
53+
$this->doTestGetOptAndGetArg($fs);
54+
});
55+
}
56+
57+
private function doTestGetOptAndGetArg(FlagsParser $fs): void
58+
{
59+
// int type
60+
$ok = $fs->parse(['--str-opt1', 'val1', '--int-opt', '335', '233']);
61+
$this->assertTrue($ok);
62+
$this->assertSame(335, $fs->getOpt('int-opt'));
63+
$this->assertSame(233, $fs->getArg('int-arg'));
64+
$fs->resetResults();
65+
}
66+
4767
public function testStopOnTwoHl(): void
4868
{
4969
$this->runTestsWithParsers(function (FlagsParser $fs) {

0 commit comments

Comments
 (0)