| 148 |
lars |
1 |
<?php declare(strict_types=1);
|
|
|
2 |
/*
|
|
|
3 |
* This file is part of sebastian/cli-parser.
|
|
|
4 |
*
|
|
|
5 |
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
|
|
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 is_string;
|
|
|
23 |
use function key;
|
|
|
24 |
use function next;
|
|
|
25 |
use function preg_replace;
|
|
|
26 |
use function reset;
|
|
|
27 |
use function sort;
|
|
|
28 |
use function strlen;
|
|
|
29 |
use function strpos;
|
|
|
30 |
use function strstr;
|
|
|
31 |
use function substr;
|
|
|
32 |
|
|
|
33 |
final class Parser
|
|
|
34 |
{
|
|
|
35 |
/**
|
|
|
36 |
* @psalm-param list<string> $argv
|
|
|
37 |
* @psalm-param list<string> $longOptions
|
|
|
38 |
*
|
|
|
39 |
* @throws AmbiguousOptionException
|
|
|
40 |
* @throws RequiredOptionArgumentMissingException
|
|
|
41 |
* @throws OptionDoesNotAllowArgumentException
|
|
|
42 |
* @throws UnknownOptionException
|
|
|
43 |
*/
|
|
|
44 |
public function parse(array $argv, string $shortOptions, array $longOptions = null): array
|
|
|
45 |
{
|
|
|
46 |
if (empty($argv)) {
|
|
|
47 |
return [[], []];
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
$options = [];
|
|
|
51 |
$nonOptions = [];
|
|
|
52 |
|
|
|
53 |
if ($longOptions) {
|
|
|
54 |
sort($longOptions);
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
if (isset($argv[0][0]) && $argv[0][0] !== '-') {
|
|
|
58 |
array_shift($argv);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
reset($argv);
|
|
|
62 |
|
|
|
63 |
$argv = array_map('trim', $argv);
|
|
|
64 |
|
|
|
65 |
while (false !== $arg = current($argv)) {
|
|
|
66 |
$i = key($argv);
|
|
|
67 |
|
|
|
68 |
assert(is_int($i));
|
|
|
69 |
|
|
|
70 |
next($argv);
|
|
|
71 |
|
|
|
72 |
if ($arg === '') {
|
|
|
73 |
continue;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
if ($arg === '--') {
|
|
|
77 |
$nonOptions = array_merge($nonOptions, array_slice($argv, $i + 1));
|
|
|
78 |
|
|
|
79 |
break;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
if ($arg[0] !== '-' || (strlen($arg) > 1 && $arg[1] === '-' && !$longOptions)) {
|
|
|
83 |
$nonOptions[] = $arg;
|
|
|
84 |
|
|
|
85 |
continue;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
if (strlen($arg) > 1 && $arg[1] === '-' && is_array($longOptions)) {
|
|
|
89 |
$this->parseLongOption(
|
|
|
90 |
substr($arg, 2),
|
|
|
91 |
$longOptions,
|
|
|
92 |
$options,
|
|
|
93 |
$argv
|
|
|
94 |
);
|
|
|
95 |
} else {
|
|
|
96 |
$this->parseShortOption(
|
|
|
97 |
substr($arg, 1),
|
|
|
98 |
$shortOptions,
|
|
|
99 |
$options,
|
|
|
100 |
$argv
|
|
|
101 |
);
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
return [$options, $nonOptions];
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* @throws RequiredOptionArgumentMissingException
|
|
|
110 |
*/
|
|
|
111 |
private function parseShortOption(string $arg, string $shortOptions, array &$opts, array &$args): void
|
|
|
112 |
{
|
|
|
113 |
$argLength = strlen($arg);
|
|
|
114 |
|
|
|
115 |
for ($i = 0; $i < $argLength; $i++) {
|
|
|
116 |
$option = $arg[$i];
|
|
|
117 |
$optionArgument = null;
|
|
|
118 |
|
|
|
119 |
if ($arg[$i] === ':' || ($spec = strstr($shortOptions, $option)) === false) {
|
|
|
120 |
throw new UnknownOptionException('-' . $option);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
assert(is_string($spec));
|
|
|
124 |
|
|
|
125 |
if (strlen($spec) > 1 && $spec[1] === ':') {
|
|
|
126 |
if ($i + 1 < $argLength) {
|
|
|
127 |
$opts[] = [$option, substr($arg, $i + 1)];
|
|
|
128 |
|
|
|
129 |
break;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
if (!(strlen($spec) > 2 && $spec[2] === ':')) {
|
|
|
133 |
$optionArgument = current($args);
|
|
|
134 |
|
|
|
135 |
if (!$optionArgument) {
|
|
|
136 |
throw new RequiredOptionArgumentMissingException('-' . $option);
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
assert(is_string($optionArgument));
|
|
|
140 |
|
|
|
141 |
next($args);
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
$opts[] = [$option, $optionArgument];
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* @psalm-param list<string> $longOptions
|
|
|
151 |
*
|
|
|
152 |
* @throws AmbiguousOptionException
|
|
|
153 |
* @throws RequiredOptionArgumentMissingException
|
|
|
154 |
* @throws OptionDoesNotAllowArgumentException
|
|
|
155 |
* @throws UnknownOptionException
|
|
|
156 |
*/
|
|
|
157 |
private function parseLongOption(string $arg, array $longOptions, array &$opts, array &$args): void
|
|
|
158 |
{
|
|
|
159 |
$count = count($longOptions);
|
|
|
160 |
$list = explode('=', $arg);
|
|
|
161 |
$option = $list[0];
|
|
|
162 |
$optionArgument = null;
|
|
|
163 |
|
|
|
164 |
if (count($list) > 1) {
|
|
|
165 |
$optionArgument = $list[1];
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
$optionLength = strlen($option);
|
|
|
169 |
|
|
|
170 |
foreach ($longOptions as $i => $longOption) {
|
|
|
171 |
$opt_start = substr($longOption, 0, $optionLength);
|
|
|
172 |
|
|
|
173 |
if ($opt_start !== $option) {
|
|
|
174 |
continue;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
$opt_rest = substr($longOption, $optionLength);
|
|
|
178 |
|
|
|
179 |
if ($opt_rest !== '' && $i + 1 < $count && $option[0] !== '=' && strpos($longOptions[$i + 1], $option) === 0) {
|
|
|
180 |
throw new AmbiguousOptionException('--' . $option);
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
if (substr($longOption, -1) === '=') {
|
|
|
184 |
/* @noinspection StrlenInEmptyStringCheckContextInspection */
|
|
|
185 |
if (substr($longOption, -2) !== '==' && !strlen((string) $optionArgument)) {
|
|
|
186 |
if (false === $optionArgument = current($args)) {
|
|
|
187 |
throw new RequiredOptionArgumentMissingException('--' . $option);
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
next($args);
|
|
|
191 |
}
|
|
|
192 |
} elseif ($optionArgument) {
|
|
|
193 |
throw new OptionDoesNotAllowArgumentException('--' . $option);
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
$fullOption = '--' . preg_replace('/={1,2}$/', '', $longOption);
|
|
|
197 |
$opts[] = [$fullOption, $optionArgument];
|
|
|
198 |
|
|
|
199 |
return;
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
throw new UnknownOptionException('--' . $option);
|
|
|
203 |
}
|
|
|
204 |
}
|