From 43071528668efbeefebbc34471b3989b782c3c38 Mon Sep 17 00:00:00 2001 From: Huidae Cho Date: Thu, 7 Dec 2023 08:36:59 -0700 Subject: [PATCH] parser: Support renamed flags (flags/options to options/flags as well) (#3256) * parser: Suppoert renamed flags (flags/options to options/flags as well) * return after fatal error * Fix short flag and --overwrite renaming * Single return for long flag renaming * Add more specific comments about option value for flag to/from option renaming * Remove "in GRASS 7" * Declare and assign renamed_key in one line --- lib/gis/parser.c | 87 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 4 deletions(-) diff --git a/lib/gis/parser.c b/lib/gis/parser.c index c66a82c7aac..d84dcb174f2 100644 --- a/lib/gis/parser.c +++ b/lib/gis/parser.c @@ -998,7 +998,8 @@ int module_gui_wx(void) void set_flag(int f) { struct Flag *flag; - char *err; + char *key, *err; + const char *renamed_key; err = NULL; @@ -1024,6 +1025,67 @@ void set_flag(int f) flag = flag->next_flag; } + /* First, check if key has been renamed */ + G_asprintf(&key, "-%c", f); + renamed_key = get_renamed_option(key); + G_free(key); + + if (renamed_key) { + /* if renamed to a new flag */ + if (*renamed_key == '-') { + /* if renamed to a long flag */ + if (renamed_key[1] == '-') { + if (strcmp(renamed_key, "--overwrite") == 0) { + /* this is a special case for -? to --overwrite */ + G_warning(_("Please update the usage of <%s>: " + "flag <%c> has been renamed to <%s>"), + G_program_name(), f, renamed_key); + st->overwrite = 1; + } + else { + /* long flags other than --overwrite are usually specific to + * GRASS internals, just print an error and let's not + * support them */ + G_asprintf(&err, + _("Please update the usage of <%s>: " + "flag <%c> has been renamed to <%s>"), + G_program_name(), f, renamed_key); + append_error(err); + } + return; + } + /* if renamed to a short flag */ + for (flag = &st->first_flag; flag; flag = flag->next_flag) { + if (renamed_key[1] == flag->key) { + G_warning(_("Please update the usage of <%s>: " + "flag <%c> has been renamed to <%s>"), + G_program_name(), f, renamed_key); + flag->answer = 1; + if (flag->suppress_required) + st->suppress_required = 1; + if (flag->suppress_overwrite) + st->suppress_overwrite = 1; + return; + } + } + } + else { + /* if renamed to a new option (no option value given but will be + * required), fatal error */ + struct Option *opt = NULL; + for (opt = &st->first_option; opt; opt = opt->next_opt) { + if (strcmp(renamed_key, opt->key) == 0) { + G_asprintf(&err, + _("Please update the usage of <%s>: " + "flag <%c> has been renamed to option <%s>"), + G_program_name(), f, renamed_key); + append_error(err); + return; + } + } + } + } + G_asprintf(&err, _("%s: Sorry, <%c> is not a valid flag"), G_program_name(), f); append_error(err); @@ -1175,12 +1237,29 @@ void set_option(const char *string) if (found) opt = matches[0]; - /* First, check if key has been renamed in GRASS 7 */ + /* First, check if key has been renamed */ if (found == 0) { - const char *renamed_key = NULL; + const char *renamed_key = get_renamed_option(the_key); - renamed_key = get_renamed_option(the_key); if (renamed_key) { + /* if renamed to a new flag (option value given but will be lost), + * fatal error */ + if (*renamed_key == '-') { + if (renamed_key[1] == '-') + G_asprintf(&err, + _("Please update the usage of <%s>: " + "option <%s> has been renamed to flag <%s>"), + G_program_name(), the_key, renamed_key); + else + G_asprintf(&err, + _("Please update the usage of <%s>: " + "option <%s> has been renamed to flag <%c>"), + G_program_name(), the_key, renamed_key[1]); + append_error(err); + return; + } + + /* if renamed to a new option */ for (at_opt = &st->first_option; at_opt; at_opt = at_opt->next_opt) { if (strcmp(renamed_key, at_opt->key) == 0) {