4 use Robo\Common\ProcessUtils;
7 * Use this to add arguments and options to the $arguments property.
14 protected $arguments = '';
17 * Pass argument to executable. Its value will be automatically escaped.
23 public function arg($arg)
25 return $this->args($arg);
29 * Pass methods parameters as arguments to executable. Argument values
30 * are automatically escaped.
32 * @param string|string[] $args
36 public function args($args)
38 if (!is_array($args)) {
39 $args = func_get_args();
41 $this->arguments .= ' ' . implode(' ', array_map('static::escape', $args));
46 * Pass the provided string in its raw (as provided) form as an argument to executable.
52 public function rawArg($arg)
54 $this->arguments .= " $arg";
60 * Escape the provided value, unless it contains only alphanumeric
61 * plus a few other basic characters.
63 * @param string $value
67 public static function escape($value)
69 if (preg_match('/^[a-zA-Z0-9\/\.@~_-]+$/', $value)) {
72 return ProcessUtils::escapeArgument($value);
76 * Pass option to executable. Options are prefixed with `--` , value can be provided in second parameter.
77 * Option values are automatically escaped.
79 * @param string $option
80 * @param string $value
81 * @param string $separator
85 public function option($option, $value = null, $separator = ' ')
87 if ($option !== null and strpos($option, '-') !== 0) {
88 $option = "--$option";
90 $this->arguments .= null == $option ? '' : " " . $option;
91 $this->arguments .= null == $value ? '' : $separator . static::escape($value);
96 * Pass multiple options to executable. The associative array contains
97 * the key:value pairs that become `--key value`, for each item in the array.
98 * Values are automatically escaped.
100 public function options(array $options, $separator = ' ')
102 foreach ($options as $option => $value) {
103 $this->option($option, $value, $separator);
109 * Pass an option with multiple values to executable. Value can be a string or array.
110 * Option values are automatically escaped.
112 * @param string $option
113 * @param string|array $value
114 * @param string $separator
118 public function optionList($option, $value = array(), $separator = ' ')
120 if (is_array($value)) {
121 foreach ($value as $item) {
122 $this->optionList($option, $item, $separator);
125 $this->option($option, $value, $separator);