Skip to content

Commit 03e3331

Browse files
committed
feat: add more useful methods for the parser
1 parent 5bcb94e commit 03e3331

File tree

5 files changed

+177
-22
lines changed

5 files changed

+177
-22
lines changed

src/Contract/ParserInterface.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,22 @@ public function hasOpt(string $name): bool;
117117
public function getOpt(string $name, $default = null);
118118

119119
/**
120+
* Set option value, will format and validate value.
121+
*
120122
* @param string $name
121123
* @param mixed $value
122124
*
123125
* @return mixed
124126
*/
125127
public function setOpt(string $name, $value): void;
126128

129+
/**
130+
* Set trusted option value, will not format and validate value.
131+
*
132+
* @param mixed $value
133+
*/
134+
public function setTrustedOpt(string $name, $value): void;
135+
127136
/**
128137
* @param string|int $nameOrIndex
129138
*
@@ -142,12 +151,22 @@ public function hasArg($nameOrIndex): bool;
142151
public function getArg($nameOrIndex, $default = null);
143152

144153
/**
154+
* Set trusted argument value, will not format and validate value.
155+
*
145156
* @param string|int $nameOrIndex
146157
* @param mixed $value
147158
*
148159
* @return mixed
149160
*/
150-
// TODO public function setArg($nameOrIndex, $value): void;
161+
public function setArg($nameOrIndex, $value): void;
162+
163+
/**
164+
* Set trusted argument value, will not format and validate value.
165+
*
166+
* @param string $name
167+
* @param mixed $value
168+
*/
169+
public function setTrustedArg(string $name, $value): void;
151170

