f0b0498daf521a91dc83e36cb3fa7f20122b3705
6 * Command line arguments
8 class Args
implements \ArrayAccess
13 const OPTIONAL
= 0x000;
18 const REQUIRED
= 0x001;
21 * Only one value, even when used multiple times
26 * Aggregate an array, when used multiple times
31 * Option takes no argument
36 * Option requires an argument
41 * Option takes an optional argument
46 * Option halts processing
48 const HALT
= 0x10000000;
51 * Original option spec
69 * Compile the original spec
72 public function __construct(array $spec = null) {
73 $this->compile($spec);
77 * Compile the original spec
79 * @return pharext\CliArgs self
81 public function compile(array $spec = null) {
82 $this->orig
= array_merge($this->orig
, (array) $spec);
83 foreach ((array) $spec as $arg) {
85 $this->spec
["-".$arg[0]] = $arg;
87 $this->spec
["--".$arg[1]] = $arg;
96 public function getSpec() {
104 public function getCompiledSpec() {
109 * Parse command line arguments according to the compiled spec.
111 * The Generator yields any parsing errors.
112 * Parsing will stop when all arguments are processed or the first option
113 * flagged CliArgs::HALT was encountered.
119 public function parse($argc, array $argv) {
120 for ($i = 0; $i < $argc; ++
$i) {
123 if ($o{0} === '-' && strlen($o) > 2 && $o{1} !== '-') {
124 // multiple short opts, .e.g -vps
125 $argc +
= strlen($o) - 2;
126 array_splice($argv, $i, 1, array_map(function($s) {
128 }, str_split(substr($o, 1))));
130 } elseif ($o{0} === '-' && strlen($o) > 2 && $o{1} === '-' && 0 < ($eq = strpos($o, "="))) {
132 array_splice($argv, $i, 1, [
133 substr($o, 0, $eq++
),
139 if (!isset($this->spec
[$o])) {
140 yield
sprintf("Unknown option %s", $o);
141 } elseif (!$this->optAcceptsArg($o)) {
143 } elseif ($i+
1 < $argc && !isset($this->spec
[$argv[$i+
1]])) {
144 $this[$o] = $argv[++
$i];
145 } elseif ($this->optRequiresArg($o)) {
146 yield
sprintf("Option --%s requires an argument", $this->optLongName($o));
149 $this[$o] = $this->optDefaultArg($o);
152 if ($this->optHalts($o)) {
159 * Validate that all required options were given.
161 * The Generator yields any validation errors.
165 public function validate() {
166 $required = array_filter($this->orig
, function($spec) {
167 return $spec[3] & self
::REQUIRED
;
169 foreach ($required as $req) {
170 if (!strlen($this[$req[0]])) {
171 yield
sprintf("Option --%s is required", $req[1]);
177 * Retreive the default argument of an option
181 private function optDefaultArg($o) {
183 if (isset($this->spec
[$o][4])) {
184 return $this->spec
[$o][4];
190 * Retrieve the help message of an option
194 private function optHelp($o) {
196 if (isset($this->spec
[$o][2])) {
197 return $this->spec
[$o][2];
203 * Retrieve option's flags
207 private function optFlags($o) {
209 if (isset($this->spec
[$o])) {
210 return $this->spec
[$o][3];
216 * Check whether an option is flagged for halting argument processing
220 private function optHalts($o) {
221 return $this->optFlags($o) & self
::HALT
;
225 * Check whether an option needs an argument
229 private function optRequiresArg($o) {
230 return $this->optFlags($o) & self
::REQARG
;
234 * Check wether an option accepts any argument
238 private function optAcceptsArg($o) {
239 return $this->optFlags($o) & 0xf00;
243 * Check whether an option can be used more than once
247 private function optIsMulti($o) {
248 return $this->optFlags($o) & self
::MULTI
;
252 * Retreive the long name of an option
256 private function optLongName($o) {
258 return $this->spec
[$o][1];
262 * Retreive the short name of an option
266 private function optShortName($o) {
268 return $this->spec
[$o][0];
272 * Retreive the canonical name (--long-name) of an option
276 private function opt($o) {
278 if (strlen($o) > 1) {
287 * Implements ArrayAccess and virtual properties
289 function offsetExists($o) {
291 return isset($this->args
[$o]);
293 function __isset($o) {
294 return $this->offsetExists($o);
296 function offsetGet($o) {
298 if (isset($this->args
[$o])) {
299 return $this->args
[$o];
301 return $this->optDefaultArg($o);
304 return $this->offsetGet($o);
306 function offsetSet($o, $v) {
307 $osn = $this->optShortName($o);
308 $oln = $this->optLongName($o);
309 if ($this->optIsMulti($o)) {
311 $this->args
["-$osn"][] = $v;
313 $this->args
["--$oln"][] = $v;
316 $this->args
["-$osn"] = $v;
318 $this->args
["--$oln"] = $v;
321 function __set($o, $v) {
322 $this->offsetSet($o, $v);
324 function offsetUnset($o) {
325 unset($this->args
["-".$this->optShortName($o)]);
326 unset($this->args
["--".$this->optLongName($o)]);
328 function __unset($o) {
329 $this->offsetUnset($o);