3
* Zend_Console_Getopt is a class to parse options for command-line
8
* This source file is subject to the new BSD license that is bundled
9
* with this package in the file LICENSE.txt.
10
* It is also available through the world-wide-web at this URL:
11
* http://framework.zend.com/license/new-bsd
12
* If you did not receive a copy of the license and are unable to
13
* obtain it through the world-wide-web, please send an email
14
* to license@zend.com so we can send you a copy immediately.
17
* @package Zend_Console_Getopt
18
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
24
* @see Zend_Console_Getopt_Exception
26
require_once 'Zend/Console/Getopt/Exception.php';
29
* Zend_Console_Getopt is a class to parse options for command-line
33
* Argument: an element of the argv array. This may be part of an option,
34
* or it may be a non-option command-line argument.
35
* Flag: the letter or word set off by a '-' or '--'. Example: in '--output filename',
36
* '--output' is the flag.
37
* Parameter: the additional argument that is associated with the option.
38
* Example: in '--output filename', the 'filename' is the parameter.
39
* Option: the combination of a flag and its parameter, if any.
40
* Example: in '--output filename', the whole thing is the option.
42
* The following features are supported:
44
* - Short flags like '-a'. Short flags are preceded by a single
45
* dash. Short flags may be clustered e.g. '-abc', which is the
46
* same as '-a' '-b' '-c'.
47
* - Long flags like '--verbose'. Long flags are preceded by a
48
* double dash. Long flags may not be clustered.
49
* - Options may have a parameter, e.g. '--output filename'.
50
* - Parameters for long flags may also be set off with an equals sign,
51
* e.g. '--output=filename'.
52
* - Parameters for long flags may be checked as string, word, or integer.
53
* - Automatic generation of a helpful usage message.
54
* - Signal end of options with '--'; subsequent arguments are treated
55
* as non-option arguments, even if they begin with '-'.
56
* - Raise exception Zend_Console_Getopt_Exception in several cases
57
* when invalid flags or parameters are given. Usage message is
58
* returned in the exception object.
60
* The format for specifying options uses a PHP associative array.
61
* The key is has the format of a list of pipe-separated flag names,
62
* followed by an optional '=' to indicate a required parameter or
63
* '-' to indicate an optional parameter. Following that, the type
64
* of parameter may be specified as 's' for string, 'w' for word,
68
* - 'user|username|u=s' this means '--user' or '--username' or '-u'
69
* are synonyms, and the option requires a string parameter.
70
* - 'p=i' this means '-p' requires an integer parameter. No synonyms.
71
* - 'verbose|v-i' this means '--verbose' or '-v' are synonyms, and
72
* they take an optional integer parameter.
73
* - 'help|h' this means '--help' or '-h' are synonyms, and
74
* they take no parameter.
76
* The values in the associative array are strings that are used as
77
* brief descriptions of the options when printing a usage message.
79
* The simpler format for specifying options used by PHP's getopt()
80
* function is also supported. This is similar to GNU getopt and shell
83
* Example: 'abc:' means options '-a', '-b', and '-c'
84
* are legal, and the latter requires a string parameter.
87
* @package Zend_Console_Getopt
88
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
89
* @license http://framework.zend.com/license/new-bsd New BSD License
90
* @version Release: @package_version@
91
* @since Class available since Release 0.6.0
93
* @todo Handle params with multiple values, e.g. --colors=red,green,blue
94
* Set value of parameter to the array of values. Allow user to specify
95
* the separator with Zend_Console_Getopt::CONFIG_PARAMETER_SEPARATOR.
96
* If this config value is null or empty string, do not split values
97
* into arrays. Default separator is comma (',').
99
* @todo Handle params with multiple values specified with separate options
100
* e.g. --colors red --colors green --colors blue should give one
101
* option with an array(red, green, blue).
102
* Enable with Zend_Console_Getopt::CONFIG_CUMULATIVE_PARAMETERS.
103
* Default is that subsequent options overwrite the parameter value.
105
* @todo Handle flags occurring multiple times, e.g. -v -v -v
106
* Set value of the option's parameter to the integer count of instances
107
* instead of a boolean.
108
* Enable with Zend_Console_Getopt::CONFIG_CUMULATIVE_FLAGS.
109
* Default is that the value is simply boolean true regardless of
110
* how many instances of the flag appear.
112
* @todo Handle flags that implicitly print usage message, e.g. --help
114
* @todo Handle freeform options, e.g. --set-variable
115
* Enable with Zend_Console_Getopt::CONFIG_FREEFORM_FLAGS
116
* All flag-like syntax is recognized, no flag generates an exception.
118
* @todo Handle numeric options, e.g. -1, -2, -3, -1000
119
* Enable with Zend_Console_Getopt::CONFIG_NUMERIC_FLAGS
120
* The rule must specify a named flag and the '#' symbol as the
121
* parameter type. e.g., 'lines=#'
123
* @todo Enable user to specify header and footer content in the help message.
125
* @todo Feature request to handle option interdependencies.
126
* e.g. if -b is specified, -a must be specified or else the
129
* @todo Feature request to implement callbacks.
130
* e.g. if -a is specified, run function 'handleOptionA'().
132
class Zend_Console_Getopt
135
* The options for a given application can be in multiple formats.
136
* modeGnu is for traditional 'ab:c:' style getopt format.
137
* modeZend is for a more structured format.
139
const MODE_ZEND = 'zend';
140
const MODE_GNU = 'gnu';
143
* Constant tokens for various symbols used in the mode_zend
146
const PARAM_REQUIRED = '=';
147
const PARAM_OPTIONAL = '-';
148
const TYPE_STRING = 's';
149
const TYPE_WORD = 'w';
150
const TYPE_INTEGER = 'i';
153
* These are constants for optional behavior of this class.
154
* ruleMode is either 'zend' or 'gnu' or a user-defined mode.
155
* dashDash is true if '--' signifies the end of command-line options.
156
* ignoreCase is true if '--opt' and '--OPT' are implicitly synonyms.
158
const CONFIG_RULEMODE = 'ruleMode';
159
const CONFIG_DASHDASH = 'dashDash';
160
const CONFIG_IGNORECASE = 'ignoreCase';
163
* Defaults for getopt configuration are:
164
* ruleMode is 'zend' format,
165
* dashDash (--) token is enabled,
166
* ignoreCase is not enabled.
170
protected $_getoptConfig = array(
171
self::CONFIG_RULEMODE => self::MODE_ZEND,
172
self::CONFIG_DASHDASH => true,
173
self::CONFIG_IGNORECASE => false
177
* Stores the command-line arguments for the calling applicaion.
181
protected $_argv = array();
184
* Stores the name of the calling applicaion.
188
protected $_progname = '';
191
* Stores the list of legal options for this application.
195
protected $_rules = array();
198
* Stores alternate spellings of legal options.
202
protected $_ruleMap = array();
205
* Stores options given by the user in the current invocation
206
* of the application, as well as parameters given in options.
210
protected $_options = array();
213
* Stores the command-line arguments other than options.
217
protected $_remainingArgs = array();
220
* State of the options: parsed or not yet parsed?
224
protected $_parsed = false;
227
* The constructor takes one to three parameters.
229
* The first parameter is $rules, which may be a string for
230
* gnu-style format, or a structured array for Zend-style format.
232
* The second parameter is $argv, and it is optional. If not
233
* specified, $argv is inferred from the global argv.
235
* The third parameter is an array of configuration parameters
236
* to control the behavior of this instance of Getopt; it is optional.
238
* @param array $rules
240
* @param array $getoptConfig
243
public function __construct($rules, $argv = null, $getoptConfig = array())
245
$this->_progname = $_SERVER['argv'][0];
246
$this->setOptions($getoptConfig);
247
$this->addRules($rules);
248
if (!is_array($argv)) {
249
$argv = array_slice($_SERVER['argv'], 1);
252
$this->addArguments((array)$argv);
257
* Return the state of the option seen on the command line of the
258
* current application invocation. This function returns true, or the
259
* parameter to the option, if any. If the option was not given,
260
* this function returns null.
262
* The magic __get method works in the context of naming the option
263
* as a virtual member of this class.
268
public function __get($key)
270
return $this->getOption($key);
274
* Test whether a given option has been seen.
279
public function __isset($key)
282
if (isset($this->_ruleMap[$key])) {
283
$key = $this->_ruleMap[$key];
284
return isset($this->_options[$key]);
290
* Set the value for a given option.
293
* @param string $value
296
public function __set($key, $value)
299
if (isset($this->_ruleMap[$key])) {
300
$key = $this->_ruleMap[$key];
301
$this->_options[$key] = $value;
306
* Return the current set of options and parameters seen as a string.
310
public function __toString()
312
return $this->toString();
321
public function __unset($key)
324
if (isset($this->_ruleMap[$key])) {
325
$key = $this->_ruleMap[$key];
326
unset($this->_options[$key]);
331
* Define additional command-line arguments.
332
* These are appended to those defined when the constructor was called.
335
* @return Zend_Console_Getopt Provides a fluent interface
337
public function addArguments($argv)
339
$this->_argv = array_merge($this->_argv, $argv);
340
$this->_parsed = false;
345
* Define full set of command-line arguments.
346
* These replace any currently defined.
349
* @return Zend_Console_Getopt Provides a fluent interface
351
public function setArguments($argv)
353
$this->_argv = $argv;
354
$this->_parsed = false;
359
* Define multiple configuration options from an associative array.
360
* These are not program options, but properties to configure
361
* the behavior of Zend_Console_Getopt.
363
* @param array $getoptConfig
364
* @return Zend_Console_Getopt Provides a fluent interface
366
public function setOptions($getoptConfig)
368
if (isset($getoptConfig)) {
369
foreach ($getoptConfig as $key => $value) {
370
$this->setOption($key, $value);
377
* Define one configuration option as a key/value pair.
378
* These are not program options, but properties to configure
379
* the behavior of Zend_Console_Getopt.
381
* @param string $configKey
382
* @param string $configValue
383
* @return Zend_Console_Getopt Provides a fluent interface
385
public function setOption($configKey, $configValue)
387
if ($configKey !== null) {
388
$this->_getoptConfig[$configKey] = $configValue;
394
* Define additional option rules.
395
* These are appended to the rules defined when the constructor was called.
397
* @param array $rules
398
* @return Zend_Console_Getopt Provides a fluent interface
400
public function addRules($rules)
402
$ruleMode = $this->_getoptConfig['ruleMode'];
403
switch ($this->_getoptConfig['ruleMode']) {
404
case self::MODE_ZEND:
405
if (is_array($rules)) {
406
$this->_addRulesModeZend($rules);
409
// intentional fallthrough
411
$this->_addRulesModeGnu($rules);
415
* Call addRulesModeFoo() for ruleMode 'foo'.
416
* The developer should subclass Getopt and
417
* provide this method.
419
$method = '_addRulesMode' . ucfirst($ruleMode);
420
$this->$method($rules);
422
$this->_parsed = false;
427
* Return the current set of options and parameters seen as a string.
431
public function toString()
435
foreach ($this->_options as $flag => $value) {
436
$s[] = $flag . '=' . ($value === true ? 'true' : $value);
438
return implode(' ', $s);
442
* Return the current set of options and parameters seen
443
* as an array of canonical options and parameters.
445
* Clusters have been expanded, and option aliases
446
* have been mapped to their primary option names.
450
public function toArray()
454
foreach ($this->_options as $flag => $value) {
456
if ($value !== true) {
464
* Return the current set of options and parameters seen in Json format.
468
public function toJson()
472
foreach ($this->_options as $flag => $value) {
473
$j['options'][] = array(
476
'parameter' => $value
484
require_once 'Zend/Json.php';
485
$json = Zend_Json::encode($j);
491
* Return the current set of options and parameters seen in XML format.
495
public function toXml()
498
$doc = new DomDocument('1.0', 'utf-8');
499
$optionsNode = $doc->createElement('options');
500
$doc->appendChild($optionsNode);
501
foreach ($this->_options as $flag => $value) {
502
$optionNode = $doc->createElement('option');
503
$optionNode->setAttribute('flag', utf8_encode($flag));
504
if ($value !== true) {
505
$optionNode->setAttribute('parameter', utf8_encode($value));
507
$optionsNode->appendChild($optionNode);
509
$xml = $doc->saveXML();
514
* Return a list of options that have been seen in the current argv.
518
public function getOptions()
521
return array_keys($this->_options);
525
* Return the state of the option seen on the command line of the
526
* current application invocation.
528
* This function returns true, or the parameter value to the option, if any.
529
* If the option was not given, this function returns false.
531
* @param string $flag
534
public function getOption($flag)
537
if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {
538
$flag = strtolower($flag);
540
if (isset($this->_ruleMap[$flag])) {
541
$flag = $this->_ruleMap[$flag];
542
if (isset($this->_options[$flag])) {
543
return $this->_options[$flag];
550
* Return the arguments from the command-line following all options found.
554
public function getRemainingArgs()
557
return $this->_remainingArgs;
561
* Return a useful option reference, formatted for display in an
564
* Note that this usage information is provided in most Exceptions
565
* generated by this class.
569
public function getUsageMessage()
571
$usage = "Usage: {$this->_progname} [ options ]\n";
573
foreach ($this->_rules as $rule) {
575
if (is_array($rule['alias'])) {
576
foreach ($rule['alias'] as $flag) {
577
$flags[] = (strlen($flag) == 1 ? '-' : '--') . $flag;
580
$linepart['name'] = implode('|', $flags);
581
if (isset($rule['param']) && $rule['param'] != 'none') {
582
$linepart['name'] .= ' ';
583
switch ($rule['param']) {
585
$linepart['name'] .= "[ <{$rule['paramType']}> ]";
588
$linepart['name'] .= "<{$rule['paramType']}>";
592
if (strlen($linepart['name']) > $maxLen) {
593
$maxLen = strlen($linepart['name']);
595
$linepart['help'] = '';
596
if (isset($rule['help'])) {
597
$linepart['help'] .= $rule['help'];
599
$lines[] = $linepart;
601
foreach ($lines as $linepart) {
602
$usage .= sprintf("%s %s\n",
603
str_pad($linepart['name'], $maxLen),
610
* Define aliases for options.
612
* The parameter $aliasMap is an associative array
613
* mapping option name (short or long) to an alias.
615
* @param array $aliasMap
616
* @throws Zend_Console_Getopt_Exception
617
* @return Zend_Console_Getopt Provides a fluent interface
619
public function setAliases($aliasMap)
621
foreach ($aliasMap as $flag => $alias)
623
if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {
624
$flag = strtolower($flag);
625
$alias = strtolower($alias);
627
if (!isset($this->_ruleMap[$flag])) {
630
$flag = $this->_ruleMap[$flag];
631
if (isset($this->_rules[$alias]) || isset($this->_ruleMap[$alias])) {
632
$o = (strlen($alias) == 1 ? '-' : '--') . $alias;
634
* @see Zend_Console_Getopt_Exception
636
throw new Zend_Console_Getopt_Exception(
637
"Option \"$o\" is being defined more than once.");
639
$this->_rules[$flag]['alias'][] = $alias;
640
$this->_ruleMap[$alias] = $flag;
646
* Define help messages for options.
648
* The parameter $help_map is an associative array
649
* mapping option name (short or long) to the help string.
651
* @param array $helpMap
652
* @return Zend_Console_Getopt Provides a fluent interface
654
public function setHelp($helpMap)
656
foreach ($helpMap as $flag => $help)
658
if (!isset($this->_ruleMap[$flag])) {
661
$flag = $this->_ruleMap[$flag];
662
$this->_rules[$flag]['help'] = $help;
668
* Parse command-line arguments and find both long and short
671
* Also find option parameters, and remaining arguments after
672
* all options have been parsed.
674
* @return Zend_Console_Getopt|null Provides a fluent interface
676
public function parse()
678
if ($this->_parsed === true) {
681
$argv = $this->_argv;
682
$this->_options = array();
683
$this->_remainingArgs = array();
684
while (count($argv) > 0) {
685
if ($argv[0] == '--') {
687
if ($this->_getoptConfig[self::CONFIG_DASHDASH]) {
688
$this->_remainingArgs = array_merge($this->_remainingArgs, $argv);
692
if (substr($argv[0], 0, 2) == '--') {
693
$this->_parseLongOption($argv);
694
} else if (substr($argv[0], 0, 1) == '-') {
695
$this->_parseShortOptionCluster($argv);
697
$this->_remainingArgs[] = array_shift($argv);
700
$this->_parsed = true;
705
* Parse command-line arguments for a single long option.
706
* A long option is preceded by a double '--' character.
707
* Long options may not be clustered.
709
* @param mixed &$argv
712
protected function _parseLongOption(&$argv)
714
$optionWithParam = ltrim(array_shift($argv), '-');
715
$l = explode('=', $optionWithParam);
716
$flag = array_shift($l);
717
$param = array_shift($l);
719
array_unshift($argv, $param);
721
$this->_parseSingleOption($flag, $argv);
725
* Parse command-line arguments for short options.
726
* Short options are those preceded by a single '-' character.
727
* Short options may be clustered.
729
* @param mixed &$argv
732
protected function _parseShortOptionCluster(&$argv)
734
$flagCluster = ltrim(array_shift($argv), '-');
735
foreach (str_split($flagCluster) as $flag) {
736
$this->_parseSingleOption($flag, $argv);
741
* Parse command-line arguments for a single option.
743
* @param string $flag
745
* @throws Zend_Console_Getopt_Exception
748
protected function _parseSingleOption($flag, &$argv)
750
if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {
751
$flag = strtolower($flag);
753
if (!isset($this->_ruleMap[$flag])) {
755
* @see Zend_Console_Getopt_Exception
757
throw new Zend_Console_Getopt_Exception(
758
"Option \"$flag\" is not recognized.",
759
$this->getUsageMessage());
761
$realFlag = $this->_ruleMap[$flag];
762
switch ($this->_rules[$realFlag]['param']) {
764
if (count($argv) > 0) {
765
$param = array_shift($argv);
766
$this->_checkParameterType($realFlag, $param);
769
* @see Zend_Console_Getopt_Exception
771
throw new Zend_Console_Getopt_Exception(
772
"Option \"$flag\" requires a parameter.",
773
$this->getUsageMessage());
777
if (count($argv) > 0 && substr($argv[0], 0, 1) != '-') {
778
$param = array_shift($argv);
779
$this->_checkParameterType($realFlag, $param);
787
$this->_options[$realFlag] = $param;
791
* Return true if the parameter is in a valid format for
793
* Throw an exception in most other cases.
795
* @param string $flag
796
* @param string $param
797
* @throws Zend_Console_Getopt_Exception
800
protected function _checkParameterType($flag, $param)
803
if (isset($this->_rules[$flag]['paramType'])) {
804
$type = $this->_rules[$flag]['paramType'];
808
if (preg_match('/\W/', $param)) {
810
* @see Zend_Console_Getopt_Exception
812
throw new Zend_Console_Getopt_Exception(
813
"Option \"$flag\" requires a single-word parameter, but was given \"$param\".",
814
$this->getUsageMessage());
818
if (preg_match('/\D/', $param)) {
820
* @see Zend_Console_Getopt_Exception
822
throw new Zend_Console_Getopt_Exception(
823
"Option \"$flag\" requires an integer parameter, but was given \"$param\".",
824
$this->getUsageMessage());
835
* Define legal options using the gnu-style format.
837
* @param string $rules
840
protected function _addRulesModeGnu($rules)
842
$ruleArray = array();
845
* Options may be single alphanumeric characters.
846
* Options may have a ':' which indicates a required string parameter.
847
* No long options or option aliases are supported in GNU style.
849
preg_match_all('/([a-zA-Z0-9]:?)/', $rules, $ruleArray);
850
foreach ($ruleArray[1] as $rule) {
852
$flag = substr($rule, 0, 1);
853
if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {
854
$flag = strtolower($flag);
856
$r['alias'][] = $flag;
857
if (substr($rule, 1, 1) == ':') {
858
$r['param'] = 'required';
859
$r['paramType'] = 'string';
861
$r['param'] = 'none';
863
$this->_rules[$flag] = $r;
864
$this->_ruleMap[$flag] = $flag;
869
* Define legal options using the Zend-style format.
871
* @param array $rules
872
* @throws Zend_Console_Getopt_Exception
875
protected function _addRulesModeZend($rules)
877
foreach ($rules as $ruleCode => $helpMessage)
879
// this may have to translate the long parm type if there
880
// are any complaints that =string will not work (even though that use
881
// case is not documented)
882
if (in_array(substr($ruleCode, -2, 1), array('-', '='))) {
883
$flagList = substr($ruleCode, 0, -2);
884
$delimiter = substr($ruleCode, -2, 1);
885
$paramType = substr($ruleCode, -1);
887
$flagList = $ruleCode;
888
$delimiter = $paramType = null;
890
if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {
891
$flagList = strtolower($flagList);
893
$flags = explode('|', $flagList);
895
$mainFlag = $flags[0];
896
foreach ($flags as $flag) {
899
* @see Zend_Console_Getopt_Exception
901
throw new Zend_Console_Getopt_Exception(
902
"Blank flag not allowed in rule \"$ruleCode\".");
904
if (strlen($flag) == 1) {
905
if (isset($this->_ruleMap[$flag])) {
907
* @see Zend_Console_Getopt_Exception
909
throw new Zend_Console_Getopt_Exception(
910
"Option \"-$flag\" is being defined more than once.");
912
$this->_ruleMap[$flag] = $mainFlag;
913
$rule['alias'][] = $flag;
915
if (isset($this->_rules[$flag]) || isset($this->_ruleMap[$flag])) {
917
* @see Zend_Console_Getopt_Exception
919
throw new Zend_Console_Getopt_Exception(
920
"Option \"--$flag\" is being defined more than once.");
922
$this->_ruleMap[$flag] = $mainFlag;
923
$rule['alias'][] = $flag;
926
if (isset($delimiter)) {
927
switch ($delimiter) {
928
case self::PARAM_REQUIRED:
929
$rule['param'] = 'required';
931
case self::PARAM_OPTIONAL:
933
$rule['param'] = 'optional';
935
switch (substr($paramType, 0, 1)) {
936
case self::TYPE_WORD:
937
$rule['paramType'] = 'word';
939
case self::TYPE_INTEGER:
940
$rule['paramType'] = 'integer';
942
case self::TYPE_STRING:
944
$rule['paramType'] = 'string';
947
$rule['param'] = 'none';
949
$rule['help'] = $helpMessage;
950
$this->_rules[$mainFlag] = $rule;