3
* This file is part of PHPUnit.
5
* (c) Sebastian Bergmann <sebastian@phpunit.de>
7
* For the full copyright and license information, please view the LICENSE
8
* file that was distributed with this source code.
12
* Command-line options parsing class.
16
* @author Andrei Zmievski <andrei@php.net>
17
* @author Sebastian Bergmann <sebastian@phpunit.de>
18
* @copyright Sebastian Bergmann <sebastian@phpunit.de>
19
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
20
* @link http://www.phpunit.de/
21
* @since Class available since Release 3.0.0
23
class PHPUnit_Util_Getopt
25
public static function getopt(array $args, $short_options, $long_options = null)
28
return array(array(), array());
38
if (isset($args[0][0]) && $args[0][0] != '-') {
43
array_map('trim', $args);
45
while (list($i, $arg) = each($args)) {
51
$non_opts = array_merge($non_opts, array_slice($args, $i + 1));
56
(strlen($arg) > 1 && $arg[1] == '-' && !$long_options)) {
57
$non_opts = array_merge($non_opts, array_slice($args, $i));
59
} elseif (strlen($arg) > 1 && $arg[1] == '-') {
60
self::parseLongOption(
61
substr($arg, 2), $long_options, $opts, $args
64
self::parseShortOption(
65
substr($arg, 1), $short_options, $opts, $args
70
return array($opts, $non_opts);
73
protected static function parseShortOption($arg, $short_options, &$opts, &$args)
75
$argLen = strlen($arg);
77
for ($i = 0; $i < $argLen; $i++) {
81
if (($spec = strstr($short_options, $opt)) === false ||
83
throw new PHPUnit_Framework_Exception(
84
"unrecognized option -- $opt"
88
if (strlen($spec) > 1 && $spec[1] == ':') {
89
if (strlen($spec) > 2 && $spec[2] == ':') {
90
if ($i + 1 < $argLen) {
91
$opts[] = array($opt, substr($arg, $i + 1));
95
if ($i + 1 < $argLen) {
96
$opts[] = array($opt, substr($arg, $i + 1));
98
} elseif (list(, $opt_arg) = each($args)) {
100
throw new PHPUnit_Framework_Exception(
101
"option requires an argument -- $opt"
107
$opts[] = array($opt, $opt_arg);
111
protected static function parseLongOption($arg, $long_options, &$opts, &$args)
113
$count = count($long_options);
114
$list = explode('=', $arg);
118
if (count($list) > 1) {
122
$opt_len = strlen($opt);
124
for ($i = 0; $i < $count; $i++) {
125
$long_opt = $long_options[$i];
126
$opt_start = substr($long_opt, 0, $opt_len);
128
if ($opt_start != $opt) {
132
$opt_rest = substr($long_opt, $opt_len);
134
if ($opt_rest != '' && $opt[0] != '=' && $i + 1 < $count &&
135
$opt == substr($long_options[$i+1], 0, $opt_len)) {
136
throw new PHPUnit_Framework_Exception(
137
"option --$opt is ambiguous"
141
if (substr($long_opt, -1) == '=') {
142
if (substr($long_opt, -2) != '==') {
143
if (!strlen($opt_arg) &&
144
!(list(, $opt_arg) = each($args))) {
145
throw new PHPUnit_Framework_Exception(
146
"option --$opt requires an argument"
150
} elseif ($opt_arg) {
151
throw new PHPUnit_Framework_Exception(
152
"option --$opt doesn't allow an argument"
156
$full_option = '--' . preg_replace('/={1,2}$/', '', $long_opt);
157
$opts[] = array($full_option, $opt_arg);
162
throw new PHPUnit_Framework_Exception("unrecognized option --$opt");