This repository was archived by the owner on Feb 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharguments.php
130 lines (112 loc) · 2.88 KB
/
arguments.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
<?php
namespace Argv;
/**
* Function to clean up provided CLI arguments (cut out the script as argument)
* @param array $arguments
* @return array
*/
function cleanArguments(array $arguments): array {
$relevantArguments = array_slice($arguments, 1);
return $relevantArguments;
}
/**
* Function to normalize a flag name
* @param string $argument
* @return string
*/
function reduceFlagName(string $argument): string {
return str_replace('-', '', $argument);
}
/**
* Function to check if the script call references on a command
* @param array $arguments
* @return boolean
*/
function isCommandCall(array $arguments): bool {
$argument = current($arguments);
return !isFlag($argument) && !isFlagAlias($argument);
}
/**
* Function to check if a provided argument is a flag
* @param string $argument
* @return boolean
*/
function isFlag(string $argument): bool {
return substr($argument, 0, 2) === '--';
}
/**
* Function to check if a provided argument is a flag alias
* @param string $argument
* @return boolean
*/
function isFlagAlias(string $argument): bool {
return substr($argument, 0, 1) === '-' && !isFlag($argument);
}
/**
* Function to retrieve the flags provided by the script call
* @param array $arguments
* @param array $aliases
* @return class@anonymous
*/
function getFlags(array $arguments, array $aliases = []) {
$flagValues = getValues($arguments);
$flags = new class {
public function add(string $flagName, $value = true) {
$this->{$flagName} = $value;
return $this;
}
public function __get(string $name): bool {
return isset($this->{$name}) && $this->{$name};
}
};
foreach ($arguments as $index => $argument) {
$flagValue = $flagValues->get(++$index) ?? true;
if (isFlag($argument)) {
$flags->add(reduceFlagName($argument), $flagValue);
} else if (
isFlagAlias($argument)
&& isset($aliases[reduceFlagName($argument)])
) {
$flags->add($aliases[reduceFlagName($argument)], $flagValue);
}
}
return $flags;
}
/**
* Function to retrieve the values provided by the script call
* @param array $arguments
* @return class@anonymous
*/
function getValues(array $arguments) {
$values = new class {
private $values = [];
public function add(string $value, int $originalIndex) {
$this->values[$originalIndex] = $value;
}
public function get(int $index) {
return $this->values[$index] ?? null;
}
public function all(): array
{
return $this->values;
}
public function first(): string
{
return current($this->values);
}
};
foreach ($arguments as $index => $argument) {
if (!isFlag($argument) && !isFlagAlias($argument)) {
$values->add($argument, $index);
}
}
return $values;
}
/**
* Alias function to retrieve the command name of cleaned arguments
* @param array $arguments
* @return string
*/
function getCommand(array $arguments): string {
return getValues($arguments)->first();
}