]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHPUnit/Util/Getopt.php
Release 6.2.0
[Github/sugarcrm.git] / tests / PHPUnit / PHPUnit / Util / Getopt.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.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  * @package    PHPUnit
38  * @subpackage Util
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
44  */
45
46 /**
47  * Command-line options parsing class.
48  *
49  * @package    PHPUnit
50  * @subpackage Util
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
58  */
59 class PHPUnit_Util_Getopt
60 {
61     public static function getopt(array $args, $short_options, $long_options = NULL)
62     {
63         if (empty($args)) {
64             return array(array(), array());
65         }
66
67         $opts     = array();
68         $non_opts = array();
69
70         if ($long_options) {
71             sort($long_options);
72         }
73
74         if (isset($args[0][0]) && $args[0][0] != '-') {
75             array_shift($args);
76         }
77
78         reset($args);
79         array_map('trim', $args);
80
81         while (list($i, $arg) = each($args)) {
82             if ($arg == '') {
83                 continue;
84             }
85
86             if ($arg == '--') {
87                 $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
88                 break;
89             }
90
91             if ($arg[0] != '-' ||
92                 (strlen($arg) > 1 && $arg[1] == '-' && !$long_options)) {
93                 $non_opts = array_merge($non_opts, array_slice($args, $i));
94                 break;
95             }
96
97             elseif (strlen($arg) > 1 && $arg[1] == '-') {
98                 self::parseLongOption(
99                   substr($arg, 2), $long_options, $opts, $args
100                 );
101             }
102
103             else {
104                 self::parseShortOption(
105                   substr($arg, 1), $short_options, $opts, $args
106                 );
107             }
108         }
109
110         return array($opts, $non_opts);
111     }
112
113     protected static function parseShortOption($arg, $short_options, &$opts, &$args)
114     {
115         $argLen = strlen($arg);
116
117         for ($i = 0; $i < $argLen; $i++) {
118             $opt     = $arg[$i];
119             $opt_arg = NULL;
120
121             if (($spec = strstr($short_options, $opt)) === FALSE ||
122                 $arg[$i] == ':') {
123                 throw new PHPUnit_Framework_Exception(
124                   "unrecognized option -- $opt"
125                 );
126             }
127
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));
132                         break;
133                     }
134                 } else {
135                     if ($i + 1 < $argLen) {
136                         $opts[] = array($opt, substr($arg, $i + 1));
137                         break;
138                     }
139
140                     else if (list(, $opt_arg) = each($args)) {
141                     }
142
143                     else {
144                         throw new PHPUnit_Framework_Exception(
145                           "option requires an argument -- $opt"
146                         );
147                     }
148                 }
149             }
150
151             $opts[] = array($opt, $opt_arg);
152         }
153     }
154
155     protected static function parseLongOption($arg, $long_options, &$opts, &$args)
156     {
157         $count   = count($long_options);
158         $list    = explode('=', $arg);
159         $opt     = $list[0];
160         $opt_arg = NULL;
161
162         if (count($list) > 1) {
163             $opt_arg = $list[1];
164         }
165
166         $opt_len = strlen($opt);
167
168         for ($i = 0; $i < $count; $i++) {
169             $long_opt  = $long_options[$i];
170             $opt_start = substr($long_opt, 0, $opt_len);
171
172             if ($opt_start != $opt) {
173                 continue;
174             }
175
176             $opt_rest = substr($long_opt, $opt_len);
177
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"
182                 );
183             }
184
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"
191                         );
192                     }
193                 }
194             }
195
196             else if ($opt_arg) {
197                 throw new PHPUnit_Framework_Exception(
198                   "option --$opt doesn't allow an argument"
199                 );
200             }
201
202             $opts[] = array('--' . $opt, $opt_arg);
203             return;
204         }
205
206         throw new PHPUnit_Framework_Exception("unrecognized option --$opt");
207     }
208 }