bjori Wed Nov 21 10:24:23 2007 UTC
Added files: (Branch: PHP_5_3)
/php-src/ext/standard/tests/general_functions getopt_004.phpt
getopt_005.phpt
Modified files:
/php-src/ext/standard basic_functions.c
/php-src/main getopt.c
Log:
MFH: - Add support for optional values
MFH: - Add support for = as seperator
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/basic_functions.c?r1=1.725.2.31.2.64.2.13&r2=1.725.2.31.2.64.2.14&diff_format=u
Index: php-src/ext/standard/basic_functions.c
diff -u php-src/ext/standard/basic_functions.c:1.725.2.31.2.64.2.13
php-src/ext/standard/basic_functions.c:1.725.2.31.2.64.2.14
--- php-src/ext/standard/basic_functions.c:1.725.2.31.2.64.2.13 Tue Nov 20
22:17:00 2007
+++ php-src/ext/standard/basic_functions.c Wed Nov 21 10:24:21 2007
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: basic_functions.c,v 1.725.2.31.2.64.2.13 2007/11/20 22:17:00 johannes
Exp $ */
+/* $Id: basic_functions.c,v 1.725.2.31.2.64.2.14 2007/11/21 10:24:21 bjori Exp
$ */
#include "php.h"
#include "php_streams.h"
@@ -4532,6 +4532,10 @@
paras->opt_name = NULL;
if (paras->need_param == 1) {
opts++;
+ if (*opts == ':') {
+ paras->need_param++;
+ opts++;
+ }
}
paras++;
}
http://cvs.php.net/viewvc.cgi/php-src/main/getopt.c?r1=1.1.2.2&r2=1.1.2.3&diff_format=u
Index: php-src/main/getopt.c
diff -u php-src/main/getopt.c:1.1.2.2 php-src/main/getopt.c:1.1.2.3
--- php-src/main/getopt.c:1.1.2.2 Mon Oct 1 12:40:54 2007
+++ php-src/main/getopt.c Wed Nov 21 10:24:22 2007
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: getopt.c,v 1.1.2.2 2007/10/01 12:40:54 jani Exp $ */
+/* $Id: getopt.c,v 1.1.2.3 2007/11/21 10:24:22 bjori Exp $ */
#include <stdio.h>
#include <string.h>
@@ -80,24 +80,36 @@
}
}
if ((argv[*optind][0] == '-') && (argv[*optind][1] == '-')) {
+ char *pos;
+ int arg_end = strlen(argv[*optind])-1;
+
/* '--' indicates end of args if not followed by a known long
option name */
if (argv[*optind][2] == '\0') {
(*optind)++;
return(EOF);
}
+ arg_start = 2;
+
+ /* Check for <arg>=<val> */
+ if ((pos = php_memnstr(&argv[*optind][arg_start], "=", 1,
argv[*optind]+arg_end)) != NULL) {
+ arg_end = pos-&argv[*optind][arg_start];
+ arg_start++;
+ }
+
+
while (1) {
php_optidx++;
if (opts[php_optidx].opt_char == '-') {
(*optind)++;
return(php_opt_error(argc, argv, *optind-1,
optchr, OPTERRARG, show_err));
- } else if (opts[php_optidx].opt_name &&
!strcmp(&argv[*optind][2], opts[php_optidx].opt_name)) {
+ } else if (opts[php_optidx].opt_name &&
!strncmp(&argv[*optind][2], opts[php_optidx].opt_name, arg_end)) {
break;
}
}
optchr = 0;
dash = 0;
- arg_start = 2 + strlen(opts[php_optidx].opt_name);
+ arg_start += strlen(opts[php_optidx].opt_name);
} else {
if (!dash) {
dash = 1;
@@ -133,14 +145,23 @@
}
if (opts[php_optidx].need_param) {
/* Check for cases where the value of the argument
- is in the form -<arg> <val> or in the form -<arg><val> */
+ is in the form -<arg> <val>, -<arg>=<varl> or -<arg><val> */
dash = 0;
if (!argv[*optind][arg_start]) {
(*optind)++;
if (*optind == argc) {
- return(php_opt_error(argc, argv, *optind-1,
optchr, OPTERRARG, show_err));
- }
- *optarg = argv[(*optind)++];
+ /* Was the value required or is it optional? */
+ if (opts[php_optidx].need_param == 1) {
+ return(php_opt_error(argc, argv,
*optind-1, optchr, OPTERRARG, show_err));
+ }
+ /* Optional value is not supported with -<arg> <val>
style */
+ } else if (opts[php_optidx].need_param == 1) {
+ *optarg = argv[(*optind)++];
+ }
+ } else if (argv[*optind][arg_start] == '=') {
+ arg_start++;
+ *optarg = &argv[*optind][arg_start];
+ (*optind)++;
} else {
*optarg = &argv[*optind][arg_start];
(*optind)++;
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/general_functions/getopt_004.phpt?view=markup&rev=1.1
Index: php-src/ext/standard/tests/general_functions/getopt_004.phpt
+++ php-src/ext/standard/tests/general_functions/getopt_004.phpt
--TEST--
getopt#004 (Optional values)
--ARGS--
-v -v1 -v=10 --v --v=100
--INI--
register_argc_argv=On
variables_order=GPS
--FILE--
<?php
var_dump(getopt("v::", array("v::")));
?>
--EXPECT--
array(1) {
["v"]=>
array(5) {
[0]=>
bool(false)
[1]=>
string(1) "1"
[2]=>
string(2) "10"
[3]=>
bool(false)
[4]=>
string(3) "100"
}
}
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/general_functions/getopt_005.phpt?view=markup&rev=1.1
Index: php-src/ext/standard/tests/general_functions/getopt_005.phpt
+++ php-src/ext/standard/tests/general_functions/getopt_005.phpt
--TEST--
getopt#005 (Required values)
--ARGS--
--arg value --arg=value -avalue -a=value -a value
--INI--
register_argc_argv=On
variables_order=GPS
--FILE--
<?php
var_dump(getopt("a:", array("arg:")));
?>
--EXPECT--
array(2) {
["arg"]=>
array(2) {
[0]=>
string(5) "value"
[1]=>
string(5) "value"
}
["a"]=>
array(3) {
[0]=>
string(5) "value"
[1]=>
string(5) "value"
[2]=>
string(5) "value"
}
}
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php