diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index a373421..4ff4567 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -38,6 +38,7 @@ PhpCsFixer\Finder::create() ->exclude('test') ->exclude('runtime') + ->exclude('.github') ->exclude('vendor') ->in(__DIR__) ) diff --git a/example/cliapp.php b/example/cliapp.php index 443b08c..46ae421 100644 --- a/example/cliapp.php +++ b/example/cliapp.php @@ -1,4 +1,11 @@ -setName('myApp'); + $app->setDesc('my cli application. v1.0.1'); }) - ->add('test1', fn(FlagsParser $fs) => vdump($fs->getOpts()), [ + ->add('test1', fn (FlagsParser $fs) => vdump($fs->getOpts()), [ 'desc' => 'the test 1 command', 'options' => [ 'opt1' => 'opt1 for command test1', @@ -21,7 +29,7 @@ ], ]); -$app->add('test2', function (FlagsParser $fs) { +$cli->add('test2', function (FlagsParser $fs): void { Cli::info('options:'); vdump($fs->getOpts()); Cli::info('arguments:'); @@ -37,9 +45,8 @@ ] ]); -$app->add('show-err', fn() => throw new RuntimeException('test show exception')); +$cli->add('show-err', fn () => throw new RuntimeException('test show exception')); -$app->addHandler(DemoCmdHandler::class); - -$app->run(); +$cli->addHandler(DemoCmdHandler::class); +$cli->run(); diff --git a/example/clicmd.php b/example/clicmd.php index f08c7a9..9015703 100644 --- a/example/clicmd.php +++ b/example/clicmd.php @@ -1,4 +1,11 @@ -name = 'demo'; $cmd->desc = 'description for demo command'; @@ -28,11 +35,10 @@ ->withArguments([ 'arg1' => 'this is arg1, is string' ]) - ->setHandler(function (FlagsParser $fs) { + ->setHandler(function (FlagsParser $fs): void { Cli::info('options:'); vdump($fs->getOpts()); Cli::info('arguments:'); vdump($fs->getArgs()); }) ->run(); - diff --git a/example/not-stop_on_first.php b/example/not-stop_on_first.php index f43ac7a..8ee5689 100644 --- a/example/not-stop_on_first.php +++ b/example/not-stop_on_first.php @@ -1,5 +1,11 @@ -getArgs() ); -// vdump($fs->getArg('arrArg')); \ No newline at end of file +// vdump($fs->getArg('arrArg')); diff --git a/src/CliApp.php b/src/CliApp.php index a5106c7..dfb8d52 100644 --- a/src/CliApp.php +++ b/src/CliApp.php @@ -1,4 +1,11 @@ params['name'] = $name; } + /** + * @param string $name + */ + public function setDesc(string $name): void + { + $this->params['name'] = $name; + } + /** * @return FlagsParser */ diff --git a/src/CliCmd.php b/src/CliCmd.php index a50d0cf..fee1fbd 100644 --- a/src/CliCmd.php +++ b/src/CliCmd.php @@ -1,4 +1,11 @@ 1) { - if (!$name ) { + if (!$name) { $name = $k; } elseif ($kl > strlen($name)) { $aliases[] = $name; diff --git a/src/Contract/CmdHandlerInterface.php b/src/Contract/CmdHandlerInterface.php index 69765b1..83be517 100644 --- a/src/Contract/CmdHandlerInterface.php +++ b/src/Contract/CmdHandlerInterface.php @@ -1,4 +1,11 @@ shorts; return $info; } - } diff --git a/src/FlagUtil.php b/src/FlagUtil.php index 612fe9b..ebddf90 100644 --- a/src/FlagUtil.php +++ b/src/FlagUtil.php @@ -1,4 +1,11 @@ 'flags']); $testFunc($fs); - echo "- tests by use the parser: ", SFlags::class, "\n"; + echo '- tests by use the parser: ', SFlags::class, "\n"; $sfs = SFlags::new(['name' => 'simple-flags']); $testFunc($sfs); } @@ -84,5 +84,4 @@ protected function bindingOptsAndArgs(FlagsParser $fs): void $fs->addOptsByRules($optRules); $fs->addArgsByRules($argRules); } - } diff --git a/test/Cases/DemoCmdHandler.php b/test/Cases/DemoCmdHandler.php index ace1304..d2eb510 100644 --- a/test/Cases/DemoCmdHandler.php +++ b/test/Cases/DemoCmdHandler.php @@ -1,4 +1,11 @@ add('test1', fn() => $buf->set('key', 'in test1')); + $app->add('test1', fn () => $buf->set('key', 'in test1')); $app->addCommands([ 'test2' => [ 'desc' => 'desc for test2 command', - 'handler' => function () use ($buf) { + 'handler' => function () use ($buf): void { $buf->set('key', 'in test2'); }, 'options' => [ diff --git a/test/Concern/RuleParserTest.php b/test/Concern/RuleParserTest.php index 0a2c534..b6d8c12 100644 --- a/test/Concern/RuleParserTest.php +++ b/test/Concern/RuleParserTest.php @@ -1,4 +1,11 @@ assertSame(['23', '45'], $define['default']); } - public function testParseRule_string_hasAliases(): void { $p = RuleParser::new(); diff --git a/test/Flag/ArgumentTest.php b/test/Flag/ArgumentTest.php index cde21fe..23484af 100644 --- a/test/Flag/ArgumentTest.php +++ b/test/Flag/ArgumentTest.php @@ -1,4 +1,11 @@ runTestsWithParsers(function (FlagsParser $fs) { + $this->runTestsWithParsers(function (FlagsParser $fs): void { $this->doCheckBasic($fs); }); } @@ -63,7 +70,7 @@ private function doCheckBasic(FlagsParser $fs): void public function testGetOptAndGetArg(): void { - $this->runTestsWithParsers(function (FlagsParser $fs) { + $this->runTestsWithParsers(function (FlagsParser $fs): void { $this->bindingOptsAndArgs($fs); $this->doTestGetOptAndGetArg($fs); }); @@ -81,7 +88,7 @@ private function doTestGetOptAndGetArg(FlagsParser $fs): void $fs->resetResults(); // getMustOpt - $e = $this->runAndGetException(function (FlagsParser $fs) { + $e = $this->runAndGetException(function (FlagsParser $fs): void { $fs->getMustOpt('str-opt'); }, $fs); @@ -89,7 +96,7 @@ private function doTestGetOptAndGetArg(FlagsParser $fs): void $this->assertSame("The option 'str-opt' is required", $e->getMessage()); // getMustArg - $e = $this->runAndGetException(function (FlagsParser $fs) { + $e = $this->runAndGetException(function (FlagsParser $fs): void { $fs->getMustArg('str-arg'); }, $fs); @@ -99,7 +106,7 @@ private function doTestGetOptAndGetArg(FlagsParser $fs): void public function testParse_specialArg(): void { - $this->runTestsWithParsers(function (FlagsParser $fs) { + $this->runTestsWithParsers(function (FlagsParser $fs): void { $fs->addOpt('a', '', 'an string opt'); $fs->addArg('num', 'an int arg', 'int'); @@ -117,7 +124,7 @@ public function testParse_specialArg(): void public function testStopOnTwoHl(): void { - $this->runTestsWithParsers(function (FlagsParser $fs) { + $this->runTestsWithParsers(function (FlagsParser $fs): void { $this->doCheckStopOnTwoHl($fs); }); } @@ -140,7 +147,7 @@ private function doCheckStopOnTwoHl(FlagsParser $fs): void public function testStopOnFirstArg(): void { - $this->runTestsWithParsers(function (FlagsParser $fs) { + $this->runTestsWithParsers(function (FlagsParser $fs): void { $this->runStopOnFirstArg($fs); }); } @@ -191,11 +198,11 @@ private function runStopOnFirstArg(FlagsParser $fs): void public function testSkipOnUndefined(): void { - $this->runTestsWithParsers(function (FlagsParser $fs) { + $this->runTestsWithParsers(function (FlagsParser $fs): void { $this->runSkipOnUndefined_false($fs); }); - $this->runTestsWithParsers(function (FlagsParser $fs) { + $this->runTestsWithParsers(function (FlagsParser $fs): void { $this->runSkipOnUndefined_true($fs); }); } @@ -240,7 +247,7 @@ private function runSkipOnUndefined_true(FlagsParser $fs): void public function testRenderHelp_showTypeOnHelp(): void { - $this->runTestsWithParsers(function (FlagsParser $fs) { + $this->runTestsWithParsers(function (FlagsParser $fs): void { $this->bindingOptsAndArgs($fs); $this->renderFlagsHelp($fs); }); @@ -248,7 +255,7 @@ public function testRenderHelp_showTypeOnHelp(): void public function testRenderHelp_showTypeOnHelp_false(): void { - $this->runTestsWithParsers(function (FlagsParser $fs) { + $this->runTestsWithParsers(function (FlagsParser $fs): void { $fs->setShowTypeOnHelp(false); $this->bindingOptsAndArgs($fs); $this->renderFlagsHelp($fs); @@ -271,7 +278,7 @@ public function testException_RepeatName(): void protected function doCheckRepeatName(FlagsParser $fs): void { - $e = $this->runAndGetException(function (FlagsParser $fs) { + $e = $this->runAndGetException(function (FlagsParser $fs): void { $fs->addOptsByRules([ '--name' => 'an string', 'name' => 'an string', @@ -280,7 +287,7 @@ protected function doCheckRepeatName(FlagsParser $fs): void $this->assertEquals(FlagException::class, get_class($e)); - $e = $this->runAndGetException(function (FlagsParser $fs) { + $e = $this->runAndGetException(function (FlagsParser $fs): void { $fs->addArgsByRules([ 'name' => 'an string', ]); @@ -300,7 +307,7 @@ public function testException_addOpt(): void private function doCheckErrorOnAddOpt(FlagsParser $fs): void { // empty name - $e = $this->runAndGetException(function (FlagsParser $fs) { + $e = $this->runAndGetException(function (FlagsParser $fs): void { $fs->addOpt('', '', 'an desc'); }, $fs); @@ -308,7 +315,7 @@ private function doCheckErrorOnAddOpt(FlagsParser $fs): void $this->assertSame('invalid flag option name: ', $e->getMessage()); // invalid name - $e = $this->runAndGetException(function (FlagsParser $fs) { + $e = $this->runAndGetException(function (FlagsParser $fs): void { $fs->addOpt('name=+', '', 'an desc'); }, $fs); @@ -316,7 +323,7 @@ private function doCheckErrorOnAddOpt(FlagsParser $fs): void $this->assertSame('invalid flag option name: name=+', $e->getMessage()); // invalid type - $e = $this->runAndGetException(function (FlagsParser $fs) { + $e = $this->runAndGetException(function (FlagsParser $fs): void { $fs->addOpt('name', '', 'an desc', 'invalid'); }, $fs); @@ -334,7 +341,7 @@ public function testException_addArg(): void private function doCheckErrorOnAddArg(FlagsParser $fs): void { // invalid name - $e = $this->runAndGetException(function (FlagsParser $fs) { + $e = $this->runAndGetException(function (FlagsParser $fs): void { $fs->addArg('name=+', 'an desc'); }, $fs); @@ -342,7 +349,7 @@ private function doCheckErrorOnAddArg(FlagsParser $fs): void $this->assertSame('invalid flag argument name: #0(name=+)', $e->getMessage()); // invalid type - $e = $this->runAndGetException(function (FlagsParser $fs) { + $e = $this->runAndGetException(function (FlagsParser $fs): void { $fs->addArg('name', 'an desc', 'invalid'); }, $fs); @@ -383,14 +390,14 @@ private function doCheckSetOptAndSetArg($fs): void $this->assertSame('trust-value1', $fs->getArg('str-arg')); // test error - $e = $this->runAndGetException(function (FlagsParser $fs) { + $e = $this->runAndGetException(function (FlagsParser $fs): void { $fs->setOpt('not-exist-opt', '22'); }, $fs); $this->assertSame(FlagException::class, get_class($e)); $this->assertSame("flag option 'not-exist-opt' is undefined", $e->getMessage()); - $e = $this->runAndGetException(function (FlagsParser $fs) { + $e = $this->runAndGetException(function (FlagsParser $fs): void { $fs->setArg('not-exist-arg', '22'); }, $fs); diff --git a/test/FlagsTest.php b/test/FlagsTest.php index fb00317..092e0dd 100644 --- a/test/FlagsTest.php +++ b/test/FlagsTest.php @@ -40,7 +40,7 @@ public function testAddOptionAndParse(): void $fs->parse($flags); self::assertTrue($fs->hasMatched('name')); - $this->assertNotEmpty( $fs->getOption('name')); + $this->assertNotEmpty($fs->getOption('name')); $this->assertSame('inhere', $fs->getOpt('name')); $this->assertSame(0, $fs->getOpt('age', 0)); $this->assertSame(89, $fs->getOpt('int1')); diff --git a/test/SFlagsTest.php b/test/SFlagsTest.php index d15eea3..55fefb1 100644 --- a/test/SFlagsTest.php +++ b/test/SFlagsTest.php @@ -139,5 +139,4 @@ public function testOptRule_required(): void ]); $fs->parse([]); } - } diff --git a/test/ValidatorTest.php b/test/ValidatorTest.php index e43b473..04c16e0 100644 --- a/test/ValidatorTest.php +++ b/test/ValidatorTest.php @@ -1,4 +1,11 @@