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;
92 * Parse command line arguments according to the compiled spec.
94 * The Generator yields any parsing errors.
95 * Parsing will stop when all arguments are processed or the first option
96 * flagged CliArgs::HALT was encountered.
102 public function parse($argc, array $argv) {
103 for ($i = 0; $i < $argc; ++
$i) {
106 if (!isset($this->spec
[$o])) {
107 yield
sprintf("Unknown option %s", $argv[$i]);
108 } elseif (!$this->optAcceptsArg($o)) {
110 } elseif ($i+
1 < $argc && !isset($this->spec
[$argv[$i+
1]])) {
111 $this[$o] = $argv[++
$i];
112 } elseif ($this->optNeedsArg($o)) {
113 yield
sprintf("Option --%s needs an argument", $this->optLongName($o));
116 $this[$o] = $this->optDefaultArg($o);
119 if ($this->optHalts($o)) {
126 * Validate that all required options were given.
128 * The Generator yields any validation errors.
132 public function validate() {
133 $required = array_filter($this->orig
, function($spec) {
134 return $spec[3] & self
::REQUIRED
;
136 foreach ($required as $req) {
137 if (!isset($this[$req[0]])) {
138 yield
sprintf("Option --%s is required", $req[1]);
144 * Output command line help message
145 * @param string $prog
147 public function help($prog) {
148 printf("\nUsage:\n\n $ %s", $prog);
152 foreach ($this->orig
as $spec) {
153 if ($spec[3] & self
::REQARG
) {
154 if ($spec[3] & self
::REQUIRED
) {
165 printf(" [-%s]", implode("|-", array_column($flags, 0)));
167 foreach ($required as $req) {
168 printf(" -%s <arg>", $req[0]);
171 printf(" [-%s <arg>]", implode("|-", array_column($optional, 0)));
174 foreach ($this->orig
as $spec) {
175 printf(" -%s|--%s %s", $spec[0], $spec[1], ($spec[3] & self
::REQARG
) ?
"<arg> " : (($spec[3] & self
::OPTARG
) ?
"[<arg>]" : " "));
176 printf("%s%s %s", str_repeat(" ", 16-strlen($spec[1])), $spec[2], ($spec[3] & self
::REQUIRED
) ?
"(REQUIRED)" : "");
177 if (isset($spec[4])) {
178 printf(" [%s]", $spec[4]);
186 * Retreive the default argument of an option
190 private function optDefaultArg($o) {
192 if (isset($this->spec
[$o][4])) {
193 return $this->spec
[$o][4];
199 * Retrieve the help message of an option
203 private function optHelp($o) {
205 if (isset($this->spec
[$o][2])) {
206 return $this->spec
[$o][2];
212 * Check whether an option is flagged for halting argument processing
216 private function optHalts($o) {
218 return $this->spec
[$o][3] & self
::HALT
;
222 * Check whether an option needs an argument
226 private function optNeedsArg($o) {
228 return $this->spec
[$o][3] & self
::REQARG
;
232 * Check wether an option accepts any argument
236 private function optAcceptsArg($o) {
238 return $this->spec
[$o][3] & 0xf00;
242 * Check whether an option can be used more than once
246 private function optIsMulti($o) {
248 return $this->spec
[$o][3] & 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 if ($this->optIsMulti($o)) {
308 $this->args
["-".$this->optShortName($o)][] = $v;
309 $this->args
["--".$this->optLongName($o)][] = $v;
311 $this->args
["-".$this->optShortName($o)] = $v;
312 $this->args
["--".$this->optLongName($o)] = $v;
315 function __set($o, $v) {
316 $this->offsetSet($o, $v);
318 function offsetUnset($o) {
319 unset($this->args
["-".$this->optShortName($o)]);
320 unset($this->args
["--".$this->optLongName($o)]);
322 function __unset($o) {
323 $this->offsetUnset($o);