~ballot/wordpress/openstack-objectstorage-breaking-insight

« back to all changes in this revision

Viewing changes to vendor/phpunit/phpunit/src/Util/Getopt.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 * This file is part of PHPUnit.
 
4
 *
 
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
 
6
 *
 
7
 * For the full copyright and license information, please view the LICENSE
 
8
 * file that was distributed with this source code.
 
9
 */
 
10
 
 
11
/**
 
12
 * Command-line options parsing class.
 
13
 *
 
14
 * @package    PHPUnit
 
15
 * @subpackage Util
 
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
 
22
 */
 
23
class PHPUnit_Util_Getopt
 
24
{
 
25
    public static function getopt(array $args, $short_options, $long_options = null)
 
26
    {
 
27
        if (empty($args)) {
 
28
            return array(array(), array());
 
29
        }
 
30
 
 
31
        $opts     = array();
 
32
        $non_opts = array();
 
33
 
 
34
        if ($long_options) {
 
35
            sort($long_options);
 
36
        }
 
37
 
 
38
        if (isset($args[0][0]) && $args[0][0] != '-') {
 
39
            array_shift($args);
 
40
        }
 
41
 
 
42
        reset($args);
 
43
        array_map('trim', $args);
 
44
 
 
45
        while (list($i, $arg) = each($args)) {
 
46
            if ($arg == '') {
 
47
                continue;
 
48
            }
 
49
 
 
50
            if ($arg == '--') {
 
51
                $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
 
52
                break;
 
53
            }
 
54
 
 
55
            if ($arg[0] != '-' ||
 
56
                (strlen($arg) > 1 && $arg[1] == '-' && !$long_options)) {
 
57
                $non_opts = array_merge($non_opts, array_slice($args, $i));
 
58
                break;
 
59
            } elseif (strlen($arg) > 1 && $arg[1] == '-') {
 
60
                self::parseLongOption(
 
61
                    substr($arg, 2), $long_options, $opts, $args
 
62
                );
 
63
            } else {
 
64
                self::parseShortOption(
 
65
                    substr($arg, 1), $short_options, $opts, $args
 
66
                );
 
67
            }
 
68
        }
 
69
 
 
70
        return array($opts, $non_opts);
 
71
    }
 
72
 
 
73
    protected static function parseShortOption($arg, $short_options, &$opts, &$args)
 
74
    {
 
75
        $argLen = strlen($arg);
 
76
 
 
77
        for ($i = 0; $i < $argLen; $i++) {
 
78
            $opt     = $arg[$i];
 
79
            $opt_arg = null;
 
80
 
 
81
            if (($spec = strstr($short_options, $opt)) === false ||
 
82
                $arg[$i] == ':') {
 
83
                throw new PHPUnit_Framework_Exception(
 
84
                    "unrecognized option -- $opt"
 
85
                );
 
86
            }
 
87
 
 
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));
 
92
                        break;
 
93
                    }
 
94
                } else {
 
95
                    if ($i + 1 < $argLen) {
 
96
                        $opts[] = array($opt, substr($arg, $i + 1));
 
97
                        break;
 
98
                    } elseif (list(, $opt_arg) = each($args)) {
 
99
                    } else {
 
100
                        throw new PHPUnit_Framework_Exception(
 
101
                            "option requires an argument -- $opt"
 
102
                        );
 
103
                    }
 
104
                }
 
105
            }
 
106
 
 
107
            $opts[] = array($opt, $opt_arg);
 
108
        }
 
109
    }
 
110
 
 
111
    protected static function parseLongOption($arg, $long_options, &$opts, &$args)
 
112
    {
 
113
        $count   = count($long_options);
 
114
        $list    = explode('=', $arg);
 
115
        $opt     = $list[0];
 
116
        $opt_arg = null;
 
117
 
 
118
        if (count($list) > 1) {
 
119
            $opt_arg = $list[1];
 
120
        }
 
121
 
 
122
        $opt_len = strlen($opt);
 
123
 
 
124
        for ($i = 0; $i < $count; $i++) {
 
125
            $long_opt  = $long_options[$i];
 
126
            $opt_start = substr($long_opt, 0, $opt_len);
 
127
 
 
128
            if ($opt_start != $opt) {
 
129
                continue;
 
130
            }
 
131
 
 
132
            $opt_rest = substr($long_opt, $opt_len);
 
133
 
 
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"
 
138
                );
 
139
            }
 
140
 
 
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"
 
147
                        );
 
148
                    }
 
149
                }
 
150
            } elseif ($opt_arg) {
 
151
                throw new PHPUnit_Framework_Exception(
 
152
                    "option --$opt doesn't allow an argument"
 
153
                );
 
154
            }
 
155
 
 
156
            $full_option = '--' . preg_replace('/={1,2}$/', '', $long_opt);
 
157
            $opts[] = array($full_option, $opt_arg);
 
158
 
 
159
            return;
 
160
        }
 
161
 
 
162
        throw new PHPUnit_Framework_Exception("unrecognized option --$opt");
 
163
    }
 
164
}