Sort the output of --help mostly alphabetical, make it align better, make
[PostgreSQL.git] / src / bin / scripts / dropuser.c
blobbfdc642d0bdb3d64b88295794f44c2f21fc315dd
1 /*-------------------------------------------------------------------------
3 * dropuser
5 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
8 * $PostgreSQL$
10 *-------------------------------------------------------------------------
13 #include "postgres_fe.h"
14 #include "common.h"
15 #include "dumputils.h"
18 static void help(const char *progname);
21 int
22 main(int argc, char *argv[])
24 static struct option long_options[] = {
25 {"host", required_argument, NULL, 'h'},
26 {"port", required_argument, NULL, 'p'},
27 {"username", required_argument, NULL, 'U'},
28 {"password", no_argument, NULL, 'W'},
29 {"echo", no_argument, NULL, 'e'},
30 {"quiet", no_argument, NULL, 'q'},
31 {"interactive", no_argument, NULL, 'i'},
32 {NULL, 0, NULL, 0}
35 const char *progname;
36 int optindex;
37 int c;
39 char *dropuser = NULL;
40 char *host = NULL;
41 char *port = NULL;
42 char *username = NULL;
43 bool password = false;
44 bool echo = false;
45 bool interactive = false;
47 PQExpBufferData sql;
49 PGconn *conn;
50 PGresult *result;
52 progname = get_progname(argv[0]);
53 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
55 handle_help_version_opts(argc, argv, "dropuser", help);
57 while ((c = getopt_long(argc, argv, "h:p:U:Weqi", long_options, &optindex)) != -1)
59 switch (c)
61 case 'h':
62 host = optarg;
63 break;
64 case 'p':
65 port = optarg;
66 break;
67 case 'U':
68 username = optarg;
69 break;
70 case 'W':
71 password = true;
72 break;
73 case 'e':
74 echo = true;
75 break;
76 case 'q':
77 /* obsolete; remove in 8.4 */
78 break;
79 case 'i':
80 interactive = true;
81 break;
82 default:
83 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
84 exit(1);
88 switch (argc - optind)
90 case 0:
91 break;
92 case 1:
93 dropuser = argv[optind];
94 break;
95 default:
96 fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
97 progname, argv[optind + 1]);
98 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
99 exit(1);
102 if (dropuser == NULL)
103 dropuser = simple_prompt("Enter name of role to drop: ", 128, true);
105 if (interactive)
107 printf(_("Role \"%s\" will be permanently removed.\n"), dropuser);
108 if (!yesno_prompt("Are you sure?"))
109 exit(0);
112 initPQExpBuffer(&sql);
113 appendPQExpBuffer(&sql, "DROP ROLE %s;\n", fmtId(dropuser));
115 conn = connectDatabase("postgres", host, port, username, password, progname);
117 if (echo)
118 printf("%s", sql.data);
119 result = PQexec(conn, sql.data);
121 if (PQresultStatus(result) != PGRES_COMMAND_OK)
123 fprintf(stderr, _("%s: removal of role \"%s\" failed: %s"),
124 progname, dropuser, PQerrorMessage(conn));
125 PQfinish(conn);
126 exit(1);
129 PQclear(result);
130 PQfinish(conn);
131 exit(0);
135 static void
136 help(const char *progname)
138 printf(_("%s removes a PostgreSQL role.\n\n"), progname);
139 printf(_("Usage:\n"));
140 printf(_(" %s [OPTION]... [ROLENAME]\n"), progname);
141 printf(_("\nOptions:\n"));
142 printf(_(" -e, --echo show the commands being sent to the server\n"));
143 printf(_(" -i, --interactive prompt before deleting anything\n"));
144 printf(_(" --help show this help, then exit\n"));
145 printf(_(" --version output version information, then exit\n"));
146 printf(_("\nConnection options:\n"));
147 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
148 printf(_(" -p, --port=PORT database server port\n"));
149 printf(_(" -U, --username=USERNAME user name to connect as (not the one to drop)\n"));
150 printf(_(" -W, --password force password prompt\n"));
151 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));