Fixing content type ordering when content_type is not defined.
[akelos.git] / vendor / pear / Console / Getopt.php
blob7966d1acd9fc1dad776b29a9cd40b57ed70d697c
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4 |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2003 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 3.0 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available through the world-wide-web at the following url: |
11 // | http://www.php.net/license/3_0.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Author: Andrei Zmievski <andrei@php.net> |
17 // +----------------------------------------------------------------------+
19 // $Id: Getopt.php,v 1.21.4.7 2003/12/05 21:57:01 andrei Exp $
21 require_once 'PEAR.php';
23 /**
24 * Command-line options parsing class.
26 * @author Andrei Zmievski <andrei@php.net>
29 class Console_Getopt {
30 /**
31 * Parses the command-line options.
33 * The first parameter to this function should be the list of command-line
34 * arguments without the leading reference to the running program.
36 * The second parameter is a string of allowed short options. Each of the
37 * option letters can be followed by a colon ':' to specify that the option
38 * requires an argument, or a double colon '::' to specify that the option
39 * takes an optional argument.
41 * The third argument is an optional array of allowed long options. The
42 * leading '--' should not be included in the option name. Options that
43 * require an argument should be followed by '=', and options that take an
44 * option argument should be followed by '=='.
46 * The return value is an array of two elements: the list of parsed
47 * options and the list of non-option command-line arguments. Each entry in
48 * the list of parsed options is a pair of elements - the first one
49 * specifies the option, and the second one specifies the option argument,
50 * if there was one.
52 * Long and short options can be mixed.
54 * Most of the semantics of this function are based on GNU getopt_long().
56 * @param array $args an array of command-line arguments
57 * @param string $short_options specifies the list of allowed short options
58 * @param array $long_options specifies the list of allowed long options
60 * @return array two-element array containing the list of parsed options and
61 * the non-option arguments
63 * @access public
66 function getopt2($args, $short_options, $long_options = null)
68 return Console_Getopt::doGetopt(2, $args, $short_options, $long_options);
71 /**
72 * This function expects $args to start with the script name (POSIX-style).
73 * Preserved for backwards compatibility.
74 * @see getopt2()
75 */
76 function getopt($args, $short_options, $long_options = null)
78 return Console_Getopt::doGetopt(1, $args, $short_options, $long_options);
81 /**
82 * The actual implementation of the argument parsing code.
84 function doGetopt($version, $args, $short_options, $long_options = null)
86 // in case you pass directly readPHPArgv() as the first arg
87 if (PEAR::isError($args)) {
88 return $args;
90 if (empty($args)) {
91 return array(array(), array());
93 $opts = array();
94 $non_opts = array();
96 settype($args, 'array');
98 if ($long_options) {
99 sort($long_options);
103 * Preserve backwards compatibility with callers that relied on
104 * erroneous POSIX fix.
106 if ($version < 2) {
107 if (isset($args[0]{0}) && $args[0]{0} != '-') {
108 array_shift($args);
112 reset($args);
113 while (list($i, $arg) = each($args)) {
115 /* The special element '--' means explicit end of
116 options. Treat the rest of the arguments as non-options
117 and end the loop. */
118 if ($arg == '--') {
119 $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
120 break;
123 if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && !$long_options)) {
124 $non_opts = array_merge($non_opts, array_slice($args, $i));
125 break;
126 } elseif (strlen($arg) > 1 && $arg{1} == '-') {
127 $error = Console_Getopt::_parseLongOption(substr($arg, 2), $long_options, $opts, $args);
128 if (PEAR::isError($error))
129 return $error;
130 } else {
131 $error = Console_Getopt::_parseShortOption(substr($arg, 1), $short_options, $opts, $args);
132 if (PEAR::isError($error))
133 return $error;
137 return array($opts, $non_opts);
141 * @access private
144 function _parseShortOption($arg, $short_options, &$opts, &$args)
146 for ($i = 0; $i < strlen($arg); $i++) {
147 $opt = $arg{$i};
148 $opt_arg = null;
150 /* Try to find the short option in the specifier string. */
151 if (($spec = strstr($short_options, $opt)) === false || $arg{$i} == ':')
153 return PEAR::raiseError("Console_Getopt: unrecognized option -- $opt");
156 if (strlen($spec) > 1 && $spec{1} == ':') {
157 if (strlen($spec) > 2 && $spec{2} == ':') {
158 if ($i + 1 < strlen($arg)) {
159 /* Option takes an optional argument. Use the remainder of
160 the arg string if there is anything left. */
161 $opts[] = array($opt, substr($arg, $i + 1));
162 break;
164 } else {
165 /* Option requires an argument. Use the remainder of the arg
166 string if there is anything left. */
167 if ($i + 1 < strlen($arg)) {
168 $opts[] = array($opt, substr($arg, $i + 1));
169 break;
170 } else if (list(, $opt_arg) = each($args))
171 /* Else use the next argument. */;
172 else
173 return PEAR::raiseError("Console_Getopt: option requires an argument -- $opt");
177 $opts[] = array($opt, $opt_arg);
182 * @access private
185 function _parseLongOption($arg, $long_options, &$opts, &$args)
187 @list($opt, $opt_arg) = explode('=', $arg);
188 $opt_len = strlen($opt);
190 for ($i = 0; $i < count($long_options); $i++) {
191 $long_opt = $long_options[$i];
192 $opt_start = substr($long_opt, 0, $opt_len);
194 /* Option doesn't match. Go on to the next one. */
195 if ($opt_start != $opt)
196 continue;
198 $opt_rest = substr($long_opt, $opt_len);
200 /* Check that the options uniquely matches one of the allowed
201 options. */
202 if ($opt_rest != '' && $opt{0} != '=' &&
203 $i + 1 < count($long_options) &&
204 $opt == substr($long_options[$i+1], 0, $opt_len)) {
205 return PEAR::raiseError("Console_Getopt: option --$opt is ambiguous");
208 if (substr($long_opt, -1) == '=') {
209 if (substr($long_opt, -2) != '==') {
210 /* Long option requires an argument.
211 Take the next argument if one wasn't specified. */;
212 if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args))) {
213 return PEAR::raiseError("Console_Getopt: option --$opt requires an argument");
216 } else if ($opt_arg) {
217 return PEAR::raiseError("Console_Getopt: option --$opt doesn't allow an argument");
220 $opts[] = array('--' . $opt, $opt_arg);
221 return;
224 return PEAR::raiseError("Console_Getopt: unrecognized option --$opt");
228 * Safely read the $argv PHP array across different PHP configurations.
229 * Will take care on register_globals and register_argc_argv ini directives
231 * @access public
232 * @return mixed the $argv PHP array or PEAR error if not registered
234 function readPHPArgv()
236 global $argv;
237 if (!is_array($argv)) {
238 if (!@is_array($_SERVER['argv'])) {
239 if (!@is_array($GLOBALS['HTTP_SERVER_VARS']['argv'])) {
240 return PEAR::raiseError("Console_Getopt: Could not read cmd args (register_argc_argv=Off?)");
242 return $GLOBALS['HTTP_SERVER_VARS']['argv'];
244 return $_SERVER['argv'];
246 return $argv;