Skip to content

Commit ca832ff

Browse files
authored
Merge pull request #21 from smeghead/feature/use-getopt-php
feat: use getopt-php.
2 parents 28dadbc + 83bfbc9 commit ca832ff

File tree

4 files changed

+20
-113
lines changed

4 files changed

+20
-113
lines changed

bin/php-variable-hard-usage

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,26 @@ foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php
1010
}
1111
}
1212

13+
use GetOpt\GetOpt;
14+
use GetOpt\Option;
1315
use Smeghead\PhpVariableHardUsage\EntryPoint;
14-
use Smeghead\PhpVariableHardUsage\Option\GetOptions;
1516

16-
$getOptions = new GetOptions($_SERVER['argv']);
17-
$result = $getOptions->parse();
17+
$getOpt = new GetOpt([
18+
Option::create('h', 'help', GetOpt::NO_ARGUMENT),
19+
Option::create('v', 'version', GetOpt::NO_ARGUMENT),
20+
Option::create('t', 'threshold', GetOpt::REQUIRED_ARGUMENT)
21+
->setValidation(function ($value) {
22+
return is_numeric($value) && $value >= 0;
23+
}, 'Threshold must be a number greater than or equal to 0'),
24+
]);
25+
26+
try {
27+
$getOpt->process();
28+
} catch (Exception $exception) {
29+
file_put_contents('php://stderr', 'Error: ' . $exception->getMessage() . PHP_EOL);
30+
exit(1);
31+
}
1832

1933
$entryPoint = new EntryPoint();
20-
$exitCode = $entryPoint->run($result->options, $result->paths);
34+
$exitCode = $entryPoint->run($getOpt->options, $getOpt->operands);
2135
exit($exitCode);

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
],
88
"require": {
99
"php" : ">=8.1",
10-
"nikic/php-parser": "^5.2"
10+
"nikic/php-parser": "^5.2",
11+
"ulrichsg/getopt-php": "^4.0"
1112
},
1213
"require-dev": {
1314
"phpunit/phpunit": "^9.6",

src/Option/GetOptions.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

test/GetOptionsTest.php

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)