Commit 1b37a8c3 authored by Andrew Dunstan's avatar Andrew Dunstan

Don't override arguments set via options with positional arguments.

A number of utility programs were rather careless about paremeters
that can be set via both an option argument and a positional
argument. This leads to results which can violate the Principal
Of Least Astonishment. These changes refuse to use positional
arguments to override settings that have been made via positional
arguments. The changes are backpatched to all live branches.
parent fe546f3d
......@@ -2760,8 +2760,11 @@ main(int argc, char *argv[])
}
/* Non-option argument specifies data directory */
if (optind < argc)
/*
* Non-option argument specifies data directory
* as long as it wasn't already specified with -D / --pgdata
*/
if (optind < argc && strlen(pg_data) == 0)
{
pg_data = xstrdup(argv[optind]);
optind++;
......
......@@ -112,18 +112,22 @@ main(int argc, char *argv[])
}
}
switch (argc - optind)
/*
* Non-option argument specifies database name
* as long as it wasn't already specified with -d / --dbname
*/
if (optind < argc && dbname == NULL)
{
case 0:
break;
case 1:
dbname = argv[optind];
break;
default:
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
progname, argv[optind + 1]);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
dbname = argv[optind];
optind++;
}
if (optind < argc)
{
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
progname, argv[optind + 1]);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
}
setup_cancel_handler();
......
......@@ -91,14 +91,24 @@ main(int argc, char *argv[])
}
}
/*
* We set dbname from positional arguments if it is not
* already set by option arguments -d. If not doing
* listlangs, positional dbname must follow positional
* langname.
*/
if (argc - optind > 0)
{
if (listlangs)
dbname = argv[optind++];
{
if (dbname == NULL)
dbname = argv[optind++];
}
else
{
langname = argv[optind++];
if (argc - optind > 0)
if (argc - optind > 0 && dbname == NULL)
dbname = argv[optind++];
}
}
......
......@@ -90,14 +90,24 @@ main(int argc, char *argv[])
}
}
/*
* We set dbname from positional arguments if it is not
* already set by option arguments -d. If not doing
* listlangs, positional dbname must follow positional
* langname.
*/
if (argc - optind > 0)
{
if (listlangs)
dbname = argv[optind++];
{
if (dbname == NULL)
dbname = argv[optind++];
}
else
{
langname = argv[optind++];
if (argc - optind > 0)
if (argc - optind > 0 && dbname == NULL)
dbname = argv[optind++];
}
}
......
......@@ -122,17 +122,22 @@ main(int argc, char *argv[])
}
}
switch (argc - optind)
/*
* Non-option argument specifies database name
* as long as it wasn't already specified with -d / --dbname
*/
if (optind < argc && dbname == NULL)
{
case 0:
break;
case 1:
dbname = argv[optind];
break;
default:
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"), progname, argv[optind + 1]);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
dbname = argv[optind];
optind++;
}
if (optind < argc)
{
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
progname, argv[optind + 1]);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
}
setup_cancel_handler();
......
......@@ -135,18 +135,23 @@ main(int argc, char *argv[])
}
}
switch (argc - optind)
/*
* Non-option argument specifies database name
* as long as it wasn't already specified with -d / --dbname
*/
if (optind < argc && dbname == NULL)
{
case 0:
break;
case 1:
dbname = argv[optind];
break;
default:
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
progname, argv[optind + 1]);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
dbname = argv[optind];
optind++;
}
if (optind < argc)
{
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
progname, argv[optind + 1]);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
}
if (analyze_only)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment