-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathWithOptions.php
More file actions
133 lines (118 loc) · 3.6 KB
/
Copy pathWithOptions.php
File metadata and controls
133 lines (118 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace GetOpt;
trait WithOptions
{
/** @var Option[] */
protected $options = [];
/** @var Option[] */
protected $optionMapping = [];
/**
* Add $options to the list of options
*
* $options can be a string as for phps `getopt()` function, an array of Option instances or an array of arrays.
*
* You can also mix Option instances and arrays. E.g.:
* $getopt->addOptions([
* ['?', 'help', GetOpt::NO_ARGUMENT, 'Show this help'],
* new Option('v', 'verbose'),
* (new Option(null, 'version'))->setDescription('Print version and exit'),
* Option::create('q', 'quiet')->setDescription('Don\'t write any output')
* ]);
*
* @see OptionParser::parseArray() for how to use arrays
* @param string|array|Option[] $options
* @return self
*/
public function addOptions($options)
{
if (is_string($options)) {
$options = OptionParser::parseString($options);
}
if (!is_array($options)) {
throw new \InvalidArgumentException('GetOpt(): argument must be string or array');
}
foreach ($options as $option) {
$this->addOption($option);
}
return $this;
}
/**
* Add $option to the list of options
*
* $option can also be a string in format of php`s `getopt()` function. But only the first option will be added.
*
* Otherwise it has to be an array or an Option instance.
*
* @see GetOpt::addOptions() for more details
* @param string|array|Option $option
* @return self
*/
public function addOption($option)
{
if (!$option instanceof Option) {
if (is_string($option)) {
$options = OptionParser::parseString($option);
// this is addOption - so we use only the first one
$option = $options[0];
} elseif (is_array($option)) {
$option = OptionParser::parseArray($option);
} else {
throw new \InvalidArgumentException(sprintf(
'$option has to be a string, an array or an Option. %s given',
gettype($option)
));
}
}
if ($this->conflicts($option)) {
throw new \InvalidArgumentException('$option`s short and long name have to be unique');
}
$this->options[] = $option;
$short = $option->getShort();
$long = $option->getLong();
if ($short) {
$this->optionMapping[$short] = $option;
}
if ($long) {
$this->optionMapping[$long] = $option;
}
return $this;
}
/**
* Check if option conflicts with defined options.
*
* @param Option $option
* @return bool
*/
public function conflicts(Option $option): bool
{
$short = $option->getShort();
$long = $option->getLong();
return ($short && isset($this->optionMapping[$short])) || ($long && isset($this->optionMapping[$long]));
}
/**
* Get all options
*
* @return Option[]
*/
public function getOptions(): array
{
return $this->options;
}
/**
* Get an option by $name
*
* @param string $name Short or long name of the option
* @return Option
*/
public function getOption(string $name): ?Option
{
return isset($this->optionMapping[$name]) ? $this->optionMapping[$name] : null;
}
/**
* @return bool
*/
public function hasOptions(): bool
{
return !empty($this->options);
}
}