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) {
82 $this->orig
= array_merge($this->orig
, $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))));
132 if (!isset($this->spec
[$o])) {
133 yield
sprintf("Unknown option %s", $o);
134 } elseif (!$this->optAcceptsArg($o)) {
136 } elseif ($i+
1 < $argc && !isset($this->spec
[$argv[$i+
1]])) {
137 $this[$o] = $argv[++
$i];
138 } elseif ($this->optRequiresArg($o)) {
139 yield
sprintf("Option --%s requires an argument", $this->optLongName($o));
142 $this[$o] = $this->optDefaultArg($o);
145 if ($this->optHalts($o)) {
152 * Validate that all required options were given.
154 * The Generator yields any validation errors.
158 public function validate() {
159 $required = array_filter($this->orig
, function($spec) {
160 return $spec[3] & self
::REQUIRED
;
162 foreach ($required as $req) {
163 if (!isset($this[$req[0]])) {
164 yield
sprintf("Option --%s is required", $req[1]);
170 * Retreive the default argument of an option
174 private function optDefaultArg($o) {
176 if (isset($this->spec
[$o][4])) {
177 return $this->spec
[$o][4];
183 * Retrieve the help message of an option
187 private function optHelp($o) {
189 if (isset($this->spec
[$o][2])) {
190 return $this->spec
[$o][2];
196 * Retrieve option's flags
200 private function optFlags($o) {
202 if (isset($this->spec
[$o])) {
203 return $this->spec
[$o][3];
209 * Check whether an option is flagged for halting argument processing
213 private function optHalts($o) {
214 return $this->optFlags($o) & self
::HALT
;
218 * Check whether an option needs an argument
222 private function optRequiresArg($o) {
223 return $this->optFlags($o) & self
::REQARG
;
227 * Check wether an option accepts any argument
231 private function optAcceptsArg($o) {
232 return $this->optFlags($o) & 0xf00;
236 * Check whether an option can be used more than once
240 private function optIsMulti($o) {
241 return $this->optFlags($o) & self
::MULTI
;
245 * Retreive the long name of an option
249 private function optLongName($o) {
251 return $this->spec
[$o][1];
255 * Retreive the short name of an option
259 private function optShortName($o) {
261 return $this->spec
[$o][0];
265 * Retreive the canonical name (--long-name) of an option
269 private function opt($o) {
271 if (strlen($o) > 1) {
280 * Implements ArrayAccess and virtual properties
282 function offsetExists($o) {
284 return isset($this->args
[$o]);
286 function __isset($o) {
287 return $this->offsetExists($o);
289 function offsetGet($o) {
291 if (isset($this->args
[$o])) {
292 return $this->args
[$o];
294 return $this->optDefaultArg($o);
297 return $this->offsetGet($o);
299 function offsetSet($o, $v) {
300 $osn = $this->optShortName($o);
301 $oln = $this->optLongName($o);
302 if ($this->optIsMulti($o)) {
304 $this->args
["-$osn"][] = $v;
306 $this->args
["--$oln"][] = $v;
309 $this->args
["-$osn"] = $v;
311 $this->args
["--$oln"] = $v;
314 function __set($o, $v) {
315 $this->offsetSet($o, $v);
317 function offsetUnset($o) {
318 unset($this->args
["-".$this->optShortName($o)]);
319 unset($this->args
["--".$this->optLongName($o)]);
321 function __unset($o) {
322 $this->offsetUnset($o);