1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Argument.php |
4
|
|
|
* |
5
|
|
|
* PHP version 7.3 and up. |
6
|
|
|
* |
7
|
|
|
* @category Core |
8
|
|
|
* @package Redbox_Cli |
9
|
|
|
* @author Johnny Mast <[email protected]> |
10
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
11
|
|
|
* @link https://github.com/johnnymast/mysql_websocket_chat |
12
|
|
|
* @since 1.5 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Redbox\Cli\Arguments; |
16
|
|
|
|
17
|
|
|
use Redbox\Cli\Object\ArgumentObject; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The Argument class represents one single argument |
21
|
|
|
* set to the Manager. To make sure its universal we use |
22
|
|
|
* the ArgumentObject as an abstract to this class. |
23
|
|
|
* |
24
|
|
|
* @category Core |
25
|
|
|
* @package Redbox_Cli |
26
|
|
|
* @author Johnny Mast <[email protected]> |
27
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
28
|
|
|
* @link https://github.com/johnnymast/redbox-cli |
29
|
|
|
* @since 1.0 |
30
|
|
|
*/ |
31
|
|
|
class Argument extends ArgumentObject |
32
|
|
|
{ |
33
|
|
|
const LINE_FMT = "\t%s\n\t\t%s\n"; |
34
|
|
|
|
35
|
|
|
public $prefix; |
36
|
|
|
public $defaultValue; |
37
|
|
|
public $longPrefix; |
38
|
|
|
public $description; |
39
|
|
|
public $required; |
40
|
|
|
public $name; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns the usage information for this argument something like |
44
|
|
|
* -u user, --user user, (default: me_myself_i) |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
12 |
|
public function usageInfo(): string |
49
|
|
|
{ |
50
|
12 |
|
$arg = array(); |
51
|
12 |
|
if ($this->prefix) { |
52
|
8 |
|
$arg[] = '-' . $this->prefix . ' ' . $this->name; |
53
|
|
|
} |
54
|
12 |
|
if ($this->longPrefix) { |
55
|
8 |
|
$arg[] = '--' . $this->longPrefix . ' ' . $this->name; |
56
|
|
|
} |
57
|
12 |
|
if ($this->defaultValue) { |
58
|
4 |
|
$arg[] = '(default: ' . $this->defaultValue . ')'; |
59
|
|
|
} |
60
|
12 |
|
if (!$this->prefix && !$this->longPrefix) { |
61
|
2 |
|
$arg[] = $this->name; |
62
|
|
|
} |
63
|
12 |
|
$arg = implode(', ', $arg); |
64
|
12 |
|
return $arg; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns a usage line something like. |
69
|
|
|
* -u user, --user user, (default: me_myself_i) |
70
|
|
|
* Username |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
7 |
|
public function usageLine(): string |
75
|
|
|
{ |
76
|
7 |
|
$arg = $this->usageInfo(); |
77
|
7 |
|
return sprintf(self::LINE_FMT, $arg, $this->description); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|