Skip to content

Commit

Permalink
parser: Support renamed flags (flags/options to options/flags as well) (
Browse files Browse the repository at this point in the history
OSGeo#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
  • Loading branch information
HuidaeCho committed Jan 9, 2024
1 parent a8f893c commit 4307152
Showing 1 changed file with 83 additions and 4 deletions.
87 changes: 83 additions & 4 deletions lib/gis/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4307152

Please sign in to comment.