6 * Command line arguments
8 class CliArgs
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) {
84 foreach ((array) $spec as $arg) {
85 $this->spec
["-".$arg[0]] = $arg;
86 $this->spec
["--".$arg[1]] = $arg;
95 public function getSpec() {
100 * Parse command line arguments according to the compiled spec.
102 * The Generator yields any parsing errors.
103 * Parsing will stop when all arguments are processed or the first option
104 * flagged CliArgs::HALT was encountered.
110 public function parse($argc, array $argv) {
111 for ($i = 0; $i < $argc; ++
$i) {
114 if ($o{0} === '-' && strlen($o) > 2 && $o{1} !== '-') {
115 // multiple short opts, .e.g -vps
116 $argc +
= strlen($o) - 2;
117 array_splice($argv, $i, 1, array_map(function($s) {
119 }, str_split(substr($o, 1))));
123 if (!isset($this->spec
[$o])) {
124 yield
sprintf("Unknown option %s", $o);
125 } elseif (!$this->optAcceptsArg($o)) {
127 } elseif ($i+
1 < $argc && !isset($this->spec
[$argv[$i+
1]])) {
128 $this[$o] = $argv[++
$i];
129 } elseif ($this->optRequiresArg($o)) {
130 yield
sprintf("Option --%s requires an argument", $this->optLongName($o));
133 $this[$o] = $this->optDefaultArg($o);
136 if ($this->optHalts($o)) {
143 * Validate that all required options were given.
145 * The Generator yields any validation errors.
149 public function validate() {
150 $required = array_filter($this->orig
, function($spec) {
151 return $spec[3] & self
::REQUIRED
;
153 foreach ($required as $req) {
154 if (!isset($this[$req[0]])) {
155 yield
sprintf("Option --%s is required", $req[1]);
161 * Output command line help message
162 * @param string $prog
164 public function help($prog) {
165 printf("\nUsage:\n\n $ %s", $prog);
169 foreach ($this->orig
as $spec) {
170 if ($spec[3] & self
::REQARG
) {
171 if ($spec[3] & self
::REQUIRED
) {
182 printf(" [-%s]", implode("|-", array_column($flags, 0)));
184 foreach ($required as $req) {
185 printf(" -%s <arg>", $req[0]);
188 printf(" [-%s <arg>]", implode("|-", array_column($optional, 0)));
191 foreach ($this->orig
as $spec) {
192 printf(" -%s|--%s %s", $spec[0], $spec[1], ($spec[3] & self
::REQARG
) ?
"<arg> " : (($spec[3] & self
::OPTARG
) ?
"[<arg>]" : " "));
193 printf("%s%s %s", str_repeat(" ", 16-strlen($spec[1])), $spec[2], ($spec[3] & self
::REQUIRED
) ?
"(REQUIRED)" : "");
194 if (isset($spec[4])) {
195 printf(" [%s]", $spec[4]);
203 * Retreive the default argument of an option
207 private function optDefaultArg($o) {
209 if (isset($this->spec
[$o][4])) {
210 return $this->spec
[$o][4];
216 * Retrieve the help message of an option
220 private function optHelp($o) {
222 if (isset($this->spec
[$o][2])) {
223 return $this->spec
[$o][2];
229 * Retrieve option's flags
233 private function optFlags($o) {
235 if (isset($this->spec
[$o])) {
236 return $this->spec
[$o][3];
242 * Check whether an option is flagged for halting argument processing
246 private function optHalts($o) {
247 return $this->optFlags($o) & self
::HALT
;
251 * Check whether an option needs an argument
255 private function optRequiresArg($o) {
256 return $this->optFlags($o) & self
::REQARG
;
260 * Check wether an option accepts any argument
264 private function optAcceptsArg($o) {
265 return $this->optFlags($o) & 0xf00;
269 * Check whether an option can be used more than once
273 private function optIsMulti($o) {
274 return $this->optFlags($o) & self
::MULTI
;
278 * Retreive the long name of an option
282 private function optLongName($o) {
284 return $this->spec
[$o][1];
288 * Retreive the short name of an option
292 private function optShortName($o) {
294 return $this->spec
[$o][0];
298 * Retreive the canonical name (--long-name) of an option
302 private function opt($o) {
304 if (strlen($o) > 1) {
313 * Implements ArrayAccess and virtual properties
315 function offsetExists($o) {
317 return isset($this->args
[$o]);
319 function __isset($o) {
320 return $this->offsetExists($o);
322 function offsetGet($o) {
324 if (isset($this->args
[$o])) {
325 return $this->args
[$o];
327 return $this->optDefaultArg($o);
330 return $this->offsetGet($o);
332 function offsetSet($o, $v) {
333 if ($this->optIsMulti($o)) {
334 $this->args
["-".$this->optShortName($o)][] = $v;
335 $this->args
["--".$this->optLongName($o)][] = $v;
337 $this->args
["-".$this->optShortName($o)] = $v;
338 $this->args
["--".$this->optLongName($o)] = $v;
341 function __set($o, $v) {
342 $this->offsetSet($o, $v);
344 function offsetUnset($o) {
345 unset($this->args
["-".$this->optShortName($o)]);
346 unset($this->args
["--".$this->optLongName($o)]);
348 function __unset($o) {
349 $this->offsetUnset($o);