1: <?php
2:
3: namespace Charcoal\App\Script;
4:
5: use \Traversable;
6: use \RuntimeException;
7: use \InvalidArgumentException;
8:
9: 10: 11:
12: trait ArgScriptTrait
13: {
14: 15: 16: 17: 18: 19: 20: 21: 22: 23:
24: protected function parseAsArray($var, $delimiter = '[\s,]+')
25: {
26: if (is_string($var)) {
27: if (!is_string($delimiter)) {
28: throw new InvalidArgumentException('The delimiter must be a string.');
29: }
30:
31: $var = preg_split('#(?<!\\\\)'.$delimiter.'#', $var);
32: }
33:
34: if (is_array($var) || $var instanceof Traversable) {
35: return $var;
36: }
37:
38: throw new InvalidArgumentException('The value cannot be split.');
39: }
40:
41: 42: 43: 44: 45: 46:
47: protected function parseArguments()
48: {
49: $cli = $this->climate();
50: $args = $cli->arguments;
51:
52: $ask = $args->defined('interactive');
53: $params = $this->arguments();
54: foreach ($params as $key => $param) {
55: $setter = $this->setter($key);
56:
57: if (!is_callable([ $this, $setter ])) {
58: continue;
59: }
60:
61: $value = $args->get($key);
62: if (!empty($value) || is_numeric($value)) {
63: $this->{$setter}($value);
64: }
65:
66: if ($ask) {
67: if (isset($param['prompt'])) {
68: $label = $param['prompt'];
69: } else {
70: continue;
71: }
72:
73: $value = $this->input($key);
74: if (!empty($value) || is_numeric($value)) {
75: $this->{$setter}($value);
76: }
77: }
78: }
79:
80: return $this;
81: }
82: }
83: