1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ArgvPreprocessor.php |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace netfocusinc\argh; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Handles the preparation of PHP $argv arrays that is required before parsing |
11
|
|
|
* |
12
|
|
|
* @author Benjamin Hough |
13
|
|
|
* |
14
|
|
|
* @internal |
15
|
|
|
* |
16
|
|
|
* @since 1.0.0 |
17
|
|
|
*/ |
18
|
|
|
class ArgvPreprocessor |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Prepare an array of arguments for parsing |
23
|
|
|
* |
24
|
|
|
* PHP registers the $argv array with input from the command line. |
25
|
|
|
* This process includes some items that make it difficult to parse the way Argh wants to. |
26
|
|
|
* This function takes an $argv array and creates a new array where ... |
27
|
|
|
* Bracketed lists are put in a single element, regardless of spacing |
28
|
|
|
* Strings containing spaces are wrapped in single quotes; unless they are part of a list |
29
|
|
|
* |
30
|
|
|
* |
31
|
|
|
* @param array $args An '$argv' like array of arguments |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public static function process(array $argv) |
36
|
|
|
{ |
37
|
|
|
if(count($argv) < 1) |
38
|
|
|
{ |
39
|
|
|
throw(new ArghException(__CLASS__ . ': $argv is empty')); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// |
43
|
|
|
// Remove first element from $argv, it contains the name of the php script |
44
|
|
|
// |
45
|
|
|
|
46
|
|
|
$args = array_slice($argv, 1); |
47
|
|
|
|
48
|
|
|
// |
49
|
|
|
// Lists |
50
|
|
|
// |
51
|
|
|
|
52
|
|
|
for($i=0; $i<count($args); $i++) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
// Lists begin with an opening bracket '[' |
55
|
|
|
if( strpos($args[$i], '[') !== FALSE ) |
56
|
|
|
{ |
57
|
|
|
// Found the beginning of a list at $i |
58
|
|
|
//echo "DEBUG: Found beginning of a list at $i\n"; |
59
|
|
|
|
60
|
|
|
// Search for the end of the list; starting at the current element |
61
|
|
|
for($j=$i; $j<count($args); $j++) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
if( strpos($args[$j], ']') !== FALSE ) |
64
|
|
|
{ |
65
|
|
|
// Found the end of a list at $j |
66
|
|
|
//echo "DEBUG: Found end of a list at $j\n"; |
67
|
|
|
|
68
|
|
|
if($j != $i) |
69
|
|
|
{ |
70
|
|
|
// List does NOT open and close in the same element; elements must be re-combined |
71
|
|
|
|
72
|
|
|
// List closing bracket should always be the last character of the element |
73
|
|
|
if( strpos($args[$j], ']') == (strlen($args[$j])-1) ) |
74
|
|
|
{ |
75
|
|
|
// Create a new string for $args $i thru $j |
76
|
|
|
$list = ""; |
77
|
|
|
for($k=$i; $k<=$j; $k++) $list .= $args[$k]; |
78
|
|
|
|
79
|
|
|
//echo "DEBUG: Replace elements $i thru $j with: $list\n"; |
80
|
|
|
|
81
|
|
|
// Replace $args from $i thru $j with $list |
82
|
|
|
array_splice($args, $i, ($j-$i)+1, $list); |
83
|
|
|
|
84
|
|
|
// Stop searching for the end of the list |
85
|
|
|
break; |
86
|
|
|
} |
87
|
|
|
else |
88
|
|
|
{ |
89
|
|
|
throw new ArghException(__METHOD__ . ': Syntax Error: Invalid list.'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
} // END: if($j != $i) |
93
|
|
|
else |
94
|
|
|
{ |
95
|
|
|
//echo "DEBUG: List is already contained in a single element.\n"; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} // END: if( ($closeAt = strpos($args[$j], ']')) !== FALSE ) |
99
|
|
|
|
100
|
|
|
} // END: for($j=$i; $j<count($args); $j++) |
101
|
|
|
|
102
|
|
|
} // END: if( strpos($args[$i], ' ') !== FALSE ) |
103
|
|
|
|
104
|
|
|
} // END: for($i=0; $i<count($args); $i++) |
105
|
|
|
|
106
|
|
|
// |
107
|
|
|
// Quotes |
108
|
|
|
// |
109
|
|
|
|
110
|
|
|
// Check for arguments with spaces, these were originally quoted on the command line |
111
|
|
|
for($i=0; $i<count($args); $i++) |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
if( strpos($args[$i], ' ') !== FALSE ) |
114
|
|
|
{ |
115
|
|
|
|
116
|
|
|
// Check if argument is a list |
117
|
|
|
if( strpos($args[$i], '[') !== FALSE ) |
118
|
|
|
{ |
119
|
|
|
|
120
|
|
|
// |
121
|
|
|
// DO NOT ADD QUOTES SURROUNDING ITEMS IN A LIST |
122
|
|
|
// THEY WILL JUST HAVE TO BE STRIPPED LATER |
123
|
|
|
// |
124
|
|
|
|
125
|
|
|
} // END: if( strpos($args[$i], '[') !== FALSE ) |
126
|
|
|
else |
127
|
|
|
{ |
128
|
|
|
//!TODO: handle --msg="Hello World"; We don't want "--msg=Hello World" |
129
|
|
|
//! TODO: FIX. THIS IS BROKEN |
130
|
|
|
|
131
|
|
|
if( strpos($args[$i], '=') === FALSE ) |
132
|
|
|
{ |
133
|
|
|
// Wrap (space containing) argument in quotes |
134
|
|
|
$args[$i] = "'" . $args[$i] . "'"; |
135
|
|
|
} |
136
|
|
|
else |
137
|
|
|
{ |
138
|
|
|
// Wrap (space containing) argument value - after '=' sign in quotes |
139
|
|
|
|
140
|
|
|
// Split argument into parts, delimted by '=' |
141
|
|
|
$tokens = explode('=', $args[$i]); |
142
|
|
|
|
143
|
|
|
$args[$i] = $tokens[0] . '=' . "'" . $tokens[1] . "'"; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
} // END: if( strpos($args[$i], ' ') !== FALSE ) |
150
|
|
|
|
151
|
|
|
} // END: for($i=0; $i<count($args); $i++) |
152
|
|
|
|
153
|
|
|
// DEBUG |
154
|
|
|
//echo "\n------- AFTER PRE PROCESSING --------\n"; |
155
|
|
|
//print_r($args); |
156
|
|
|
//echo "\n-------------------------------------\n\n"; |
157
|
|
|
|
158
|
|
|
return $args; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
} |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: