diff --git a/readme.md b/readme.md index 72464df..e6c0637 100644 --- a/readme.md +++ b/readme.md @@ -16,3 +16,19 @@ composer require nette/command-line It requires PHP version 8.1 and supports PHP up to 8.3. If you like Nette, **[please make a donation now](https://nette.org/donate)**. Thank you! + +Usage +----- + +``` + -o Short option + --long Long option + -o | --long Combined short & long + -o value With argument + -o With argument (alternative syntax) + -o=value With argument (alternative syntax) + -o value With default value (default: php) + -o value... Repeatable option + -o Enumerated required value + -o [alt1|alt2] Enumerated optional value +``` diff --git a/src/CommandLine/Parser.php b/src/CommandLine/Parser.php index 27aa168..be0d7cc 100644 --- a/src/CommandLine/Parser.php +++ b/src/CommandLine/Parser.php @@ -55,6 +55,23 @@ class Parser private string $help; + /* + Options: + -p Short option + --long Long option + -p | --long Combined short & long + + -p With default value (default: php) + + -p= + -p + -p info + -p info... + -p Repeatable + -p [option1|option2] Enum + */ + + public function __construct(string $help, array $defaults = []) { $this->help = $help; @@ -89,6 +106,73 @@ public function __construct(string $help, array $defaults = []) } + public function addArgument(string $name, array $options): static + { + $this->positional[] = $name; + $this->options[$name] = [ + self::Argument => null, + self::Optional => $options[self::Optional] ?? null, + self::Repeatable => $options[self::Repeatable] ?? null, + self::Enum => null, + self::Default => $options[self::Default] ?? null, + ]; + } + + + /** + * @param string|string[] $name + */ + public function addOption($name): static + { + $this->options[$name] = [ + self::Argument => false, + self::Optional => false, // ? + self::Repeatable => false, + self::Enum => null, + self::Default => null, + ]; + } + + + /** + * @param string|string[] $name + */ + public function addValueOption($name, array $options): static + { + foreach ((array) $name as $name) { + $this->options[$name] = [ + self::Argument => true, + self::Optional => false, // ? + self::Repeatable => false, + self::Enum => null, + self::Default => null, + ]; + } + } + + + /** + * @param string|string[] $name + */ + public function addEnumeratedOption($name, array $elements, array $options): static + { + foreach ((array) $name as $name) { + $this->options[$name] = [ + self::Argument => true, + self::Optional => false, // ? + self::Repeatable => false, + self::Enum => null, + self::Default => null, + ]; + } + } + + + public static function fromHelp(string $help): static + { + } + + public function parse(?array $args = null): array { if ($args === null) { @@ -181,6 +265,7 @@ public function parse(?array $args = null): array public function help(): void { + // TODO: generate help? echo $this->help; }