152171
/**
153172
* @return array
@@ -184,14 +203,4 @@ public function isLocked(): bool;
184203
public function lock(): void;
185204

186205
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;
197206
}

src/Flags.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,34 @@ public function hasArg($nameOrIndex): bool
576576
return isset($this->arguments[$index]);
577577
}
578578

579+
/**
580+
* @param string|int $nameOrIndex
581+
* @param mixed $value
582+
*/
583+
public function setArg($nameOrIndex, $value): void
584+
{
585+
$arg = $this->getArgument($nameOrIndex);
586+
if (!$arg) { // not exist
587+
throw new FlagException("flag argument '$nameOrIndex' is undefined");
588+
}
589+
590+
$arg->setValue($value);
591+
}
592+
593+
/**
594+
* @param string $name
595+
* @param mixed $value
596+
*/
597+
public function setTrustedArg(string $name, $value): void
598+
{
599+
$arg = $this->getArgument($name);
600+
if (!$arg) { // not exist
601+
throw new FlagException("flag argument '$name' is undefined");
602+
}
603+
604+
$arg->setTrustedValue($value);
605+
}
606+
579607
/**
580608
* @param string|int $nameOrIndex
581609
* @param null|mixed $default
@@ -584,11 +612,16 @@ public function hasArg($nameOrIndex): bool
584612
*/
585613
public function getArg($nameOrIndex, $default = null)
586614
{
587-
if ($arg = $this->getArgument($nameOrIndex)) {
615+
$arg = $this->getArgument($nameOrIndex);
616+
if (!$arg) { // not exist
617+
throw new FlagException("flag argument '$nameOrIndex' is undefined");
618+
}
619+
620+
if ($arg->hasValue()) {
588621
return $arg->getValue();
589622
}
590623

591-
return $default;
624+
return $default ?? $arg->getTypeDefault();
592625
}
593626

594627
/**
@@ -830,6 +863,20 @@ public function setOpt(string $name, $value): void
830863
$opt->setValue($value);
831864
}
832865

866+
/**
867+
* @param string $name
868+
* @param mixed $value
869+
*/
870+
public function setTrustedOpt(string $name, $value): void
871+
{
872+
$opt = $this->getDefinedOption($name);
873+
if (!$opt) { // not exist option
874+
throw new FlagException("flag option '$name' is undefined");
875+
}
876+
877+
$opt->setTrustedValue($value);
878+
}
879+
833880
/**
834881
* @param string $name
835882
*

src/SFlags.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,20 @@ public function setOpt(string $name, $value): void
819819
$this->opts[$name] = FlagType::fmtBasicTypeValue($define['type'], $value);
820820
}
821821

822+
/**
823+
* @param string $name
824+
* @param mixed $value
825+
*/
826+
public function setTrustedOpt(string $name, $value): void
827+
{
828+
$define = $this->optDefines[$name] ?? [];
829+
if (!$define) { // not exist
830+
throw new FlagException("flag option '$name' is undefined");
831+
}
832+
833+
$this->opts[$name] = $value;
834+
}
835+
822836
/**
823837
* @return array
824838
*/
@@ -836,7 +850,7 @@ public function hasArg($nameOrIndex): bool
836850
{
837851
$index = $this->getArgIndex($nameOrIndex);
838852

839-
return $index > -1 && isset($this->args[$index]);
853+
return $index > -1;
840854
}
841855

842856
/**
@@ -850,6 +864,36 @@ public function getArgument($nameOrIndex, $default = null)
850864
return $this->getArg($nameOrIndex, $default);
851865
}
852866

867+
/**
868+
* @param int|string $nameOrIndex
869+
* @param mixed $value
870+
*/
871+
public function setArg($nameOrIndex, $value): void
872+
{
873+
$index = $this->getArgIndex($nameOrIndex);
874+
if ($index < 0) {
875+
throw new FlagException("flag argument '$nameOrIndex' is undefined");
876+
}
877+
878+
$define = $this->argDefines[$index];
879+
// set value
880+
$this->args[$index] = FlagType::fmtBasicTypeValue($define['type'], $value);
881+
}
882+
883+
/**
884+
* @param string $name
885+
* @param mixed $value
886+
*/
887+
public function setTrustedArg(string $name, $value): void
888+
{
889+
$index = $this->getArgIndex($name);
890+
if ($index < 0) {
891+
throw new FlagException("flag argument '$name' is undefined");
892+
}
893+
894+
$this->args[$index] = $value;
895+
}
896+
853897
/**
854898
* @param int|string $nameOrIndex
855899
* @param mixed|null $default

test/BaseFlagsTestCase.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/**
2121
* Class BaseTestCase
22+
*
2223
* @package Inhere\ConsoleTest
2324
*/
2425
abstract class BaseFlagsTestCase extends TestCase
@@ -66,17 +67,18 @@ protected function createParsers(): array
6667
protected function bindingOptsAndArgs(FlagsParser $fs): void
6768
{
6869
$optRules = [
69-
'int-opt' => 'int;an int option',
70-
'int-opt1' => 'int;an int option with shorts;false;;i,g',
71-
'str-opt' => 'an string option',
72-
'str-opt1' => 'string;an int option with required;true',
73-
'str-opt2' => 'string;an int option with default;false;inhere',
74-
'bool-opt' => 'bool;an int option with an short;false;;b',
70+
'int-opt' => 'int;an int option',
71+
'int-opt1' => 'int;an int option with shorts;false;;i,g',
72+
'str-opt' => 'an string option',
73+
'str-opt1' => "string;an int option with required,\nand has multi line desc;true",
74+
'str-opt2' => 'string;an string option with default;false;inhere',
75+
'bool-opt' => 'bool;an int option with an short;false;;b',
7576
'-a, --bool-opt1' => 'bool;an int option with an short',
76-
's' => 'string;an string option only short name',
77+
's' => 'string;an string option only short name',
7778
];
7879
$argRules = [
7980
'intarg' => 'int;an int argument',
81+
'str-arg' => "an string argument,\nand has multi line desc",
8082
];
8183

8284
$fs->addOptsByRules($optRules);

test/FlagsParserTest.php

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
class FlagsParserTest extends BaseFlagsTestCase
1313
{
14-
public function testBasic(): void
14+
public function testParserBasic(): void
1515
{
1616
$this->runTestsWithParsers(function (FlagsParser $fs) {
1717
$this->doCheckBasic($fs);
@@ -23,11 +23,14 @@ private function doCheckBasic(FlagsParser $fs): void
2323
$this->assertTrue($fs->isEmpty());
2424
$this->assertFalse($fs->isNotEmpty());
2525
$this->assertFalse($fs->hasShortOpts());
26+
$this->assertFalse($fs->hasArg('github'));
2627

2728
$fs->setArgRules([
2829
'github' => 'an string argument'
2930
]);
3031
$this->assertFalse($fs->isEmpty());
32+
$this->assertTrue($fs->hasArg('github'));
33+
$this->assertFalse($fs->hasArg('not-exist'));
3134
$this->assertTrue($fs->isNotEmpty());
3235
$this->assertFalse($fs->hasShortOpts());
3336

@@ -37,6 +40,8 @@ private function doCheckBasic(FlagsParser $fs): void
3740
$this->assertFalse($fs->isEmpty());
3841
$this->assertTrue($fs->isNotEmpty());
3942
$this->assertTrue($fs->hasShortOpts());
43+
$this->assertTrue($fs->hasOpt('name'));
44+
$this->assertFalse($fs->hasOpt('not-exist'));
4045
}
4146

4247
public function testStopOnTwoHl(): void
@@ -273,4 +278,52 @@ private function doCheckErrorOnAddArg(FlagsParser $fs): void
273278
$this->assertSame(FlagException::class, get_class($e));
274279
$this->assertSame("invalid flag type 'invalid', argument: #0(name)", $e->getMessage());
275280
}
281+
282+
public function testSetOptAndSetArg(): void
283+
{
284+
foreach ($this->createParsers() as $fs) {
285+
$this->bindingOptsAndArgs($fs);
286+
$this->doCheckSetOptAndSetArg($fs);
287+
}
288+
}
289+
290+
private function doCheckSetOptAndSetArg($fs): void
291+
{
292+
$this->assertSame(0, $fs->getOpt('int-opt'));
293+
$this->assertSame('', $fs->getOpt('str-opt'));
294+
$this->assertSame('', $fs->getArg('str-arg'));
295+
296+
// test set
297+
$fs->setOpt('int-opt', '22');
298+
$fs->setOpt('str-opt', 'value');
299+
$fs->setArg('str-arg', 'value1');
300+
301+
$this->assertSame(22, $fs->getOpt('int-opt'));
302+
$this->assertSame('value', $fs->getOpt('str-opt'));
303+
$this->assertSame('value1', $fs->getArg('str-arg'));
304+
305+
// test set trust
306+
$fs->setTrustedOpt('int-opt', '33'); // will not format, validate value.
307+
$fs->setTrustedOpt('str-opt', 'trust-value');
308+
$fs->setTrustedArg('str-arg', 'trust-value1');
309+
310+
$this->assertSame('33', $fs->getOpt('int-opt'));
311+
$this->assertSame('trust-value', $fs->getOpt('str-opt'));
312+
$this->assertSame('trust-value1', $fs->getArg('str-arg'));
313+
314+
// test error
315+
$e = $this->runAndGetException(function (FlagsParser $fs) {
316+
$fs->setOpt('not-exist-opt', '22');
317+
}, $fs);
318+
319+
$this->assertSame(FlagException::class, get_class($e));
320+
$this->assertSame("flag option 'not-exist-opt' is undefined", $e->getMessage());
321+
322+
$e = $this->runAndGetException(function (FlagsParser $fs) {
323+
$fs->setArg('not-exist-arg', '22');
324+
}, $fs);
325+
326+
$this->assertSame(FlagException::class, get_class($e));
327+
$this->assertSame("flag argument 'not-exist-arg' is undefined", $e->getMessage());
328+
}
276329
}

0 commit comments

Comments
 (0)