5 * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.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 <sebastian@phpunit.de>
40 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.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
47 * Command-line options parsing class.
51 * @author Andrei Zmievski <andrei@php.net>
52 * @author Sebastian Bergmann <sebastian@phpunit.de>
53 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
54 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
55 * @version Release: 3.5.13
56 * @link http://www.phpunit.de/
57 * @since Class available since Release 3.0.0
59 class PHPUnit_Util_Getopt
61 public static function getopt(array $args, $short_options, $long_options = NULL)
64 return array(array(), array());
74 if (isset($args[0][0]) && $args[0][0] != '-') {
79 array_map('trim', $args);
81 while (list($i, $arg) = each($args)) {
87 $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
92 (strlen($arg) > 1 && $arg[1] == '-' && !$long_options)) {
93 $non_opts = array_merge($non_opts, array_slice($args, $i));
97 elseif (strlen($arg) > 1 && $arg[1] == '-') {
98 self::parseLongOption(
99 substr($arg, 2), $long_options, $opts, $args
104 self::parseShortOption(
105 substr($arg, 1), $short_options, $opts, $args
110 return array($opts, $non_opts);
113 protected static function parseShortOption($arg, $short_options, &$opts, &$args)
115 $argLen = strlen($arg);
117 for ($i = 0; $i < $argLen; $i++) {
121 if (($spec = strstr($short_options, $opt)) === FALSE ||
123 throw new PHPUnit_Framework_Exception(
124 "unrecognized option -- $opt"
128 if (strlen($spec) > 1 && $spec[1] == ':') {
129 if (strlen($spec) > 2 && $spec[2] == ':') {
130 if ($i + 1 < $argLen) {
131 $opts[] = array($opt, substr($arg, $i + 1));
135 if ($i + 1 < $argLen) {
136 $opts[] = array($opt, substr($arg, $i + 1));
140 else if (list(, $opt_arg) = each($args)) {
144 throw new PHPUnit_Framework_Exception(
145 "option requires an argument -- $opt"
151 $opts[] = array($opt, $opt_arg);
155 protected static function parseLongOption($arg, $long_options, &$opts, &$args)
157 $count = count($long_options);
158 $list = explode('=', $arg);
162 if (count($list) > 1) {
166 $opt_len = strlen($opt);
168 for ($i = 0; $i < $count; $i++) {
169 $long_opt = $long_options[$i];
170 $opt_start = substr($long_opt, 0, $opt_len);
172 if ($opt_start != $opt) {
176 $opt_rest = substr($long_opt, $opt_len);
178 if ($opt_rest != '' && $opt[0] != '=' && $i + 1 < $count &&
179 $opt == substr($long_options[$i+1], 0, $opt_len)) {
180 throw new PHPUnit_Framework_Exception(
181 "option --$opt is ambiguous"
185 if (substr($long_opt, -1) == '=') {
186 if (substr($long_opt, -2) != '==') {
187 if (!strlen($opt_arg) &&
188 !(list(, $opt_arg) = each($args))) {
189 throw new PHPUnit_Framework_Exception(
190 "option --$opt requires an argument"
197 throw new PHPUnit_Framework_Exception(
198 "option --$opt doesn't allow an argument"
202 $opts[] = array('--' . $opt, $opt_arg);
206 throw new PHPUnit_Framework_Exception("unrecognized option --$opt");