@@ -103,7 +103,6 @@ pgBackup current;
103103ProbackupSubcmd backup_subcmd = NO_CMD ;
104104
105105static bool help_opt = false;
106- static bool version_opt = false;
107106
108107static void opt_backup_mode (pgut_option * opt , const char * arg );
109108static void opt_log_level_console (pgut_option * opt , const char * arg );
@@ -117,7 +116,6 @@ static pgut_option options[] =
117116{
118117 /* directory options */
119118 { 'b' , 1 , "help" , & help_opt , SOURCE_CMDLINE },
120- { 'b' , 'V' , "version" , & version_opt , SOURCE_CMDLINE },
121119 { 's' , 'D' , "pgdata" , & pgdata , SOURCE_CMDLINE },
122120 { 's' , 'B' , "backup-path" , & backup_path , SOURCE_CMDLINE },
123121 /* common options */
@@ -196,14 +194,12 @@ static pgut_option options[] =
196194int
197195main (int argc , char * argv [])
198196{
199- char * command = NULL ;
197+ char * command = NULL ,
198+ * command_name ;
200199 char path [MAXPGPATH ];
201200 /* Check if backup_path is directory. */
202201 struct stat stat_buf ;
203202 int rc ;
204- int i ,
205- len = 0 ,
206- allocated = 0 ;
207203
208204 /* initialize configuration */
209205 pgBackup_init (& current );
@@ -216,97 +212,104 @@ main(int argc, char *argv[])
216212 */
217213 main_tid = pthread_self ();
218214
219- /*
220- * Make command string before getopt_long() will call. It permutes the
221- * content of argv.
222- */
223- allocated = sizeof (char ) * MAXPGPATH ;
224- command = (char * ) palloc (allocated );
225-
226- for (i = 0 ; i < argc ; i ++ )
215+ /* Parse subcommands and non-subcommand options */
216+ if (argc > 1 )
227217 {
228- int arglen = strlen (argv [i ]);
229-
230- if (arglen + len > allocated )
218+ if (strcmp (argv [1 ], "archive-push" ) == 0 )
219+ backup_subcmd = ARCHIVE_PUSH_CMD ;
220+ else if (strcmp (argv [1 ], "archive-get" ) == 0 )
221+ backup_subcmd = ARCHIVE_GET_CMD ;
222+ else if (strcmp (argv [1 ], "add-instance" ) == 0 )
223+ backup_subcmd = ADD_INSTANCE_CMD ;
224+ else if (strcmp (argv [1 ], "del-instance" ) == 0 )
225+ backup_subcmd = DELETE_INSTANCE_CMD ;
226+ else if (strcmp (argv [1 ], "init" ) == 0 )
227+ backup_subcmd = INIT_CMD ;
228+ else if (strcmp (argv [1 ], "backup" ) == 0 )
229+ backup_subcmd = BACKUP_CMD ;
230+ else if (strcmp (argv [1 ], "restore" ) == 0 )
231+ backup_subcmd = RESTORE_CMD ;
232+ else if (strcmp (argv [1 ], "validate" ) == 0 )
233+ backup_subcmd = VALIDATE_CMD ;
234+ else if (strcmp (argv [1 ], "show" ) == 0 )
235+ backup_subcmd = SHOW_CMD ;
236+ else if (strcmp (argv [1 ], "delete" ) == 0 )
237+ backup_subcmd = DELETE_CMD ;
238+ else if (strcmp (argv [1 ], "set-config" ) == 0 )
239+ backup_subcmd = SET_CONFIG_CMD ;
240+ else if (strcmp (argv [1 ], "show-config" ) == 0 )
241+ backup_subcmd = SHOW_CONFIG_CMD ;
242+ else if (strcmp (argv [1 ], "--help" ) == 0 ||
243+ strcmp (argv [1 ], "-?" ) == 0 ||
244+ strcmp (argv [1 ], "help" ) == 0 )
231245 {
232- allocated *= 2 ;
233- command = repalloc (command , allocated );
246+ if (argc > 2 )
247+ help_command (argv [2 ]);
248+ else
249+ help_pg_probackup ();
234250 }
235-
236- strncpy (command + len , argv [i ], arglen );
237- len += arglen ;
238- command [len ++ ] = ' ' ;
251+ else if (strcmp (argv [1 ], "--version" ) == 0
252+ || strcmp (argv [1 ], "version" ) == 0
253+ || strcmp (argv [1 ], "-V" ) == 0 )
254+ {
255+ #ifdef PGPRO_VERSION
256+ fprintf (stderr , "%s %s (Postgres Pro %s %s)\n" ,
257+ PROGRAM_NAME , PROGRAM_VERSION ,
258+ PGPRO_VERSION , PGPRO_EDITION );
259+ #else
260+ fprintf (stderr , "%s %s (PostgreSQL %s)\n" ,
261+ PROGRAM_NAME , PROGRAM_VERSION , PG_VERSION );
262+ #endif
263+ exit (0 );
264+ }
265+ else
266+ elog (ERROR , "Unknown subcommand \"%s\"" , argv [1 ]);
239267 }
240268
241- command [len ] = '\0' ;
269+ if (backup_subcmd == NO_CMD )
270+ elog (ERROR , "No subcommand specified" );
242271
243- /* Parse command line arguments */
244- optind = 1 ;
245- /* process command-line options */
246- while (optind < argc )
272+ /*
273+ * Make command string before getopt_long() will call. It permutes the
274+ * content of argv.
275+ */
276+ command_name = pstrdup (argv [1 ]);
277+ if (backup_subcmd == BACKUP_CMD ||
278+ backup_subcmd == RESTORE_CMD ||
279+ backup_subcmd == VALIDATE_CMD ||
280+ backup_subcmd == DELETE_CMD )
247281 {
248- pgut_getopt (argc , argv , options );
249- /* Process a command */
250- if (optind < argc )
282+ int i ,
283+ len = 0 ,
284+ allocated = 0 ;
285+
286+ allocated = sizeof (char ) * MAXPGPATH ;
287+ command = (char * ) palloc (allocated );
288+
289+ for (i = 0 ; i < argc ; i ++ )
251290 {
252- if (strcmp (argv [optind ], "archive-push" ) == 0 )
253- backup_subcmd = ARCHIVE_PUSH_CMD ;
254- else if (strcmp (argv [optind ], "archive-get" ) == 0 )
255- backup_subcmd = ARCHIVE_GET_CMD ;
256- else if (strcmp (argv [optind ], "add-instance" ) == 0 )
257- backup_subcmd = ADD_INSTANCE_CMD ;
258- else if (strcmp (argv [optind ], "del-instance" ) == 0 )
259- backup_subcmd = DELETE_INSTANCE_CMD ;
260- else if (strcmp (argv [optind ], "init" ) == 0 )
261- backup_subcmd = INIT_CMD ;
262- else if (strcmp (argv [optind ], "backup" ) == 0 )
263- backup_subcmd = BACKUP_CMD ;
264- else if (strcmp (argv [optind ], "restore" ) == 0 )
265- backup_subcmd = RESTORE_CMD ;
266- else if (strcmp (argv [optind ], "validate" ) == 0 )
267- backup_subcmd = VALIDATE_CMD ;
268- else if (strcmp (argv [optind ], "show" ) == 0 )
269- backup_subcmd = SHOW_CMD ;
270- else if (strcmp (argv [optind ], "delete" ) == 0 )
271- backup_subcmd = DELETE_CMD ;
272- else if (strcmp (argv [optind ], "set-config" ) == 0 )
273- backup_subcmd = SET_CONFIG_CMD ;
274- else if (strcmp (argv [optind ], "show-config" ) == 0 )
275- backup_subcmd = SHOW_CONFIG_CMD ;
276- else if (strcmp (argv [optind ], "help" ) == 0 )
291+ int arglen = strlen (argv [i ]);
292+
293+ if (arglen + len > allocated )
277294 {
278- optind ++ ;
279- help_opt = true ;
295+ allocated *= 2 ;
296+ command = repalloc ( command , allocated ) ;
280297 }
281- else if (strcmp (argv [optind ], "version" ) == 0 )
282- version_opt = true;
283- else
284- elog (ERROR , "Unknown subcommand \"%s\"" , argv [optind ]);
285- optind ++ ;
298+
299+ strncpy (command + len , argv [i ], arglen );
300+ len += arglen ;
301+ command [len ++ ] = ' ' ;
286302 }
303+
304+ command [len ] = '\0' ;
287305 }
288306
307+ optind += 1 ;
308+ /* Parse command line arguments */
309+ pgut_getopt (argc , argv , options );
310+
289311 if (help_opt )
290- {
291- if (argc - optind < 1 )
292- help_pg_probackup ();
293- else
294- help_command (argv [optind ]);
295- }
296- else if (version_opt )
297- {
298- #ifdef PGPRO_VERSION
299- fprintf (stderr , "%s %s (Postgres Pro %s %s)\n" ,
300- PROGRAM_NAME , PROGRAM_VERSION ,
301- PGPRO_VERSION , PGPRO_EDITION );
302- #else
303- fprintf (stderr , "%s %s (PostgreSQL %s)\n" ,
304- PROGRAM_NAME , PROGRAM_VERSION , PG_VERSION );
305- #endif
306- return 0 ;
307- }
308- else if (backup_subcmd == NO_CMD )
309- elog (ERROR , "No subcommand specified" );
312+ help_command (command_name );
310313
311314 /* backup_path is required for all pg_probackup commands except help */
312315 if (backup_path == NULL )
0 commit comments