1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* CommandParameter.php |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace netfocusinc\argh; |
8
|
|
|
|
9
|
|
|
use netfocusinc\argh\Parameter; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A Command Parameter. |
13
|
|
|
* |
14
|
|
|
* Subtype of Parameter that represents a command string |
15
|
|
|
* Command parameters must define an array of 'options'. |
16
|
|
|
* When interpreting command line arguments, commands will only be matched with command line input |
17
|
|
|
* when the command line argument matches one of the pre-defined 'options'. |
18
|
|
|
* |
19
|
|
|
* @author Benjamin Hough |
20
|
|
|
* |
21
|
|
|
* @api |
22
|
|
|
* |
23
|
|
|
* @since 1.0.0 |
24
|
|
|
*/ |
25
|
|
|
class CommandParameter extends Parameter |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
// |
29
|
|
|
// STATIC FUNCTIONS |
30
|
|
|
// |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
// |
34
|
|
|
// PUBLIC FUNCTIONS |
35
|
|
|
// |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Construct a new CommandParameter. |
39
|
|
|
* |
40
|
|
|
* Overrides Parameter constructor to enforce required 'options' |
41
|
|
|
* |
42
|
|
|
* @since 1.0.0 |
43
|
|
|
* |
44
|
|
|
* @param string $name |
45
|
|
|
* @param string $flag |
46
|
|
|
* @param bool $required |
47
|
|
|
* @param string $default The default 'option' to use for this command, when none is specified on the command line |
48
|
|
|
* @param string $description |
49
|
|
|
* @param array $options |
50
|
|
|
*/ |
51
|
|
|
|
52
|
|
|
public function __construct(string $name, string $flag=null, bool $required=FALSE, $default=null, string $description=null, array $options=array()) |
53
|
|
|
{ |
54
|
|
|
|
55
|
|
|
// Required a non-empty 'options' |
56
|
|
|
if( count($options) < 1 ) |
57
|
|
|
{ |
58
|
|
|
throw(new ArghException('CommandParameter must have options')); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Call Parameter (Parent) Constructor |
62
|
|
|
parent::__construct($name, $flag, $required, $default, $description, $options); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns ARGH_TYPE_COMMAND |
67
|
|
|
* |
68
|
|
|
* @since 1.0.0 |
69
|
|
|
* |
70
|
|
|
* @return int |
71
|
|
|
*/ |
72
|
|
|
public function getParameterType(): int |
73
|
|
|
{ |
74
|
|
|
return Parameter::ARGH_TYPE_COMMAND; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Sets the string value of this Parameter. |
79
|
|
|
* |
80
|
|
|
* Checks that the given 'value' matches one of this Commands 'options' before settings its value. |
81
|
|
|
* |
82
|
|
|
* @since 1.0.0 |
83
|
|
|
* |
84
|
|
|
* @param string $value |
85
|
|
|
* |
86
|
|
|
* @throws ArghException When $value is not one of this CommandsParameter's 'options' |
87
|
|
|
*/ |
88
|
|
|
public function setValue($value) |
89
|
|
|
{ |
90
|
|
|
if(is_array($value)) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
throw(new ArghException('Command value cannot be set to an array')); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if( ($this->hasOptions()) && (!$this->isOption($value)) ) |
96
|
|
|
{ |
97
|
|
|
throw(new ArghException('Not a valid option for \'' . $this->getName() . '\'')); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->value = strval($value); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|