createuser: Cleanup and fix internal option ordering

This utility supports 23 options that are not really ordered in the
code, making the addition of new things more complicated than necessary.
This cleanup is in preparation for a patch to add even more options.

Discussion: https://postgr.es/m/69a9851035cf0f0477bcc5d742b031a3@oss.nttdata.com
This commit is contained in:
Michael Paquier 2022-07-13 11:29:02 +09:00
parent 4cc832f94a
commit 50e4c280f0

View File

@ -28,29 +28,29 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
static struct option long_options[] = { static struct option long_options[] = {
{"host", required_argument, NULL, 'h'}, {"connection-limit", required_argument, NULL, 'c'},
{"port", required_argument, NULL, 'p'},
{"username", required_argument, NULL, 'U'},
{"role", required_argument, NULL, 'g'},
{"no-password", no_argument, NULL, 'w'},
{"password", no_argument, NULL, 'W'},
{"echo", no_argument, NULL, 'e'},
{"createdb", no_argument, NULL, 'd'}, {"createdb", no_argument, NULL, 'd'},
{"no-createdb", no_argument, NULL, 'D'}, {"no-createdb", no_argument, NULL, 'D'},
{"superuser", no_argument, NULL, 's'}, {"echo", no_argument, NULL, 'e'},
{"no-superuser", no_argument, NULL, 'S'}, {"encrypted", no_argument, NULL, 'E'},
{"createrole", no_argument, NULL, 'r'}, {"role", required_argument, NULL, 'g'},
{"no-createrole", no_argument, NULL, 'R'}, {"host", required_argument, NULL, 'h'},
{"inherit", no_argument, NULL, 'i'}, {"inherit", no_argument, NULL, 'i'},
{"no-inherit", no_argument, NULL, 'I'}, {"no-inherit", no_argument, NULL, 'I'},
{"login", no_argument, NULL, 'l'}, {"login", no_argument, NULL, 'l'},
{"no-login", no_argument, NULL, 'L'}, {"no-login", no_argument, NULL, 'L'},
{"port", required_argument, NULL, 'p'},
{"pwprompt", no_argument, NULL, 'P'},
{"createrole", no_argument, NULL, 'r'},
{"no-createrole", no_argument, NULL, 'R'},
{"superuser", no_argument, NULL, 's'},
{"no-superuser", no_argument, NULL, 'S'},
{"username", required_argument, NULL, 'U'},
{"no-password", no_argument, NULL, 'w'},
{"password", no_argument, NULL, 'W'},
{"replication", no_argument, NULL, 1}, {"replication", no_argument, NULL, 1},
{"no-replication", no_argument, NULL, 2}, {"no-replication", no_argument, NULL, 2},
{"interactive", no_argument, NULL, 3}, {"interactive", no_argument, NULL, 3},
{"connection-limit", required_argument, NULL, 'c'},
{"pwprompt", no_argument, NULL, 'P'},
{"encrypted", no_argument, NULL, 'E'},
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
}; };
@ -89,31 +89,15 @@ main(int argc, char *argv[])
handle_help_version_opts(argc, argv, "createuser", help); handle_help_version_opts(argc, argv, "createuser", help);
while ((c = getopt_long(argc, argv, "h:p:U:g:wWedDsSrRiIlLc:PE", while ((c = getopt_long(argc, argv, "c:dDeEg:h:iIlLp:PrRsSU:wW",
long_options, &optindex)) != -1) long_options, &optindex)) != -1)
{ {
switch (c) switch (c)
{ {
case 'h': case 'c':
host = pg_strdup(optarg); if (!option_parse_int(optarg, "-c/--connection-limit",
break; -1, INT_MAX, &conn_limit))
case 'p': exit(1);
port = pg_strdup(optarg);
break;
case 'U':
username = pg_strdup(optarg);
break;
case 'g':
simple_string_list_append(&roles, optarg);
break;
case 'w':
prompt_password = TRI_NO;
break;
case 'W':
prompt_password = TRI_YES;
break;
case 'e':
echo = true;
break; break;
case 'd': case 'd':
createdb = TRI_YES; createdb = TRI_YES;
@ -121,17 +105,17 @@ main(int argc, char *argv[])
case 'D': case 'D':
createdb = TRI_NO; createdb = TRI_NO;
break; break;
case 's': case 'e':
superuser = TRI_YES; echo = true;
break; break;
case 'S': case 'E':
superuser = TRI_NO; /* no-op, accepted for backward compatibility */
break; break;
case 'r': case 'g':
createrole = TRI_YES; simple_string_list_append(&roles, optarg);
break; break;
case 'R': case 'h':
createrole = TRI_NO; host = pg_strdup(optarg);
break; break;
case 'i': case 'i':
inherit = TRI_YES; inherit = TRI_YES;
@ -145,16 +129,32 @@ main(int argc, char *argv[])
case 'L': case 'L':
login = TRI_NO; login = TRI_NO;
break; break;
case 'c': case 'p':
if (!option_parse_int(optarg, "-c/--connection-limit", port = pg_strdup(optarg);
-1, INT_MAX, &conn_limit))
exit(1);
break; break;
case 'P': case 'P':
pwprompt = true; pwprompt = true;
break; break;
case 'E': case 'r':
/* no-op, accepted for backward compatibility */ createrole = TRI_YES;
break;
case 'R':
createrole = TRI_NO;
break;
case 's':
superuser = TRI_YES;
break;
case 'S':
superuser = TRI_NO;
break;
case 'U':
username = pg_strdup(optarg);
break;
case 'w':
prompt_password = TRI_NO;
break;
case 'W':
prompt_password = TRI_YES;
break; break;
case 1: case 1:
replication = TRI_YES; replication = TRI_YES;