@@ -21,7 +21,7 @@ public static function getopt(array $args, $short_options, $long_options = null)
21
21
return array (array (), array ());
22
22
}
23
23
24
- $ opts = array ();
24
+ $ opts = array ();
25
25
$ non_opts = array ();
26
26
27
27
if ($ long_options ) {
@@ -35,7 +35,10 @@ public static function getopt(array $args, $short_options, $long_options = null)
35
35
reset ($ args );
36
36
array_map ('trim ' , $ args );
37
37
38
- while (list ($ i , $ arg ) = each ($ args )) {
38
+
39
+ for ($ i = 0 ; $ i < count ($ args ); $ i ++) {
40
+ $ arg = $ args [$ i ];
41
+
39
42
if ($ arg == '' ) {
40
43
continue ;
41
44
}
@@ -45,24 +48,30 @@ public static function getopt(array $args, $short_options, $long_options = null)
45
48
break ;
46
49
}
47
50
51
+ $ nextArg = isset ($ args [$ i ]) ? $ args [$ i ] : null ;
52
+
48
53
if ($ arg [0 ] != '- ' ||
49
54
(strlen ($ arg ) > 1 && $ arg [1 ] == '- ' && !$ long_options )) {
50
55
$ non_opts [] = $ args [$ i ];
51
56
continue ;
52
57
} elseif (strlen ($ arg ) > 1 && $ arg [1 ] == '- ' ) {
53
- self ::parseLongOption (
58
+ if ( self ::parseLongOption (
54
59
substr ($ arg , 2 ),
55
60
$ long_options ,
56
61
$ opts ,
57
- $ args
58
- );
62
+ $ nextArg
63
+ )) {
64
+ $ i ++;
65
+ }
59
66
} else {
60
- self ::parseShortOption (
67
+ if ( self ::parseShortOption (
61
68
substr ($ arg , 1 ),
62
69
$ short_options ,
63
70
$ opts ,
64
- $ args
65
- );
71
+ $ nextArg
72
+ )) {
73
+ $ i ++;
74
+ }
66
75
}
67
76
}
68
77
@@ -74,7 +83,7 @@ protected static function parseShortOption($arg, $short_options, &$opts, &$args)
74
83
$ argLen = strlen ($ arg );
75
84
76
85
for ($ i = 0 ; $ i < $ argLen ; $ i ++) {
77
- $ opt = $ arg [$ i ];
86
+ $ opt = $ arg [$ i ];
78
87
$ opt_arg = null ;
79
88
80
89
if (($ spec = strstr ($ short_options , $ opt )) === false ||
@@ -107,11 +116,11 @@ protected static function parseShortOption($arg, $short_options, &$opts, &$args)
107
116
}
108
117
}
109
118
110
- protected static function parseLongOption ($ arg , $ long_options , &$ opts , & $ args )
119
+ protected static function parseLongOption ($ arg , $ long_options , &$ opts , $ nextArg )
111
120
{
112
- $ count = count ($ long_options );
113
- $ list = explode ('= ' , $ arg );
114
- $ opt = $ list [0 ];
121
+ $ count = count ($ long_options );
122
+ $ list = explode ('= ' , $ arg );
123
+ $ opt = $ list [0 ];
115
124
$ opt_arg = null ;
116
125
117
126
if (count ($ list ) > 1 ) {
@@ -121,7 +130,7 @@ protected static function parseLongOption($arg, $long_options, &$opts, &$args)
121
130
$ opt_len = strlen ($ opt );
122
131
123
132
for ($ i = 0 ; $ i < $ count ; $ i ++) {
124
- $ long_opt = $ long_options [$ i ];
133
+ $ long_opt = $ long_options [$ i ];
125
134
$ opt_start = substr ($ long_opt , 0 , $ opt_len );
126
135
127
136
if ($ opt_start != $ opt ) {
@@ -131,20 +140,24 @@ protected static function parseLongOption($arg, $long_options, &$opts, &$args)
131
140
$ opt_rest = substr ($ long_opt , $ opt_len );
132
141
133
142
if ($ opt_rest != '' && $ opt [0 ] != '= ' && $ i + 1 < $ count &&
134
- $ opt == substr ($ long_options [$ i+ 1 ], 0 , $ opt_len )) {
143
+ $ opt == substr ($ long_options [$ i + 1 ], 0 , $ opt_len )) {
135
144
throw new PHPUnit_Framework_Exception (
136
145
"option -- $ opt is ambiguous "
137
146
);
138
147
}
139
148
149
+ $ ret = false ;
150
+
140
151
if (substr ($ long_opt , -1 ) == '= ' ) {
141
152
if (substr ($ long_opt , -2 ) != '== ' ) {
142
- if (! strlen ($ opt_arg ) &&
143
- !( list (, $ opt_arg ) = each ( $ args )) ) {
153
+ if (empty ($ opt_arg ) &&
154
+ empty ( $ nextArg ) /*false*/ ) {
144
155
throw new PHPUnit_Framework_Exception (
145
156
"option -- $ opt requires an argument "
146
157
);
147
158
}
159
+
160
+ $ ret = true ;
148
161
}
149
162
} elseif ($ opt_arg ) {
150
163
throw new PHPUnit_Framework_Exception (
@@ -153,9 +166,9 @@ protected static function parseLongOption($arg, $long_options, &$opts, &$args)
153
166
}
154
167
155
168
$ full_option = '-- ' . preg_replace ('/={1,2}$/ ' , '' , $ long_opt );
156
- $ opts [] = array ($ full_option , $ opt_arg );
169
+ $ opts [] = array ($ full_option , $ opt_arg );
157
170
158
- return ;
171
+ return $ ret ;
159
172
}
160
173
161
174
throw new PHPUnit_Framework_Exception ("unrecognized option -- $ opt " );
0 commit comments