├── ChangeLog.md ├── LICENSE ├── README.md ├── SECURITY.md ├── composer.json └── src ├── Parser.php └── exceptions ├── AmbiguousOptionException.php ├── Exception.php ├── OptionDoesNotAllowArgumentException.php ├── RequiredOptionArgumentMissingException.php └── UnknownOptionException.php /ChangeLog.md: -------------------------------------------------------------------------------- 1 | # ChangeLog 2 | 3 | All notable changes are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles. 4 | 5 | ## [4.0.0] - 2025-02-07 6 | 7 | ### Removed 8 | 9 | * This component is no longer supported on PHP 8.2 10 | 11 | ## [3.0.2] - 2024-07-03 12 | 13 | ### Changed 14 | 15 | * This project now uses PHPStan instead of Psalm for static analysis 16 | 17 | ## [3.0.1] - 2024-03-02 18 | 19 | ### Changed 20 | 21 | * Do not use implicitly nullable parameters 22 | 23 | ## [3.0.0] - 2024-02-02 24 | 25 | ### Removed 26 | 27 | * This component is no longer supported on PHP 8.1 28 | 29 | ## [2.0.1] - 2024-03-02 30 | 31 | ### Changed 32 | 33 | * Do not use implicitly nullable parameters 34 | 35 | ## [2.0.0] - 2023-02-03 36 | 37 | ### Removed 38 | 39 | * This component is no longer supported on PHP 7.3, PHP 7.4, and PHP 8.0 40 | 41 | ## [1.0.1] - 2020-09-28 42 | 43 | ### Changed 44 | 45 | * Changed PHP version constraint in `composer.json` from `^7.3 || ^8.0` to `>=7.3` 46 | 47 | ## [1.0.0] - 2020-08-12 48 | 49 | * Initial release 50 | 51 | [4.0.0]: https://github.com/sebastianbergmann/cli-parser/compare/3.0...4.0.0 52 | [3.0.2]: https://github.com/sebastianbergmann/cli-parser/compare/3.0.1...3.0.2 53 | [3.0.1]: https://github.com/sebastianbergmann/cli-parser/compare/3.0.0...3.0.1 54 | [3.0.0]: https://github.com/sebastianbergmann/cli-parser/compare/2.0...3.0.0 55 | [2.0.1]: https://github.com/sebastianbergmann/cli-parser/compare/2.0.0...2.0.1 56 | [2.0.0]: https://github.com/sebastianbergmann/cli-parser/compare/1.0.1...2.0.0 57 | [1.0.1]: https://github.com/sebastianbergmann/cli-parser/compare/1.0.0...1.0.1 58 | [1.0.0]: https://github.com/sebastianbergmann/cli-parser/compare/bb7bb3297957927962b0a3335befe7b66f7462e9...1.0.0 59 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2020-2025, Sebastian Bergmann 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Stable Version](https://poser.pugx.org/sebastian/cli-parser/v)](https://packagist.org/packages/sebastian/cli-parser) 2 | [![CI Status](https://github.com/sebastianbergmann/cli-parser/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/cli-parser/actions) 3 | [![codecov](https://codecov.io/gh/sebastianbergmann/cli-parser/branch/main/graph/badge.svg)](https://codecov.io/gh/sebastianbergmann/cli-parser) 4 | 5 | # sebastian/cli-parser 6 | 7 | Library for parsing `$_SERVER['argv']`, extracted from `phpunit/phpunit`. 8 | 9 | ## Installation 10 | 11 | You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/): 12 | 13 | ``` 14 | composer require sebastian/cli-parser 15 | ``` 16 | 17 | If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency: 18 | 19 | ``` 20 | composer require --dev sebastian/cli-parser 21 | ``` 22 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | If you believe you have found a security vulnerability in the library that is developed in this repository, please report it to us through coordinated disclosure. 4 | 5 | **Please do not report security vulnerabilities through public GitHub issues, discussions, or pull requests.** 6 | 7 | Instead, please email `sebastian@phpunit.de`. 8 | 9 | Please include as much of the information listed below as you can to help us better understand and resolve the issue: 10 | 11 | * The type of issue 12 | * Full paths of source file(s) related to the manifestation of the issue 13 | * The location of the affected source code (tag/branch/commit or direct URL) 14 | * Any special configuration required to reproduce the issue 15 | * Step-by-step instructions to reproduce the issue 16 | * Proof-of-concept or exploit code (if possible) 17 | * Impact of the issue, including how an attacker might exploit the issue 18 | 19 | This information will help us triage your report more quickly. 20 | 21 | ## Web Context 22 | 23 | The library that is developed in this repository was either extracted from [PHPUnit](https://github.com/sebastianbergmann/phpunit) or developed specifically as a dependency for PHPUnit. 24 | 25 | The library is developed with a focus on development environments and the command-line. No specific testing or hardening with regard to using the library in an HTTP or web context or with untrusted input data is performed. The library might also contain functionality that intentionally exposes internal application data for debugging purposes. 26 | 27 | If the library is used in a web application, the application developer is responsible for filtering inputs or escaping outputs as necessary and for verifying that the used functionality is safe for use within the intended context. 28 | 29 | Vulnerabilities specific to the use outside a development context will be fixed as applicable, provided that the fix does not have an averse effect on the primary use case for development purposes. 30 | 31 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sebastian/cli-parser", 3 | "description": "Library for parsing CLI options", 4 | "type": "library", 5 | "homepage": "https://github.com/sebastianbergmann/cli-parser", 6 | "license": "BSD-3-Clause", 7 | "authors": [ 8 | { 9 | "name": "Sebastian Bergmann", 10 | "email": "sebastian@phpunit.de", 11 | "role": "lead" 12 | } 13 | ], 14 | "support": { 15 | "issues": "https://github.com/sebastianbergmann/cli-parser/issues", 16 | "security": "https://github.com/sebastianbergmann/cli-parser/security/policy" 17 | }, 18 | "prefer-stable": true, 19 | "require": { 20 | "php": ">=8.3" 21 | }, 22 | "require-dev": { 23 | "phpunit/phpunit": "^12.0" 24 | }, 25 | "config": { 26 | "platform": { 27 | "php": "8.3.0" 28 | }, 29 | "optimize-autoloader": true, 30 | "sort-packages": true 31 | }, 32 | "autoload": { 33 | "classmap": [ 34 | "src/" 35 | ] 36 | }, 37 | "extra": { 38 | "branch-alias": { 39 | "dev-main": "4.0-dev" 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Parser.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\CliParser; 11 | 12 | use function array_map; 13 | use function array_merge; 14 | use function array_shift; 15 | use function array_slice; 16 | use function assert; 17 | use function count; 18 | use function current; 19 | use function explode; 20 | use function is_array; 21 | use function is_int; 22 | use function key; 23 | use function next; 24 | use function preg_replace; 25 | use function reset; 26 | use function sort; 27 | use function str_ends_with; 28 | use function str_starts_with; 29 | use function strlen; 30 | use function strstr; 31 | use function substr; 32 | 33 | final class Parser 34 | { 35 | /** 36 | * @param list $argv 37 | * @param list $longOptions 38 | * 39 | * @throws AmbiguousOptionException 40 | * @throws OptionDoesNotAllowArgumentException 41 | * @throws RequiredOptionArgumentMissingException 42 | * @throws UnknownOptionException 43 | * 44 | * @return array{0: list, 1: list} 45 | */ 46 | public function parse(array $argv, string $shortOptions, ?array $longOptions = null): array 47 | { 48 | if ($argv === []) { 49 | return [[], []]; 50 | } 51 | 52 | $options = []; 53 | $nonOptions = []; 54 | 55 | if ($longOptions !== null) { 56 | sort($longOptions); 57 | } 58 | 59 | if (isset($argv[0][0]) && $argv[0][0] !== '-') { 60 | array_shift($argv); 61 | } 62 | 63 | reset($argv); 64 | 65 | $argv = array_map('trim', $argv); 66 | 67 | while (false !== $arg = current($argv)) { 68 | $i = key($argv); 69 | 70 | assert(is_int($i)); 71 | 72 | next($argv); 73 | 74 | if ($arg === '') { 75 | continue; 76 | } 77 | 78 | if ($arg === '--') { 79 | $nonOptions = array_merge($nonOptions, array_slice($argv, $i + 1)); 80 | 81 | break; 82 | } 83 | 84 | if ($arg[0] !== '-' || (strlen($arg) > 1 && $arg[1] === '-' && $longOptions === null)) { 85 | $nonOptions[] = $arg; 86 | 87 | continue; 88 | } 89 | 90 | if (strlen($arg) > 1 && $arg[1] === '-' && is_array($longOptions)) { 91 | $this->parseLongOption( 92 | substr($arg, 2), 93 | $longOptions, 94 | $options, 95 | $argv, 96 | ); 97 | 98 | continue; 99 | } 100 | 101 | $this->parseShortOption( 102 | substr($arg, 1), 103 | $shortOptions, 104 | $options, 105 | $argv, 106 | ); 107 | } 108 | 109 | return [$options, $nonOptions]; 110 | } 111 | 112 | /** 113 | * @param list $options 114 | * @param list $argv 115 | * 116 | * @throws RequiredOptionArgumentMissingException 117 | */ 118 | private function parseShortOption(string $argument, string $shortOptions, array &$options, array &$argv): void 119 | { 120 | $argumentLength = strlen($argument); 121 | 122 | for ($i = 0; $i < $argumentLength; $i++) { 123 | $option = $argument[$i]; 124 | $optionArgument = null; 125 | 126 | if ($argument[$i] === ':' || ($spec = strstr($shortOptions, $option)) === false) { 127 | throw new UnknownOptionException('-' . $option); 128 | } 129 | 130 | if (strlen($spec) > 1 && $spec[1] === ':') { 131 | if ($i + 1 < $argumentLength) { 132 | $options[] = [$option, substr($argument, $i + 1)]; 133 | 134 | break; 135 | } 136 | 137 | if (!(strlen($spec) > 2 && $spec[2] === ':')) { 138 | $optionArgument = current($argv); 139 | 140 | if ($optionArgument === false) { 141 | throw new RequiredOptionArgumentMissingException('-' . $option); 142 | } 143 | 144 | next($argv); 145 | } 146 | } 147 | 148 | $options[] = [$option, $optionArgument]; 149 | } 150 | } 151 | 152 | /** 153 | * @param list $longOptions 154 | * @param list $options 155 | * @param list $argv 156 | * 157 | * @throws AmbiguousOptionException 158 | * @throws OptionDoesNotAllowArgumentException 159 | * @throws RequiredOptionArgumentMissingException 160 | * @throws UnknownOptionException 161 | */ 162 | private function parseLongOption(string $argument, array $longOptions, array &$options, array &$argv): void 163 | { 164 | $count = count($longOptions); 165 | $list = explode('=', $argument); 166 | $option = $list[0]; 167 | $optionArgument = null; 168 | 169 | if (count($list) > 1) { 170 | /** @phpstan-ignore offsetAccess.notFound */ 171 | $optionArgument = $list[1]; 172 | } 173 | 174 | $optionLength = strlen($option); 175 | 176 | foreach ($longOptions as $i => $longOption) { 177 | $opt_start = substr($longOption, 0, $optionLength); 178 | 179 | if ($opt_start !== $option) { 180 | continue; 181 | } 182 | 183 | $opt_rest = substr($longOption, $optionLength); 184 | 185 | if ($opt_rest !== '' && 186 | $i + 1 < $count && 187 | $option[0] !== '=' && 188 | /** @phpstan-ignore offsetAccess.notFound */ 189 | str_starts_with($longOptions[$i + 1], $option)) { 190 | throw new AmbiguousOptionException('--' . $option); 191 | } 192 | 193 | if (str_ends_with($longOption, '=')) { 194 | if (!str_ends_with($longOption, '==') && (string) $optionArgument === '') { 195 | if (false === $optionArgument = current($argv)) { 196 | throw new RequiredOptionArgumentMissingException('--' . $option); 197 | } 198 | 199 | next($argv); 200 | } 201 | } elseif ($optionArgument !== null) { 202 | throw new OptionDoesNotAllowArgumentException('--' . $option); 203 | } 204 | 205 | $fullOption = '--' . preg_replace('/={1,2}$/', '', $longOption); 206 | $options[] = [$fullOption, $optionArgument]; 207 | 208 | return; 209 | } 210 | 211 | throw new UnknownOptionException('--' . $option); 212 | } 213 | } 214 | -------------------------------------------------------------------------------- /src/exceptions/AmbiguousOptionException.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\CliParser; 11 | 12 | use function sprintf; 13 | use RuntimeException; 14 | 15 | final class AmbiguousOptionException extends RuntimeException implements Exception 16 | { 17 | public function __construct(string $option) 18 | { 19 | parent::__construct( 20 | sprintf( 21 | 'Option "%s" is ambiguous', 22 | $option, 23 | ), 24 | ); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/exceptions/Exception.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\CliParser; 11 | 12 | use Throwable; 13 | 14 | interface Exception extends Throwable 15 | { 16 | } 17 | -------------------------------------------------------------------------------- /src/exceptions/OptionDoesNotAllowArgumentException.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\CliParser; 11 | 12 | use function sprintf; 13 | use RuntimeException; 14 | 15 | final class OptionDoesNotAllowArgumentException extends RuntimeException implements Exception 16 | { 17 | public function __construct(string $option) 18 | { 19 | parent::__construct( 20 | sprintf( 21 | 'Option "%s" does not allow an argument', 22 | $option, 23 | ), 24 | ); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/exceptions/RequiredOptionArgumentMissingException.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\CliParser; 11 | 12 | use function sprintf; 13 | use RuntimeException; 14 | 15 | final class RequiredOptionArgumentMissingException extends RuntimeException implements Exception 16 | { 17 | public function __construct(string $option) 18 | { 19 | parent::__construct( 20 | sprintf( 21 | 'Required argument for option "%s" is missing', 22 | $option, 23 | ), 24 | ); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/exceptions/UnknownOptionException.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\CliParser; 11 | 12 | use function sprintf; 13 | use RuntimeException; 14 | 15 | final class UnknownOptionException extends RuntimeException implements Exception 16 | { 17 | public function __construct(string $option) 18 | { 19 | parent::__construct( 20 | sprintf( 21 | 'Unknown option "%s"', 22 | $option, 23 | ), 24 | ); 25 | } 26 | } 27 | --------------------------------------------------------------------------------