boolean option processing

Option parsing for booleans tried to accept "optname:true" or
"optname:yes" or "optname:false" or "optname:no" but it didn't work
because boolean options with a value were rejected before getting
to that.  Make parsing for booleans get far enough to handle those
values, treat them as case-insensitive, and add "on" and "off" as
additional choices.  "true" and "false" can be truncated to 3
letters, the other values need to be fully spelled out but are all
only 2 or 3 letters long.
This commit is contained in:
PatR
2020-06-06 13:44:48 -07:00
parent 6f9cd70226
commit 330728a8c7
2 changed files with 22 additions and 13 deletions
+3 -1
View File
@@ -1,4 +1,4 @@
$NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.230 $ $NHDT-Date: 1591195832 2020/06/03 14:50:32 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.231 $ $NHDT-Date: 1591476281 2020/06/06 20:44:41 $
General Fixes and Modified Features General Fixes and Modified Features
----------------------------------- -----------------------------------
@@ -363,6 +363,8 @@ new objects: amulets of flying and guarding
new monsters: displacer beast ('f') and genetic engineer ('Q') new monsters: displacer beast ('f') and genetic engineer ('Q')
make camera flash which reveals previously unseen map features or objects or make camera flash which reveals previously unseen map features or objects or
monsters record those on the hero's map; monsters revert to 'unseen' monsters record those on the hero's map; monsters revert to 'unseen'
boolean options can optionally have the form "name:value" with value taken
from among "true", "yes", "on", or "false", "no", "off"
Platform- and/or Interface-Specific New Features Platform- and/or Interface-Specific New Features
+19 -12
View File
@@ -1,4 +1,4 @@
/* NetHack 3.7 options.c $NHDT-Date: 1590263453 2020/05/23 19:50:53 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.465 $ */ /* NetHack 3.7 options.c $NHDT-Date: 1591476281 2020/06/06 20:44:41 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.466 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Michael Allison, 2008. */ /*-Copyright (c) Michael Allison, 2008. */
/* NetHack may be freely redistributed. See license for details. */ /* NetHack may be freely redistributed. See license for details. */
@@ -363,10 +363,12 @@ boolean tinitial, tfrom_file;
} }
} }
#if 0
if (!got_match) { if (!got_match) {
if (has_val && !allopt[i].valok) if (has_val && !allopt[i].valok)
continue; continue;
} }
#endif
/* /*
* During option initialization, the function * During option initialization, the function
@@ -377,13 +379,13 @@ boolean tinitial, tfrom_file;
* *
*/ */
if (!got_match) if (!got_match)
got_match = match_optname(opts, allopt[i].name, allopt[i].minmatch, got_match = match_optname(opts, allopt[i].name,
allopt[i].valok); allopt[i].minmatch, TRUE);
if (got_match) { if (got_match) {
if (!allopt[i].pfx && optlen < allopt[i].minmatch) { if (!allopt[i].pfx && optlen < allopt[i].minmatch) {
config_error_add( config_error_add(
"Ambiguous option %s, %d characters are needed to differentiate", "Ambiguous option %s, %d characters are needed to differentiate",
opts, allopt[i].minmatch); opts, allopt[i].minmatch);
break; break;
} }
matchidx = i; matchidx = i;
@@ -403,7 +405,7 @@ boolean tinitial, tfrom_file;
continue; continue;
got_match = match_optname(opts, allopt[i].alias, got_match = match_optname(opts, allopt[i].alias,
(int) strlen(allopt[i].alias), (int) strlen(allopt[i].alias),
allopt[i].valok); TRUE);
if (got_match) { if (got_match) {
matchidx = i; matchidx = i;
using_alias = TRUE; using_alias = TRUE;
@@ -432,8 +434,8 @@ boolean tinitial, tfrom_file;
*/ */
if (allopt[matchidx].optfn) { if (allopt[matchidx].optfn) {
op = string_for_opt(opts, TRUE); op = string_for_opt(opts, TRUE);
optresult = (*allopt[matchidx].optfn)(allopt[matchidx].idx, do_set, optresult = (*allopt[matchidx].optfn)(allopt[matchidx].idx,
negated, opts, op); do_set, negated, opts, op);
} }
} }
@@ -4445,15 +4447,20 @@ char *op;
op = string_for_opt(opts, TRUE); op = string_for_opt(opts, TRUE);
if (op != empty_optstr) { if (op != empty_optstr) {
int ln;
if (negated) { if (negated) {
config_error_add( config_error_add(
"Negated boolean '%s' should not have a parameter", "Negated boolean '%s' should not have a parameter",
allopt[optidx].name); allopt[optidx].name);
return optn_err; return optn_err;
} }
if (!strcmp(op, "true") || !strcmp(op, "yes")) { ln = (int) strlen(op);
if (!strncmpi(op, "true", max(ln, 3))
|| !strcmpi(op, "yes") || !strcmpi(op, "on")) {
negated = FALSE; negated = FALSE;
} else if (!strcmp(op, "false") || !strcmp(op, "no")) { } else if (!strncmpi(op, "false", max(ln, 3))
|| !strcmpi(op, "no") || !strcmpi(op, "off")) {
negated = TRUE; negated = TRUE;
} else if (!allopt[optidx].valok) { } else if (!allopt[optidx].valok) {
config_error_add("Illegal parameter for a boolean"); config_error_add("Illegal parameter for a boolean");