Revision: 65204
http://sourceforge.net/p/brlcad/code/65204
Author: starseeker
Date: 2015-06-05 19:53:38 +0000 (Fri, 05 Jun 2015)
Log Message:
-----------
More tweaking of option handling.
Modified Paths:
--------------
brlcad/trunk/src/libbu/opt.c
brlcad/trunk/src/libbu/tests/CMakeLists.txt
brlcad/trunk/src/libbu/tests/opt.c
Modified: brlcad/trunk/src/libbu/opt.c
===================================================================
--- brlcad/trunk/src/libbu/opt.c 2015-06-05 19:40:24 UTC (rev 65203)
+++ brlcad/trunk/src/libbu/opt.c 2015-06-05 19:53:38 UTC (rev 65204)
@@ -380,8 +380,7 @@
bu_vls_sprintf(&varg, "%s", inputcpy);
bu_vls_nibble(&varg, 2);
-#if 0
- /* A possible exception is an equals sign, e.g. -s=1024 - in that
+ /* A exception is an equals sign, e.g. -s=1024 - in that
* instance, the expectation might be that = would be interpreted
* as an assignment. This means that to get the literal =1024 as
* an option, you would need a space after the s, e.g.: -s =1024
@@ -393,7 +392,6 @@
if (equal_pos && equal_pos == inputcpy+2) {
bu_vls_nibble(&varg, 1);
}
-#endif
(*eq_arg) = bu_strdup(bu_vls_addr(&varg));
final_opt = bu_strdup(bu_vls_addr(&vopt));
@@ -620,8 +618,8 @@
if (i != argc || arg_cnt) {
if (arg_cnt)
g_argv[0] = eq_arg;
- for (k = arg_cnt; k < g_argc; k++) {
- g_argv[k] = argv[i + k];
+ for (k = 0; k < g_argc; k++) {
+ g_argv[k+arg_cnt] = argv[i + k];
}
data->argc = g_argc;
data->argv = g_argv;
@@ -632,9 +630,17 @@
* arg_process callback returns -1, something has gone
* seriously awry and a known-to-be-invalid arg was
* seen. Fail early and hard. */
- if (msgs) bu_vls_printf(msgs, "Invalid argument
supplied to %s - haulting.\n", argv[i-1]);
+ if (msgs) bu_vls_printf(msgs, "Invalid argument
supplied to %s: %s - haulting.\n", argv[i-1], argv[i]);
return -1;
}
+ if (arg_offset == 0) {
+ if (desc->arg_cnt_min > 0) {
+ if (msgs) bu_vls_printf(msgs, "Option %s requires
an argument but none was found - haulting.\n", argv[i-1]);
+ return -1;
+ } else {
+ continue;
+ }
+ }
i = i + arg_offset - arg_cnt;
if (!arg_offset && arg_cnt) arg_offset = arg_cnt;
/* If we used 1 or more args, construct the final argv
array that will
@@ -642,14 +648,17 @@
if (arg_offset > 0) {
data->argc = arg_offset;
data->argv = (const char **)bu_calloc(arg_offset + 1,
sizeof(char *), "final array");
- if (arg_cnt) data->argv[0] = eq_arg;
- for (k = arg_cnt; k < arg_offset; k++) {
- data->argv[k] = bu_strdup(g_argv[k - arg_cnt]);
+ for (k = 0; k < arg_offset; k++) {
+ data->argv[k] = bu_strdup(g_argv[k]);
}
}
} else {
- /* Need args, but have none - invalid */
- data->valid = 0;
+ if (desc->arg_cnt_min == 0) {
+ /* If this is allowed to function just as a flag, an
int may
+ * be supplied to record the status - try to set it */
+ int *flag_var = (int *)desc->set_var;
+ if (flag_var) (*flag_var) = 1;
+ }
data->argc = 0;
data->argv = NULL;
}
Modified: brlcad/trunk/src/libbu/tests/CMakeLists.txt
===================================================================
--- brlcad/trunk/src/libbu/tests/CMakeLists.txt 2015-06-05 19:40:24 UTC (rev
65203)
+++ brlcad/trunk/src/libbu/tests/CMakeLists.txt 2015-06-05 19:53:38 UTC (rev
65204)
@@ -455,6 +455,7 @@
add_test(NAME bu_opt_1_17 COMMAND tester_bu_opt 1 --?4)
add_test(NAME bu_opt_1_18 COMMAND tester_bu_opt 1 --? 4)
add_test(NAME bu_opt_1_19 COMMAND tester_bu_opt 1 --?=4)
+add_test(NAME bu_opt_1_20 COMMAND tester_bu_opt 1 -v)
add_test(NAME bu_opt_2_00 COMMAND tester_bu_opt 2 -c)
add_test(NAME bu_opt_2_01 COMMAND tester_bu_opt 2 --color)
add_test(NAME bu_opt_2_02 COMMAND tester_bu_opt 2 --color 200/10/30)
Modified: brlcad/trunk/src/libbu/tests/opt.c
===================================================================
--- brlcad/trunk/src/libbu/tests/opt.c 2015-06-05 19:40:24 UTC (rev 65203)
+++ brlcad/trunk/src/libbu/tests/opt.c 2015-06-05 19:53:38 UTC (rev 65204)
@@ -30,9 +30,16 @@
{
int val = INT_MAX;
int *int_set = (int *)set_v;
- int int_ret = bu_opt_arg_int(msg, data, (void *)&val);
- if (int_ret == -1 || int_ret == 0) return int_ret;
+ int int_ret;
+ if (!data || !data->argv || !data->argv[0] || strlen(data->argv[0]) == 0
|| data->argc == 1) {
+ /* Have verbosity flag but no valid arg - go with just the flag */
+ if (int_set) (*int_set) = 1;
+ return 0;
+ }
+ int_ret = bu_opt_arg_int(msg, data, (void *)&val);
+ if (int_ret == -1) return -1;
+
if (val < 0 || val > 3) return -1;
if (int_set) (*int_set) = val;
@@ -67,7 +74,7 @@
if (!bu_str_to_rgb(bu_vls_addr(&tmp_color), (unsigned char *)&rgb))
{
/* Not valid with 3 */
bu_vls_free(&tmp_color);
- if (msg) bu_vls_sprintf(msg, "No valid color found.");
+ if (msg) bu_vls_sprintf(msg, "No valid color found.\n");
return -1;
} else {
/* 3 did the job */
@@ -78,7 +85,7 @@
} else {
/* Not valid with 1 and don't have 3 - we require at least one, so
* claim one argv as belonging to this option regardless. */
- if (msg) bu_vls_sprintf(msg, "No valid color found: %s",
data->argv[0]);
+ if (msg) bu_vls_sprintf(msg, "No valid color found: %s\n",
data->argv[0]);
return -1;
}
} else {
@@ -116,7 +123,8 @@
main(int argc, const char **argv)
{
int ret = 0;
- int function_num;
+ char *endptr = NULL;
+ long test_num;
struct bu_ptbl *results = NULL;
struct bu_vls parse_msgs = BU_VLS_INIT_ZERO;
static int i = 0;
@@ -149,9 +157,12 @@
if (argc < 2)
bu_exit(1, "ERROR: wrong number of parameters");
- sscanf(argv[1], "%d", &function_num);
+ test_num = strtol(argv[1], &endptr, 0);
+ if (endptr && strlen(endptr) != 0) {
+ bu_exit(1, "Invalid test number: %s\n", argv[1]);
+ }
- switch (function_num) {
+ switch (test_num) {
case 0:
ret = bu_opt_parse(&results, &parse_msgs, 0, NULL, NULL);
return (results == NULL) ? 0 : 1;
@@ -174,9 +185,9 @@
if (results) {
bu_opt_compact(results);
print_results(results);
- if (function_num == 1 || function_num == 3)
+ if (test_num == 1 || test_num == 3)
bu_log("Int var: %d\n", i);
- if (function_num == 2)
+ if (test_num == 2)
bu_log("Color var: %0.2f, %0.2f, %0.2f\n", color.buc_rgb[0],
color.buc_rgb[1], color.buc_rgb[2]);
}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits