Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * PHPUnit
4
 *
5
 * Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 *   * Redistributions of source code must retain the above copyright
13
 *     notice, this list of conditions and the following disclaimer.
14
 *
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
18
 *     distribution.
19
 *
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.
23
 *
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.
36
 *
37
 * @category   Testing
38
 * @package    PHPUnit
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
44
 */
45
 
46
require_once 'PHPUnit/Util/Filter.php';
47
 
48
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
49
 
50
/**
51
 * Command-line options parsing class.
52
 *
53
 * @category   Testing
54
 * @package    PHPUnit
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.15
60
 * @link       http://www.phpunit.de/
61
 * @since      Class available since Release 3.0.0
62
 */
63
class PHPUnit_Util_Getopt
64
{
65
    public static function getopt(array $args, $short_options, $long_options = NULL)
66
    {
67
        if (empty($args)) {
68
            return array(array(), array());
69
        }
70
 
71
        $opts     = array();
72
        $non_opts = array();
73
 
74
        if ($long_options) {
75
            sort($long_options);
76
        }
77
 
78
        if (isset($args[0][0]) && $args[0][0] != '-') {
79
            array_shift($args);
80
        }
81
 
82
        reset($args);
83
        array_map('trim', $args);
84
 
85
        while (list($i, $arg) = each($args)) {
86
            if ($arg == '') {
87
                continue;
88
            }
89
 
90
            if ($arg == '--') {
91
                $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
92
                break;
93
            }
94
 
95
            if ($arg[0] != '-' ||
96
                (strlen($arg) > 1 && $arg[1] == '-' && !$long_options)) {
97
                $non_opts = array_merge($non_opts, array_slice($args, $i));
98
                break;
99
            }
100
 
101
            elseif (strlen($arg) > 1 && $arg[1] == '-') {
102
                self::parseLongOption(
103
                  substr($arg, 2), $long_options, $opts, $args
104
                );
105
            }
106
 
107
            else {
108
                self::parseShortOption(
109
                  substr($arg, 1), $short_options, $opts, $args
110
                );
111
            }
112
        }
113
 
114
        return array($opts, $non_opts);
115
    }
116
 
117
    protected static function parseShortOption($arg, $short_options, &$opts, &$args)
118
    {
119
        $argLen = strlen($arg);
120
 
121
        for ($i = 0; $i < $argLen; $i++) {
122
            $opt     = $arg[$i];
123
            $opt_arg = NULL;
124
 
125
            if (($spec = strstr($short_options, $opt)) === FALSE ||
126
                $arg[$i] == ':') {
127
                throw new PHPUnit_Framework_Exception(
128
                  "unrecognized option -- $opt"
129
                );
130
            }
131
 
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));
136
                        break;
137
                    }
138
                } else {
139
                    if ($i + 1 < $argLen) {
140
                        $opts[] = array($opt, substr($arg, $i + 1));
141
                        break;
142
                    }
143
 
144
                    else if (list(, $opt_arg) = each($args)) {
145
                    }
146
 
147
                    else {
148
                        throw new PHPUnit_Framework_Exception(
149
                          "option requires an argument -- $opt"
150
                        );
151
                    }
152
                }
153
            }
154
 
155
            $opts[] = array($opt, $opt_arg);
156
        }
157
    }
158
 
159
    protected static function parseLongOption($arg, $long_options, &$opts, &$args)
160
    {
161
        $count   = count($long_options);
162
        $list    = explode('=', $arg);
163
        $opt     = $list[0];
164
        $opt_arg = NULL;
165
 
166
        if (count($list) > 1) {
167
            $opt_arg = $list[1];
168
        }
169
 
170
        $opt_len = strlen($opt);
171
 
172
        for ($i = 0; $i < $count; $i++) {
173
            $long_opt  = $long_options[$i];
174
            $opt_start = substr($long_opt, 0, $opt_len);
175
 
176
            if ($opt_start != $opt) {
177
                continue;
178
            }
179
 
180
            $opt_rest = substr($long_opt, $opt_len);
181
 
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"
186
                );
187
            }
188
 
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"
195
                        );
196
                    }
197
                }
198
            }
199
 
200
            else if ($opt_arg) {
201
                throw new PHPUnit_Framework_Exception(
202
                  "option --$opt doesn't allow an argument"
203
                );
204
            }
205
 
206
            $opts[] = array('--' . $opt, $opt_arg);
207
            return;
208
        }
209
 
210
        throw new PHPUnit_Framework_Exception("unrecognized option --$opt");
211
    }
212
}
213
?>