forked from getopt-php/getopt-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOption.php
162 lines (145 loc) · 4.3 KB
/
Option.php
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace Ulrichsg\Getopt;
/**
* Represents an option that Getopt accepts.
*/
class Option
{
private $short;
private $long;
private $mode;
private $description = '';
private $argument;
/**
* Creates a new option.
*
* @param string $short the option's short name (a single letter or digit) or null for long-only options
* @param string $long the option's long name (a string of 2+ letter/digit/_/- characters, starting with a letter
* or digit) or null for short-only options
* @param int $mode whether the option can/must have an argument (one of the constants defined in the Getopt class)
* (optional, defaults to no argument)
* @throws \InvalidArgumentException if both short and long name are null
*/
public function __construct($short, $long, $mode = Getopt::NO_ARGUMENT)
{
if (!$short && !$long) {
throw new \InvalidArgumentException("The short and long name may not both be empty");
}
$this->setShort($short);
$this->setLong($long);
$this->setMode($mode);
$this->argument = new Argument();
}
/**
* Defines a description for the option. This is only used for generating usage information.
*
* @param string $description
* @return Option this object (for chaining calls)
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Defines a default value for the option.
*
* @param mixed $value
* @return Option this object (for chaining calls)
*/
public function setDefaultValue($value)
{
$this->argument->setDefaultValue($value);
return $this;
}
/**
* Defines a validation function for the option.
*
* @param callable $function
* @return Option this object (for chaining calls)
*/
public function setValidation($function)
{
$this->argument->setValidation($function);
return $this;
}
/**
* Sets the argument object directly.
*
* @param Argument $arg
* @return Option this object (for chaining calls)
*/
public function setArgument(Argument $arg)
{
if ($this->mode == Getopt::NO_ARGUMENT) {
throw new \InvalidArgumentException("Option should not have any argument");
}
$this->argument = $arg;
return $this;
}
/**
* Returns true if the given string is equal to either the short or the long name.
*
* @param string $string
* @return bool
*/
public function matches($string)
{
return ($string === $this->short) || ($string === $this->long);
}
public function short()
{
return $this->short;
}
public function long()
{
return $this->long;
}
public function mode()
{
return $this->mode;
}
public function getDescription()
{
return $this->description;
}
/**
* Retrieve the argument object
*
* @return Argument
*/
public function getArgument()
{
return $this->argument;
}
/**
* Fluent interface for constructor so options can be added during construction
* @see Options::__construct()
*/
public static function create($short, $long, $mode = Getopt::NO_ARGUMENT)
{
return new self($short, $long, $mode);
}
private function setShort($short)
{
if (!(is_null($short) || preg_match("/^[a-zA-Z0-9]$/", $short))) {
throw new \InvalidArgumentException("Short option must be null or a letter/digit, found '$short'");
}
$this->short = $short;
}
private function setLong($long)
{
if (!(is_null($long) || preg_match("/^[a-zA-Z0-9][a-zA-Z0-9_-]{1,}$/", $long))) {
throw new \InvalidArgumentException("Long option must be null or an alphanumeric string, found '$long'");
}
$this->long = $long;
}
private function setMode($mode)
{
if (!in_array($mode, array(Getopt::NO_ARGUMENT, Getopt::OPTIONAL_ARGUMENT, Getopt::REQUIRED_ARGUMENT), true)) {
throw new \InvalidArgumentException("Option mode must be one of "
."Getopt::NO_ARGUMENT, Getopt::OPTIONAL_ARGUMENT and Getopt::REQUIRED_ARGUMENT");
}
$this->mode = $mode;
}
}