Issues (20)

src/Utilities/ArgumentsParser.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Ballen\Clip\Utilities;
4
5
use Ballen\Collection\Collection;
6
7
/**
8
 * Clip
9
 * 
10
 * A package for speeding up development of PHP console (CLI) applications.
11
 *
12
 * @author Bobby Allen <[email protected]>
13
 * @license https://raw.githubusercontent.com/bobsta63/clip/master/LICENSE
14
 * @link https://github.com/allebb/clip
15
 * @link http://www.bobbyallen.me
16
 *
17
 */
18
class ArgumentsParser
19
{
20
21
    /**
22
     * Collection of CLI parsed "commands" eg. (reset).
23
     * @var Collection
24
     */
25
    private $commands;
26
27
    /**
28
     * Collection of CLI parsed "options" eg. (--option=something or --option="something").
29
     * @var Collection
30
     */
31
    private $options;
32
33
    /**
34
     * Collection of CLI parsed "flags" or serial of 'flags' eg. (-y or -yRf).
35
     * @var Collection
36
     */
37
    private $flags;
38
39
    /**
40
     * Collection of CLI parsed "arguments" eg. ().
41
     * @var Collection
42
     */
43
    private $arguments;
44
45
    /**
46
     * Instaniate with the argument array
47
     * @param array $arguments The CLI arguments array ($argv) to parse.
48
     */
49 60
    public function __construct(array $arguments = [])
50
    {
51
52 60
        $this->commands = new Collection();
53 60
        $this->options = new Collection();
54 60
        $this->flags = new Collection();
55 60
        $this->arguments = new Collection();
56 60
        $this->parse($arguments);
57
    }
58
59
    /**
60
     * Return the collection of commands (eg. help).
61
     * @return Collection
62
     */
63 8
    public function commands()
64
    {
65 8
        return $this->commands;
66
    }
67
68
    /**
69
     * Return the collection of options (eg. --username=bobby).
70
     * @return Collection
71
     */
72 8
    public function options()
73
    {
74 8
        return $this->options;
75
    }
76
77
    /**
78
     * Return the collection of flags (eg. -v or --pass).
79
     * @return Collection
80
     */
81 4
    public function flags()
82
    {
83 4
        return $this->flags;
84
    }
85
86
    /**
87
     * Return all CLI arguments in order.
88
     * @return Collection
89
     */
90 6
    public function arguments()
91
    {
92 6
        return $this->arguments;
93
    }
94
95
    /**
96
     * Parses the PHP $argv variable and adds them to the relivant type collection.
97
     * @todo Clean up the nested loops etc.
98
     * @param array $args
99
     * @return void
100
     */
101 60
    private function parse($args)
102
    {
103 60
        foreach ($args as $argument) {
104 58
            $this->arguments->push($argument);
105 58
            if ($this->detectFlags($argument)) {
106 30
                continue;
107
            }
108 58
            if ($this->detectOptions($argument)) {
109 30
                continue;
110
            }
111 58
            if ($this->detectConcatFlag($argument)) {
112 30
                continue;
113
            }
114 58
            $this->commands->push($argument);
115
        }
116
    }
117
118
    /**
119
     * Checks to see if a flag or serial flag (eg. -y or --pass) is set
120
     * @param string $flag The flag name/character (can be single or a serial character)
121
     * @return boolean
122
     */
123 2
    public function isFlagSet($flag)
124
    {
125 2
        if (in_array($flag, $this->flags->all()->toArray())) {
126 2
            return true;
127
        }
128 2
        return false;
129
    }
130
131
    /**
132
     * Retrieve the value of an option (eg. --username=jbloggs or --username="jbloggs")
133
     * @param string $name The name of the option to return
134
     * @param type $default An optional default value if its not set.
0 ignored issues
show
The type Ballen\Clip\Utilities\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
135
     * @return mixed
136
     */
137 2
    public function getOption($name, $default = false)
138
    {
139 2
        return $this->options->get($name, $default);
140
    }
141
142
    /**
143
     * Returns a command from the command index (eg. new or list).
144
     * @param int $part Will return the Nth command. eg 1 for "./consoleapp new myapp" will return 'new'.
145
     * @param string $default An optional default value if the command index is not set.
146
     */
147 28
    public function getCommand($part, $default = false)
148
    {
149 28
        return $this->commands->get($part, $default);
150
    }
151
152
    /**
153
     * Detects and sets "flag" type arguments.
154
     * @param string $argument The argument string.
155
     * @return boolean
156
     */
157 58
    private function detectFlags($argument)
158
    {
159 58
        if ((substr($argument, 0, 2) === '--') && (!strpos($argument, '='))) {
160 30
            $this->flags->push(ltrim($argument, '--'));
161 30
            return true;
162
        }
163 58
        return false;
164
    }
165
166
    /**
167
     * Detects and sets "concatenated flag" type arguments.
168
     * @param string $argument The argument string.
169
     * @return boolean
170
     */
171 58
    private function detectConcatFlag($argument)
172
    {
173 58
        if (substr($argument, 0, 1) === '-') {
174 30
            for ($i = 1; isset($argument[$i]); $i++) {
175 30
                $this->flags->push($argument[$i]);
176
            }
177 30
            return true;
178
        }
179 58
        return false;
180
    }
181
182
    /**
183
     * Detects and sets "option" type arguments.
184
     * @param type $argument The argument string.
185
     * @return boolean
186
     */
187 58
    private function detectOptions($argument)
188
    {
189 58
        if (substr($argument, 0, 2) === '--') {
190 30
            $name = substr($argument, 2);
191 30
            $value = '';
192 30
            if (strpos($name, '=')) {
193 30
                list($name, $value) = explode('=', $argument, 2);
194
            }
195 30
            $this->options->put(ltrim($name, '-'), trim($value, '"'));
196 30
            return true;
197
        }
198 58
        return false;
199
    }
200
201
    /**
202
     * Checks that an array of options have been set at runtime.
203
     * @param array $options The required options.
204
     */
205 4
    public function requiredOptions($options = [])
206
    {
207 4
        if (!count($options)) {
208 2
            return true;
209
        }
210 4
        foreach ($options as $option) {
211 4
            if (!$this->options()->get($option)) {
212 2
                return false;
213
            }
214
        }
215 2
        return true;
216
    }
217
}
218