forked from lelutin/php-optparse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
optparse.php
1717 lines (1520 loc) · 50.7 KB
/
optparse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Option parser.
*
* Easily parse command line arguments in PHP. This parser has the same
* interface as the Python "optparse" module.
*
* Example usage:
* $parser = new OptionParser();
* $parser->add_option(array("-f", "--foo", "dest"=>"bar"));
* $values = $parser->parse_args($argv);
*/
if (! defined("__OPTPARSE_PHP") ) {
define("__OPTPARSE_PHP", "");
define("NO_SUCH_OPT_ERROR", 1);
define("WRONG_VALUE_COUNT_ERROR", 2);
define("OPTION_VALUE_ERROR", 3);
// Default value for an option can be Null. We need an explicit no_default value
define("NO_DEFAULT", "~~~NO~DEFAULT~~~");
// Special value for help to suppress its output for an option.
define("SUPPRESS_HELP", "~~~SUPPRESS~HELP~~~");
// Default translation simply returns the string as-is
function _no_translation($string) { return $string; }
// Define this constant before inclusion, or redefine it with the translation
// function name.
// The function should take a string in and return its translated form.
if (! defined("_OPTPARSE_T") ) {
define("_OPTPARSE_T", "_no_translation");
}
function _translate($string, $variables=array(),
$translator=_OPTPARSE_T) {
assert( function_exists($translator) );
$new_text = $translator($string);
// Use values from $variables to replace pattends of the form %(name)s
foreach ($variables as $name => $var) {
$new_text = preg_replace("/%\($name\)s/", $var, $new_text);
}
return $new_text;
}
/**
* Retrieve an element and remove it from an array.
*
* If the key is not present in the array, return the default value, given in
* the third argument. If the third argument is omitted, the default value is
* Null.
*
* @return mixed: value from the array or default value if key is not in array.
* @author Gabriel Filion
**/
function _array_pop_elem(&$array, $key, $default=null) {
assert( is_string($key) );
if ( ! array_key_exists($key, $array) ) {
$value = $default;
}
else {
$value = $array[$key];
}
if ( array_key_exists($key, $array) ) {
unset($array[$key]);
}
return $value;
}
/**
* Utility class for parsing arguments from the Command Line Interface
*
* This class has one difference from its Python counterpart: it has no
* "option_list" argument. This argument is currently marked as deprecated in
* Python's optparse module in favor of using the add_option method.
*
* @author Gabriel Filion <[email protected]>
*/
class OptionParser {
protected $standard_option_list = array();
public function __construct($settings=array()) {
$this->_positional = array();
$this->option_list = $this->standard_option_list;
// This must come first so that calls to add_option can succeed.
$this->option_class = _array_pop_elem(
$settings,
"option_class",
"Option"
);
if ( ! is_string($this->option_class) ) {
$msg = _translate("The setting \"option_class\" must be a string");
throw new InvalidArgumentException($msg);
}
$default_usage = _translate("%prog [options]");
$this->set_usage( _array_pop_elem($settings, "usage", $default_usage) );
$this->description = _array_pop_elem($settings, "description", "");
$this->epilog = _array_pop_elem($settings, "epilog", "");
$this->defaults = array();
$add_help_option = _array_pop_elem($settings, "add_help_option", true);
if ($add_help_option) {
$this->add_option( array(
"-h","--help",
"action" => "help",
"help" => _translate("show this help message and exit")
) );
}
$this->version = _array_pop_elem($settings, "version", "");
if ($this->version) {
$this->add_option( array(
"--version",
"action" => "version",
"help" => _translate("show program's version number and exit")
) );
}
$this->set_conflict_handler(_array_pop_elem(
$settings,
"conflict_handler",
"error"
) );
$this->prog = _array_pop_elem(
$settings,
"prog",
basename($_SERVER['SCRIPT_FILENAME']) // name of the executable
);
$this->formatter = _array_pop_elem($settings, "formatter", Null);
if ($this->formatter == Null) {
$this->formatter = new IndentedHelpFormatter();
}
// Still some settings left? we don't know about them. yell
if ( ! empty($settings) ) {
throw new OptionError($settings);
}
}
/**
* Add an option that the parser must recognize.
*
* The argument can be either an array with settings for the option class's
* constructor, or an Option instance.
*
* @return Option object
* @throws InvalidArgumentException: if argument isn't an array or an Option
* @author Gabriel Filion <[email protected]>
**/
public function add_option($settings) {
if ( is_array($settings) ) {
$option_class = $this->option_class;
$new_option = new $option_class($settings);
}
else if ( is_a($settings, Option) ) {
$new_option = $settings;
}
else {
$vals = array("arg" => $settings);
$msg = _translate("not an Option instance: %(arg)s", $vals);
throw new InvalidArgumentException($msg);
}
// Resolve conflict with the right conflict handler
foreach ( $new_option->option_strings as $name ) {
$option = $this->get_option($name);
if ( $option !== Null ) {
if ( $this->conflict_handler == "resolve" ) {
$this->_resolve_option_conflict($option, $name, $this);
}
else {
throw new OptionConflictError($name);
}
}
}
$this->option_list[] = $new_option;
// Option has a destination. we need a default value
if ($new_option->dest !== Null) {
if ($new_option->default !== NO_DEFAULT) {
$this->defaults[$new_option->dest] = $new_option->default;
}
else if ( ! array_key_exists($new_option->dest, $this->defaults) ) {
$this->defaults[$new_option->dest] = Null;
}
}
return $new_option;
}
/**
* Search for an option name in current options.
*
* Given an option string, search for the Option object that uses this
* string. If the option cannot be found, return Null.
*
* @return Option object: when the option is found
* @return Null: when the option is not found
* @author Gabriel Filion <[email protected]>
**/
public function get_option($text) {
$found = Null;
foreach ($this->option_list as $opt) {
if ( in_array($text, $opt->option_strings) ) {
$found = $opt;
break;
}
}
return $found;
}
/**
* Verify presence of an option in the parser.
*
* Given an option string, find out if one of the parser's options uses this
* string. It is a convenient way to verify that an option was already
* added.
*
* @return boolean: true if option is present, false if not
* @author Gabriel Filion <[email protected]>
**/
public function has_option($text) {
foreach ($this->option_list as $opt) {
if ( in_array($text, $opt->option_strings) ) {
return true;
}
}
return false;
}
/**
* Remove the option that is mapped to the given string.
*
* If the option uses other strings of text, those strings become invalid
* (unused). If the text does not correspond to an option, a
* OutOfBoundsException is thrown.
*
* @return void
* @author Gabriel Filion <[email protected]>
**/
public function remove_option($text) {
$found = false;
foreach ($this->option_list as $key => $opt) {
if ( in_array($text, $opt->option_strings) ) {
$strings = $opt->option_strings;
unset( $this->option_list[$key] );
$found = true;
$this->_reenable_option_strings($strings);
break;
}
}
if (! $found) {
$vals = array("option" => $text);
$msg = _translate(
"Option \"%(option)s\" does not exist.",
$vals
);
throw new OutOfBoundsException($msg);
}
}
/**
* Set the usage text.
*
* @return void
* @author Gabriel Filion <[email protected]>
**/
public function set_usage($new_usage) {
$this->usage = $new_usage;
}
/**
* Retrieve the usage string.
*
* @return String
* @author Gabriel Filion <[email protected]>
**/
public function get_usage() {
// Replace occurences of %prog to the program name
$usage = $this->formatter->format_usage(
preg_replace(
"/\%prog/",
$this->get_prog_name(),
$this->usage
)
);
return $usage;
}
/**
* Print usage.
*
* Default output stream is stdout. To change it, pass another stream as
* argument.
*
* @return void
* @author Gabriel Filion <[email protected]>
**/
public function print_usage($stream=STDOUT) {
fprintf($stream, $this->get_usage() );
}
/**
* Print the whole help message as seen with option -h.
*
* Default output stream is stdout. To change it, pass another stream as
* argument.
*
* @return void
* @author Gabriel Filion <[email protected]>
**/
public function print_help($stream=STDOUT) {
// TODO encode the string from format_help to ensure it is suitable for
// output.
fprintf($stream, $this->format_help() );
}
/**
* Format the help string, ready for output.
*
* @return string
* @author Gabriel Filion <[email protected]>
**/
public function format_help($formatter=Null) {
if ($formatter == Null) {
$formatter = $this->formatter;
}
$result = "";
if ($this->usage) {
$result .= $this->get_usage() . "\n";
}
if ($this->description) {
$result .= $this->format_description($formatter) . "\n";
}
$result .= $this->format_option_help($formatter);
$result .= $this->format_epilog($formatter);
return $result;
}
/**
* Format the epilog string, ready for output.
*
* @return string
* @author Gabriel Filion <[email protected]>
**/
public function format_epilog($formatter) {
return $formatter->format_epilog($this->epilog);
}
/**
* Format the description, ready for output.
*
* @return string
* @author Gabriel Filion <[email protected]>
**/
public function format_description($formatter) {
return $formatter->format_description($this->get_description() );
}
/**
* Format a string with help for all the options, ready for output.
*
* @return string
* @author Gabriel Filion <[email protected]>
**/
public function format_option_help($formatter=Null) {
if ($formatter == Null) {
$formatter = $this->formatter;
}
$result = array();
$formatter->store_option_strings($this);
array_push(
$result,
$formatter->format_heading(_translate("Options"))
);
$formatter->indent();
if (! empty($this->option_list)) {
//XXX change this call when class hierarchy is settled.
array_push(
$result,
OptionContainer_format_option_help($this, $formatter)
);
array_push($result, "\n");
}
/*for group in self.option_groups:
result.append(group.format_help(formatter))
result.append("\n")*/
$formatter->dedent();
// Drop the last "\n", or the header if no options or option groups:
array_pop($result);
return join("", $result);
}
/**
* Print version information message.
*
* Default output stream is stdout. To change it, pass another stream as
* argument.
*
* @return void
* @author Gabriel Filion <[email protected]>
**/
public function print_version($stream=STDOUT) {
// Replace occurences of %prog to the program name
$version = preg_replace(
"/\%prog/",
$this->get_prog_name(),
$this->get_version()
);
fprintf($stream, $version. "\n\n" );
}
/**
* Retrieve the program name as shown by usage.
*
* @return string
* @author Gabriel Filion <[email protected]>
**/
public function get_prog_name() {
return $this->prog;
}
/**
* Retrieve the description.
*
* @return string
* @author Gabriel Filion <[email protected]>
**/
public function get_description() {
return $this->description;
}
/**
* Retrieve the version tag.
*
* @return string
* @author Gabriel Filion <[email protected]>
**/
public function get_version() {
return $this->version;
}
/**
* Append an array or a value to an array.
*
* Strangely, PHP has no function to simply append (not merge) an array to
* another one. This provides for this lacking feature.
*
* The first array is modified in place, so nothing is returned.
*
* This function discards keys from the second array. To conserve the keys,
* use array_merge.
*
* @return void
* @author Gabriel Filion
**/
private function _array_append(&$array, $appended) {
if ( ! is_array($appended) ) {
$array[] = $appended;
}
foreach ( $appended as $value ) {
$array[] = $value;
}
}
/**
* Parse command line arguments.
*
* Given an array of arguments, parse them and create an object containing
* expected values and positional arguments.
*
* @author Gabriel Filion <[email protected]>
**/
public function parse_args($argv, $values=Null){
// Pop out the first argument, it is assumed to be the command name
array_shift($argv);
if ( $values !== Null && ! is_array($values) ) {
$msg = _translate("Default values must be in an associative array");
throw new InvalidArgumentException($msg);
}
$this->values = array();
if ($values === Null) {
$this->values = $this->get_default_values();
}
else {
// Get a copy of default values and update the array
$this->values = array_merge($this->get_default_values(), $values);
}
$rargs = $argv;
$positional = array();
while ( ! empty($rargs) ){
$arg = array_shift($rargs);
// Stop processing on a -- argument
if ( $arg == "--" ) {
// All remaining arguments are positional
$this->_array_append($positional, $rargs);
break;
}
// Options should begin with a dash. All else is positional
// A single dash alone is also a positional argument
if ( substr($arg, 0, 1) != "-" || strlen($arg) == 1) {
$positional[] = $arg;
}
else if ( substr($arg, 0, 2) == "--" ) {
$this->_process_long_option($arg, $rargs, $this->values);
}
else {
// values will be removed from $rargs during this process
$this->_process_short_options($arg, $rargs, $this->values);
}
}
return new Values($this->values, $positional);
}
/**
* Set the option conflict handler.
*
* Conflict handler can be one of "error" or "resolve".
*
* @return void
* @throws InvalidArgumentException on invalid handler name
* @author Gabriel Filion <[email protected]>
**/
public function set_conflict_handler($handler) {
if ( ! in_array( $handler, array("error", "resolve") ) ) {
$msg = _translate(
"The conflict handler must be one of \"error\" or \"resolve\""
);
throw new InvalidArgumentException($msg);
}
$this->conflict_handler = $handler;
}
/**
* Get the list of default values.
*
* @return array
* @author Gabriel Filion <[email protected]>
**/
public function get_default_values() {
return $this->defaults;
}
/**
* Set default value for only one option.
*
* Default values must have a key that corresponds to the "dest" argument of
* an option.
*
* @return void
* @author Gabriel Filion <[email protected]>
**/
public function set_default($dest, $value) {
$this->defaults[$dest] = $value;
}
/**
* Set default values for multiple destinations.
*
* Default values must have a key that corresponds to the "dest" argument of
* an option. Calling this function is the preferred way of setting default
* values for options, since multiple options can share the same
* destination.
*
* @return void
* @author Gabriel Filion <[email protected]>
**/
public function set_defaults($values) {
$this->defaults = array_merge($this->defaults, $values);
}
/**
* Exit program with an error message and return code.
*
* $text should already be translated when given to this function.
*
* @return void
* @author Gabriel Filion <[email protected]>
**/
public function error($text, $code = 1) {
$this->print_usage(STDERR);
$prog = basename($_SERVER['SCRIPT_FILENAME']);
$l10n_error = _translate("error");
fprintf(STDERR, "$prog: $l10n_error: $text\n");
exit($code);
}
/**
* Process a long option.
*
* Long options that expect value(s) will get them from the next arguments
* given on the command line. The first value can also be appended to them
* with = as a separator.
*
* Examples:
* program --enable-this
* program --option=value
* program --option=value1 value2
* program --option value1 value2
*
* @return void
* @author Gabriel Filion <[email protected]>
**/
private function _process_long_option($argument, &$rargs, &$values) {
$key_value = explode("=", $argument, 2);
$arg_text = $key_value[0];
$option = $this->_get_known_option($arg_text);
// Add the first value if it was appended to the arg with =
if ( count($key_value) > 1 ) {
// Option didn't expect this value
if ($option->nargs < 1) {
$vals = array("option" => $arg_text);
$msg = _translate(
"%(option)s option does not take a value.",
$vals
);
$this->error($msg, WRONG_VALUE_COUNT_ERROR);
}
array_unshift($rargs, $key_value[1]);
}
$this->_process_option($option, $rargs, $arg_text, $values);
}
/**
* Process a conglomerate of short options.
*
* Short options that expect value(s) will get them from the next
* arguments. The first value can also be typed right after the option
* without a space. Options can also be joined in conglomerates. Options
* that expect a value should be at the end of a conglomerate, since the
* rest of the argument will be evaluated as the option's value.
*
* Examples:
* program -q
* program -d something
* program -dsomething
* program -vvf arg_to_f
*
* @return void
* @author Gabriel Filion <[email protected]>
**/
private function _process_short_options($argument,
&$rargs,
&$values)
{
$characters = preg_split(
'//', substr($argument, 1), -1, PREG_SPLIT_NO_EMPTY
);
$i = 1;
$stop = false;
foreach($characters as $ch) {
$opt_string = "-". $ch;
$i++; // an option was consumed
$option = $this->_get_known_option($opt_string);
if ( $option->nargs >= 1) {
// The option expects values, insert the rest of $argument as
// another argument (in rargs), if there is anything.
if ( $i < strlen($argument) ) {
array_unshift($rargs, substr($argument, $i) );
}
// ... and stop iterating.
$stop = true;
}
$this->_process_option($option, $rargs, $opt_string, $values);
if ($stop) {
break;
}
}
}
/**
* Ask an option to process information.
*
* Process an option. If it throws an OptionValueError, exit with an error
* message.
*
* @return void
* @author Gabriel Filion <[email protected]>
**/
private function _process_option(&$option, &$rargs,
$opt_string, &$values) {
$nbvals = $option->nargs;
if ( $nbvals < 1 ) {
$value = $option->default;
}
else {
$value = array();
}
// Not enough values given
if ( count($rargs) < $nbvals ) {
$vals = array("option" => $opt_string);
if ( $nbvals == 1) {
$what = "an argument";
}
else {
$vals["nbargs"] = $nbvals;
$what = "%(nbargs)s arguments";
}
$msg = _translate("%(option)s option takes $what.", $vals);
$this->error($msg, WRONG_VALUE_COUNT_ERROR);
}
while ( $nbvals ) {
$value[] = array_shift($rargs);
$nbvals--;
}
// If only one value, set it directly as the value (not in an array)
if ( $option->nargs == 1 ) {
$value = $value[0];
// Treat the option as the same one until another dash (-) is found to specify we're looking at a new one.
while (is_array($rargs) && isset($rargs[0]) && is_string($rargs[0]) && $rargs[0][0] !== '-') {
$value .= ' '.array_shift($rargs);
}
}
try {
$option->process($value, $opt_string, $values, $this);
}
catch (OptionValueError $exc) {
$this->error(
$exc->getMessage(),
OPTION_VALUE_ERROR
);
}
}
/**
* Find an option with the text from command line.
*
* If the option cannot be found, exit with an error.
*
* @return Option object
* @author Gabriel Filion <[email protected]>
**/
private function _get_known_option($opt_text) {
$option = $this->get_option($opt_text);
// Unknown option. Exit with an error
if ($option === Null) {
$vals = array("option" => $opt_text);
$msg = _translate("No such option: %(option)s", $vals);
$this->error($msg, NO_SUCH_OPT_ERROR);
}
return $option;
}
/**
* Resolve option conflicts intelligently.
*
* This method is the resolver for option conflict_handler="resolve". It
* tries to resolve conflicts automatically. It disables an option string
* so that the last option added that uses this string has precedence.
*
* If an option sees its last string disabled, it is removed entirely.
* Options that get removed cannot be automatically re-enabled later.
*
* @return void
* @author Gabriel Filion <[email protected]>
**/
private function _resolve_option_conflict(&$old_option,
$option_text,
&$parser)
{
if ( count($old_option->option_strings) == 1 ) {
$parser->remove_option($option_text);
return;
}
$old_option->disable_string($option_text);
}
/**
* Re-enable an option string.
*
* When the conflict handler is set to "resolve", some strings may be
* disabled. This method tries to re-enable a string.
*
* @return void
* @author Gabriel Filion <[email protected]>
**/
private function _reenable_option_strings($option_strings) {
$options = array_reverse($this->option_list);
foreach ($option_strings as $option_text) {
foreach ($options as $option) {
$index = array_search($option_text, $option->disabled_strings);
if ($index !== false) {
$option->option_strings[] = $option_text;
unset( $option->disabled_strings[$index] );
break;
}
}
}
}
}
function OptionContainer_format_option_help($container, $formatter) {
if (empty($container->option_list) ) {
return "";
}
$result = "";
foreach ($container->option_list as $option) {
if ($option->help !== SUPPRESS_HELP) {
$result .= $formatter->format_option($option);
}
}
return $result;
}
/**
* Object returned by parse_args.
*
* It contains two attributes: one for the options and one for the positional
* arguments.
**/
class Values {
public function __construct($options, $positional) {
$this->options = $options;
$this->positional = $positional;
}
}
/**
* Class representing an option.
*
* The option parser uses this class to represent options that are added to it.
**/
class Option {
/**
* Set of possible types for options.
**/
protected $TYPES = array(
"string",
"int",
"long",
"float",
"choice"
);
/**
* Set of actions which may consume an argument for type.
**/
protected $TYPED_ACTIONS = array(
"store",
"append",
"callback"
);
/**
* Set of actions which require the type to be specified as an argument.
**/
protected $ALWAYS_TYPED_ACTIONS = array(
"store",
"append"
);
/**
* Those actions use a constant to store information. They should be paired
* with a "const" argument to the Option constructor.
**/
protected $CONST_ACTIONS = array(
"store_const",
"append_const"
);
public function __construct($settings) {
$option_strings = array();
// Get all option strings. They should be added without key in settings
$i = 0;
$longest_name = "";
while ( $option_name = _array_pop_elem($settings, "$i") ) {
$option_strings[] = $option_name;
// Get the name without leading dashes
if ($option_name[1] == "-") {
$name = substr($option_name, 2);
}
else {
$name = substr($option_name, 1);
}
// Keep only the longest name for default dest
if ( strlen($name) > strlen($longest_name) ) {
$longest_name = $name;
}
$i++;
}
if ( empty($option_strings) ) {
$msg = _translate(
"An option must have at least one string representation"
);
throw new InvalidArgumentException($msg);
}
$this->disabled_strings = array();
$this->option_strings = $option_strings;
$this->type = _array_pop_elem($settings, "type", Null);
$this->choices = _array_pop_elem($settings, "choices", Null);
// Default values that may be overridden by sensible action defaults or
// by settings
$this->dest = $longest_name;
$this->nargs = 1;
$this->default = NO_DEFAULT;
// Set some sensible defaults depending on the chosen action
$this->action = _array_pop_elem($settings, "action", "store");
$this->_set_defaults_by_action($this->action);
// Get default value
//
// Using this can lead to results that are unexpected.
// Use OptionParser.set_defaults instead
$this->default = _array_pop_elem($settings, "default", $this->default);
// Other option settings
$this->help = _array_pop_elem($settings, "help", "");
$this->callback = _array_pop_elem($settings, "callback");
$this->callback_args = _array_pop_elem($settings, "callback_args", Null);
$this->_add_kwargs(
_array_pop_elem($settings, "callback_kwargs", Null)
);
$this->const = _array_pop_elem($settings, "const", NO_DEFAULT);
// Destination and metavar
$this->dest = _array_pop_elem($settings, "dest", $this->dest);
$this->metavar = _array_pop_elem(
$settings,
"metavar",