diff --git a/src/CommandLine/Parser.php b/src/CommandLine/Parser.php index 10ee384..0c75125 100644 --- a/src/CommandLine/Parser.php +++ b/src/CommandLine/Parser.php @@ -16,12 +16,31 @@ class Parser { public const - ARGUMENT = 'argument', - OPTIONAL = 'optional', - REPEATABLE = 'repeatable', - ENUM = 'enum', - REALPATH = 'realpath', - VALUE = 'default'; + Argument = 'argument', + Optional = 'optional', + Repeatable = 'repeatable', + Enum = 'enum', + RealPath = 'realpath', + Default = 'default'; + + /** @deprecated use Parser::Argument */ + public const ARGUMENT = self::Argument; + + /** @deprecated use Parser::Optional */ + public const OPTIONAL = self::Optional; + + /** @deprecated use Parser::Repeatable */ + public const REPEATABLE = self::Repeatable; + + /** @deprecated use Parser::Enum */ + public const ENUM = self::Enum; + + /** @deprecated use Parser::Realpath */ + public const REALPATH = self::RealPath; + + /** @deprecated use Parser::Default */ + public const VALUE = self::Default; + /** @var array[] */ private $options = []; @@ -51,11 +70,11 @@ public function __construct(string $help, array $defaults = []) $name = end($m[1]); $opts = $this->options[$name] ?? []; $this->options[$name] = $opts + [ - self::ARGUMENT => (bool) end($m[2]), - self::OPTIONAL => isset($line[2]) || (substr(end($m[2]), 0, 1) === '[') || isset($opts[self::VALUE]), - self::REPEATABLE => (bool) end($m[3]), - self::ENUM => count($enums = explode('|', trim(end($m[2]), '<[]>'))) > 1 ? $enums : null, - self::VALUE => $line[2] ?? null, + self::Argument => (bool) end($m[2]), + self::Optional => isset($line[2]) || (substr(end($m[2]), 0, 1) === '[') || isset($opts[self::Default]), + self::Repeatable => (bool) end($m[3]), + self::Enum => count($enums = explode('|', trim(end($m[2]), '<[]>'))) > 1 ? $enums : null, + self::Default => $line[2] ?? null, ]; if ($name !== $m[1][0]) { $this->aliases[$m[1][0]] = $name; @@ -88,7 +107,7 @@ public function parse(?array $args = null): array $name = current($this->positional); $this->checkArg($this->options[$name], $arg); - if (empty($this->options[$name][self::REPEATABLE])) { + if (empty($this->options[$name][self::Repeatable])) { $params[$name] = $arg; next($this->positional); } else { @@ -109,31 +128,31 @@ public function parse(?array $args = null): array $opt = $this->options[$name]; - if ($arg !== true && empty($opt[self::ARGUMENT])) { + if ($arg !== true && empty($opt[self::Argument])) { throw new \Exception("Option $name has not argument."); - } elseif ($arg === true && !empty($opt[self::ARGUMENT])) { + } elseif ($arg === true && !empty($opt[self::Argument])) { if (isset($args[$i]) && $args[$i][0] !== '-') { $arg = $args[$i++]; - } elseif (empty($opt[self::OPTIONAL])) { + } elseif (empty($opt[self::Optional])) { throw new \Exception("Option $name requires argument."); } } if ( - !empty($opt[self::ENUM]) - && !in_array($arg, $opt[self::ENUM], true) + !empty($opt[self::Enum]) + && !in_array($arg, $opt[self::Enum], true) && !( - $opt[self::OPTIONAL] + $opt[self::Optional] && $arg === true ) ) { - throw new \Exception("Value of option $name must be " . implode(', or ', $opt[self::ENUM]) . '.'); + throw new \Exception("Value of option $name must be " . implode(', or ', $opt[self::Enum]) . '.'); } $this->checkArg($opt, $arg); - if (empty($opt[self::REPEATABLE])) { + if (empty($opt[self::Repeatable])) { $params[$name] = $arg; } else { $params[$name][] = $arg; @@ -143,15 +162,15 @@ public function parse(?array $args = null): array foreach ($this->options as $name => $opt) { if (isset($params[$name])) { continue; - } elseif (isset($opt[self::VALUE])) { - $params[$name] = $opt[self::VALUE]; - } elseif ($name[0] !== '-' && empty($opt[self::OPTIONAL])) { + } elseif (isset($opt[self::Default])) { + $params[$name] = $opt[self::Default]; + } elseif ($name[0] !== '-' && empty($opt[self::Optional])) { throw new \Exception("Missing required argument <$name>."); } else { $params[$name] = null; } - if (!empty($opt[self::REPEATABLE])) { + if (!empty($opt[self::Repeatable])) { $params[$name] = (array) $params[$name]; } } @@ -168,7 +187,7 @@ public function help(): void public function checkArg(array $opt, &$arg): void { - if (!empty($opt[self::REALPATH])) { + if (!empty($opt[self::RealPath])) { $path = realpath($arg); if ($path === false) { throw new \Exception("File path '$arg' not found."); diff --git a/tests/Parser.phpt b/tests/Parser.phpt index c38cc3a..4031106 100644 --- a/tests/Parser.phpt +++ b/tests/Parser.phpt @@ -40,7 +40,7 @@ test('default value', function () { $cmd = new Parser(' -p ', [ - '-p' => [Parser::VALUE => 123], + '-p' => [Parser::Default => 123], ]); Assert::same(['-p' => 123], $cmd->parse([])); @@ -116,7 +116,7 @@ test('optional argument', function () { $cmd = new Parser(' -p param ', [ - '-p' => [Parser::VALUE => 123], + '-p' => [Parser::Default => 123], ]); Assert::same(['-p' => 123], $cmd->parse([])); @@ -127,7 +127,7 @@ test('optional argument', function () { $cmd = new Parser(' -p param ', [ - '-p' => [Parser::OPTIONAL => true], + '-p' => [Parser::Optional => true], ]); Assert::same(['-p' => null], $cmd->parse([])); @@ -183,7 +183,7 @@ test('realpath', function () { $cmd = new Parser(' -p ', [ - '-p' => [Parser::REALPATH => true], + '-p' => [Parser::RealPath => true], ]); Assert::exception(function () use ($cmd) { @@ -210,21 +210,21 @@ test('positional arguments', function () { }, Throwable::class, 'Unexpected parameter val2.'); $cmd = new Parser('', [ - 'pos' => [Parser::REPEATABLE => true], + 'pos' => [Parser::Repeatable => true], ]); Assert::same(['pos' => ['val1', 'val2']], $cmd->parse(['val1', 'val2'])); $cmd = new Parser('', [ - 'pos' => [Parser::OPTIONAL => true], + 'pos' => [Parser::Optional => true], ]); Assert::same(['pos' => null], $cmd->parse([])); $cmd = new Parser('', [ - 'pos' => [Parser::VALUE => 'default', Parser::REPEATABLE => true], + 'pos' => [Parser::Default => 'default', Parser::Repeatable => true], ]); Assert::same(['pos' => ['default']], $cmd->parse([]));