@@ -39,6 +39,7 @@ main(int argc, char *argv[])
39
39
{"no-login" , no_argument , NULL , 'L' },
40
40
{"replication" , no_argument , NULL , 1 },
41
41
{"no-replication" , no_argument , NULL , 2 },
42
+ {"interactive" , no_argument , NULL , 3 },
42
43
/* adduser is obsolete, undocumented spelling of superuser */
43
44
{"adduser" , no_argument , NULL , 'a' },
44
45
{"no-adduser" , no_argument , NULL , 'A' },
@@ -52,12 +53,13 @@ main(int argc, char *argv[])
52
53
const char * progname ;
53
54
int optindex ;
54
55
int c ;
55
- char * newuser = NULL ;
56
+ const char * newuser = NULL ;
56
57
char * host = NULL ;
57
58
char * port = NULL ;
58
59
char * username = NULL ;
59
60
enum trivalue prompt_password = TRI_DEFAULT ;
60
61
bool echo = false;
62
+ bool interactive = false;
61
63
char * conn_limit = NULL ;
62
64
bool pwprompt = false;
63
65
char * newpassword = NULL ;
@@ -154,6 +156,9 @@ main(int argc, char *argv[])
154
156
case 2 :
155
157
replication = TRI_NO ;
156
158
break ;
159
+ case 3 :
160
+ interactive = true;
161
+ break ;
157
162
default :
158
163
fprintf (stderr , _ ("Try \"%s --help\" for more information.\n" ), progname );
159
164
exit (1 );
@@ -175,7 +180,17 @@ main(int argc, char *argv[])
175
180
}
176
181
177
182
if (newuser == NULL )
178
- newuser = simple_prompt ("Enter name of role to add: " , 128 , true);
183
+ {
184
+ if (interactive )
185
+ newuser = simple_prompt ("Enter name of role to add: " , 128 , true);
186
+ else
187
+ {
188
+ if (getenv ("PGUSER" ))
189
+ newuser = getenv ("PGUSER" );
190
+ else
191
+ newuser = get_user_name (progname );
192
+ }
193
+ }
179
194
180
195
if (pwprompt )
181
196
{
@@ -195,7 +210,7 @@ main(int argc, char *argv[])
195
210
196
211
if (superuser == 0 )
197
212
{
198
- if (yesno_prompt ("Shall the new role be a superuser?" ))
213
+ if (interactive && yesno_prompt ("Shall the new role be a superuser?" ))
199
214
superuser = TRI_YES ;
200
215
else
201
216
superuser = TRI_NO ;
@@ -210,15 +225,15 @@ main(int argc, char *argv[])
210
225
211
226
if (createdb == 0 )
212
227
{
213
- if (yesno_prompt ("Shall the new role be allowed to create databases?" ))
228
+ if (interactive && yesno_prompt ("Shall the new role be allowed to create databases?" ))
214
229
createdb = TRI_YES ;
215
230
else
216
231
createdb = TRI_NO ;
217
232
}
218
233
219
234
if (createrole == 0 )
220
235
{
221
- if (yesno_prompt ("Shall the new role be allowed to create more new roles?" ))
236
+ if (interactive && yesno_prompt ("Shall the new role be allowed to create more new roles?" ))
222
237
createrole = TRI_YES ;
223
238
else
224
239
createrole = TRI_NO ;
@@ -316,7 +331,7 @@ help(const char *progname)
316
331
printf (_ ("\nOptions:\n" ));
317
332
printf (_ (" -c, --connection-limit=N connection limit for role (default: no limit)\n" ));
318
333
printf (_ (" -d, --createdb role can create new databases\n" ));
319
- printf (_ (" -D, --no-createdb role cannot create databases\n" ));
334
+ printf (_ (" -D, --no-createdb role cannot create databases (default) \n" ));
320
335
printf (_ (" -e, --echo show the commands being sent to the server\n" ));
321
336
printf (_ (" -E, --encrypted encrypt stored password\n" ));
322
337
printf (_ (" -i, --inherit role inherits privileges of roles it is a\n"
@@ -327,9 +342,11 @@ help(const char *progname)
327
342
printf (_ (" -N, --unencrypted do not encrypt stored password\n" ));
328
343
printf (_ (" -P, --pwprompt assign a password to new role\n" ));
329
344
printf (_ (" -r, --createrole role can create new roles\n" ));
330
- printf (_ (" -R, --no-createrole role cannot create roles\n" ));
345
+ printf (_ (" -R, --no-createrole role cannot create roles (default) \n" ));
331
346
printf (_ (" -s, --superuser role will be superuser\n" ));
332
- printf (_ (" -S, --no-superuser role will not be superuser\n" ));
347
+ printf (_ (" -S, --no-superuser role will not be superuser (default)\n" ));
348
+ printf (_ (" --interactive prompt for missing role name and attributes rather\n"
349
+ " than using defaults\n" ));
333
350
printf (_ (" --replication role can initiate replication\n" ));
334
351
printf (_ (" --no-replication role cannot initiate replication\n" ));
335
352
printf (_ (" --help show this help, then exit\n" ));
@@ -340,7 +357,5 @@ help(const char *progname)
340
357
printf (_ (" -U, --username=USERNAME user name to connect as (not the one to create)\n" ));
341
358
printf (_ (" -w, --no-password never prompt for password\n" ));
342
359
printf (_ (" -W, --password force password prompt\n" ));
343
- printf (_ ("\nIf one of -d, -D, -r, -R, -s, -S, and ROLENAME is not specified, you will\n"
344
- "be prompted interactively.\n" ));
345
360
printf (_ ("\nReport bugs to <pgsql-bugs@postgresql.org>.\n" ));
346
361
}
0 commit comments