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
70 * @param array|Traversable $spec
72 public function __construct($spec = null) {
73 if (is_array($spec) ||
$spec instanceof Traversable
) {
74 $this->compile($spec);
80 * Compile the original spec
81 * @param array|Traversable $spec
82 * @return pharext\CliArgs self
84 public function compile($spec) {
85 foreach ($spec as $arg) {
87 $this->spec
["-".$arg[0]] = $arg;
89 $this->spec
["--".$arg[1]] = $arg;
99 public function getSpec() {
107 public function getCompiledSpec() {
112 * Parse command line arguments according to the compiled spec.
114 * The Generator yields any parsing errors.
115 * Parsing will stop when all arguments are processed or the first option
116 * flagged CliArgs::HALT was encountered.
122 public function parse($argc, array $argv) {
123 for ($i = 0; $i < $argc; ++
$i) {
126 if ($o{0} === '-' && strlen($o) > 2 && $o{1} !== '-') {
127 // multiple short opts, .e.g -vps
128 $argc +
= strlen($o) - 2;
129 array_splice($argv, $i, 1, array_map(function($s) {
131 }, str_split(substr($o, 1))));
133 } elseif ($o{0} === '-' && strlen($o) > 2 && $o{1} === '-' && 0 < ($eq = strpos($o, "="))) {
135 array_splice($argv, $i, 1, [
136 substr($o, 0, $eq++
),
142 if (!isset($this->spec
[$o])) {
143 yield
sprintf("Unknown option %s", $o);
144 } elseif (!$this->optAcceptsArg($o)) {
146 } elseif ($i+
1 < $argc && !isset($this->spec
[$argv[$i+
1]])) {
147 $this[$o] = $argv[++
$i];
148 } elseif ($this->optRequiresArg($o)) {
149 yield
sprintf("Option --%s requires an argument", $this->optLongName($o));
152 $this[$o] = $this->optDefaultArg($o);
155 if ($this->optHalts($o)) {
162 * Validate that all required options were given.
164 * The Generator yields any validation errors.
168 public function validate() {
169 $required = array_filter($this->orig
, function($spec) {
170 return $spec[3] & self
::REQUIRED
;
172 foreach ($required as $req) {
173 if (!strlen($this[$req[0]])) {
174 yield
sprintf("Option --%s is required", $req[1]);
180 public function toArray() {
182 foreach ($this->spec
as $spec) {
183 $opt = $this->opt($spec[1]);
184 $args[$opt] = $this[$opt];
190 * Retreive the default argument of an option
194 private function optDefaultArg($o) {
196 if (isset($this->spec
[$o][4])) {
197 return $this->spec
[$o][4];
203 * Retrieve the help message of an option
207 private function optHelp($o) {
209 if (isset($this->spec
[$o][2])) {
210 return $this->spec
[$o][2];
216 * Retrieve option's flags
220 private function optFlags($o) {
222 if (isset($this->spec
[$o])) {
223 return $this->spec
[$o][3];
229 * Check whether an option is flagged for halting argument processing
233 private function optHalts($o) {
234 return $this->optFlags($o) & self
::HALT
;
238 * Check whether an option needs an argument
242 private function optRequiresArg($o) {
243 return $this->optFlags($o) & self
::REQARG
;
247 * Check wether an option accepts any argument
251 private function optAcceptsArg($o) {
252 return $this->optFlags($o) & 0xf00;
256 * Check whether an option can be used more than once
260 private function optIsMulti($o) {
261 return $this->optFlags($o) & self
::MULTI
;
265 * Retreive the long name of an option
269 private function optLongName($o) {
271 return $this->spec
[$o][1];
275 * Retreive the short name of an option
279 private function optShortName($o) {
281 return $this->spec
[$o][0];
285 * Retreive the canonical name (--long-name) of an option
289 private function opt($o) {
291 if (strlen($o) > 1) {
300 * Implements ArrayAccess and virtual properties
302 function offsetExists($o) {
304 return isset($this->args
[$o]);
306 function __isset($o) {
307 return $this->offsetExists($o);
309 function offsetGet($o) {
311 if (isset($this->args
[$o])) {
312 return $this->args
[$o];
314 return $this->optDefaultArg($o);
317 return $this->offsetGet($o);
319 function offsetSet($o, $v) {
320 $osn = $this->optShortName($o);
321 $oln = $this->optLongName($o);
322 if ($this->optIsMulti($o)) {
324 $this->args
["-$osn"][] = $v;
326 $this->args
["--$oln"][] = $v;
329 $this->args
["-$osn"] = $v;
331 $this->args
["--$oln"] = $v;
334 function __set($o, $v) {
335 $this->offsetSet($o, $v);
337 function offsetUnset($o) {
338 unset($this->args
["-".$this->optShortName($o)]);
339 unset($this->args
["--".$this->optLongName($o)]);
341 function __unset($o) {
342 $this->offsetUnset($o);