5
* Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
12
* * Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
15
* * Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in
17
* the documentation and/or other materials provided with the
20
* * Neither the name of Sebastian Bergmann nor the names of his
21
* contributors may be used to endorse or promote products derived
22
* from this software without specific prior written permission.
24
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
* POSSIBILITY OF SUCH DAMAGE.
39
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
40
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
41
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
42
* @link http://www.phpunit.de/
43
* @since File available since Release 3.0.0
46
require_once 'PHPUnit/Util/Filter.php';
48
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
51
* Command-line options parsing class.
55
* @author Andrei Zmievski <andrei@php.net>
56
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
57
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
58
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
59
* @version Release: 3.4.14
60
* @link http://www.phpunit.de/
61
* @since Class available since Release 3.0.0
63
class PHPUnit_Util_Getopt
65
public static function getopt(array $args, $short_options, $long_options = NULL)
68
return array(array(), array());
78
if (isset($args[0][0]) && $args[0][0] != '-') {
83
array_map('trim', $args);
85
while (list($i, $arg) = each($args)) {
91
$non_opts = array_merge($non_opts, array_slice($args, $i + 1));
96
(strlen($arg) > 1 && $arg[1] == '-' && !$long_options)) {
97
$non_opts = array_merge($non_opts, array_slice($args, $i));
101
elseif (strlen($arg) > 1 && $arg[1] == '-') {
102
self::parseLongOption(
103
substr($arg, 2), $long_options, $opts, $args
108
self::parseShortOption(
109
substr($arg, 1), $short_options, $opts, $args
114
return array($opts, $non_opts);
117
protected static function parseShortOption($arg, $short_options, &$opts, &$args)
119
$argLen = strlen($arg);
121
for ($i = 0; $i < $argLen; $i++) {
125
if (($spec = strstr($short_options, $opt)) === FALSE ||
127
throw new PHPUnit_Framework_Exception(
128
"unrecognized option -- $opt"
132
if (strlen($spec) > 1 && $spec[1] == ':') {
133
if (strlen($spec) > 2 && $spec[2] == ':') {
134
if ($i + 1 < $argLen) {
135
$opts[] = array($opt, substr($arg, $i + 1));
139
if ($i + 1 < $argLen) {
140
$opts[] = array($opt, substr($arg, $i + 1));
144
else if (list(, $opt_arg) = each($args)) {
148
throw new PHPUnit_Framework_Exception(
149
"option requires an argument -- $opt"
155
$opts[] = array($opt, $opt_arg);
159
protected static function parseLongOption($arg, $long_options, &$opts, &$args)
161
$count = count($long_options);
162
$list = explode('=', $arg);
166
if (count($list) > 1) {
170
$opt_len = strlen($opt);
172
for ($i = 0; $i < $count; $i++) {
173
$long_opt = $long_options[$i];
174
$opt_start = substr($long_opt, 0, $opt_len);
176
if ($opt_start != $opt) {
180
$opt_rest = substr($long_opt, $opt_len);
182
if ($opt_rest != '' && $opt[0] != '=' && $i + 1 < $count &&
183
$opt == substr($long_options[$i+1], 0, $opt_len)) {
184
throw new PHPUnit_Framework_Exception(
185
"option --$opt is ambiguous"
189
if (substr($long_opt, -1) == '=') {
190
if (substr($long_opt, -2) != '==') {
191
if (!strlen($opt_arg) &&
192
!(list(, $opt_arg) = each($args))) {
193
throw new PHPUnit_Framework_Exception(
194
"option --$opt requires an argument"
201
throw new PHPUnit_Framework_Exception(
202
"option --$opt doesn't allow an argument"
206
$opts[] = array('--' . $opt, $opt_arg);
210
throw new PHPUnit_Framework_Exception("unrecognized option --$opt");
b'\\ No newline at end of file'