From 83bfbc94e909048d8fda5b0b38412eafcb86f149 Mon Sep 17 00:00:00 2001 From: smeghead Date: Fri, 28 Mar 2025 22:05:44 +0900 Subject: [PATCH] feat: use getopt-php. --- bin/php-variable-hard-usage | 22 ++++++++++--- composer.json | 3 +- src/Option/GetOptions.php | 47 ---------------------------- test/GetOptionsTest.php | 61 ------------------------------------- 4 files changed, 20 insertions(+), 113 deletions(-) delete mode 100644 src/Option/GetOptions.php delete mode 100644 test/GetOptionsTest.php diff --git a/bin/php-variable-hard-usage b/bin/php-variable-hard-usage index 016b40a..df350e3 100755 --- a/bin/php-variable-hard-usage +++ b/bin/php-variable-hard-usage @@ -10,12 +10,26 @@ foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php } } +use GetOpt\GetOpt; +use GetOpt\Option; use Smeghead\PhpVariableHardUsage\EntryPoint; -use Smeghead\PhpVariableHardUsage\Option\GetOptions; -$getOptions = new GetOptions($_SERVER['argv']); -$result = $getOptions->parse(); +$getOpt = new GetOpt([ + Option::create('h', 'help', GetOpt::NO_ARGUMENT), + Option::create('v', 'version', GetOpt::NO_ARGUMENT), + Option::create('t', 'threshold', GetOpt::REQUIRED_ARGUMENT) + ->setValidation(function ($value) { + return is_numeric($value) && $value >= 0; + }, 'Threshold must be a number greater than or equal to 0'), +]); + +try { + $getOpt->process(); +} catch (Exception $exception) { + file_put_contents('php://stderr', 'Error: ' . $exception->getMessage() . PHP_EOL); + exit(1); +} $entryPoint = new EntryPoint(); -$exitCode = $entryPoint->run($result->options, $result->paths); +$exitCode = $entryPoint->run($getOpt->options, $getOpt->operands); exit($exitCode); \ No newline at end of file diff --git a/composer.json b/composer.json index 572640d..23288cc 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,8 @@ ], "require": { "php" : ">=8.1", - "nikic/php-parser": "^5.2" + "nikic/php-parser": "^5.2", + "ulrichsg/getopt-php": "^4.0" }, "require-dev": { "phpunit/phpunit": "^9.6", diff --git a/src/Option/GetOptions.php b/src/Option/GetOptions.php deleted file mode 100644 index 4f3c48f..0000000 --- a/src/Option/GetOptions.php +++ /dev/null @@ -1,47 +0,0 @@ - $argv - */ - public function __construct(private readonly array $argv) - { - } - - public function parse(): GetOptionsResult - { - $options = []; - $args = []; - $count = count($this->argv); - for ($i = 0; $i < $count; $i++) { - $arg = $this->argv[$i]; - if (strpos($arg, '--') === 0) { - $key = substr($arg, 2); - $value = true; - if (strpos($key, '=') !== false) { - [$key, $value] = explode('=', $key, 2); - } - $options[$key] = $value; - } else { - $args[] = $arg; - } - } - return new GetOptionsResult($options, array_slice($args, 1)); - } -} - -final class GetOptionsResult { - /** - * @param array $options - * @param array $paths - */ - public function __construct( - public array $options, - public array $paths, - ) {} -} \ No newline at end of file diff --git a/test/GetOptionsTest.php b/test/GetOptionsTest.php deleted file mode 100644 index 9d44a97..0000000 --- a/test/GetOptionsTest.php +++ /dev/null @@ -1,61 +0,0 @@ -parse(); - $this->assertSame([], $result->paths); - } - - public function testHelp(): void - { - $argv = ['script.php', '--help']; - $sut = new GetOptions($argv); - $result = $sut->parse(); - $this->assertArrayHasKey('help', $result->options); - $this->assertSame(true, $result->options['help']); - } - - public function testVersion(): void - { - $argv = ['script.php', '--version']; - $sut = new GetOptions($argv); - $result = $sut->parse(); - $this->assertArrayHasKey('version', $result->options); - $this->assertSame(true, $result->options['version']); - } - - public function testSingle(): void - { - $argv = ['script.php', 'single', 'file.php']; - $sut = new GetOptions($argv); - $result = $sut->parse(); - $this->assertSame(['single', 'file.php'], $result->paths); - } - - public function testScopes(): void - { - $argv = ['script.php', 'scopes', 'dir1', 'dir2']; - $sut = new GetOptions($argv); - $result = $sut->parse(); - $this->assertSame(['scopes', 'dir1', 'dir2'], $result->paths); - } - - public function testCheckWithThreshold(): void - { - $argv = ['script.php', 'check', 'dir1', 'dir2', '--threshold=200']; - $sut = new GetOptions($argv); - $result = $sut->parse(); - $this->assertSame(['check', 'dir1', 'dir2'], $result->paths); - $this->assertArrayHasKey('threshold', $result->options); - $this->assertSame('200', $result->options['threshold']); - } -} \ No newline at end of file