Skip to content

Commit 244c2d7

Browse files
committed
Fix issues when command is absent and different usage text when command is invalid and when option is invalid
1 parent 2c792d0 commit 244c2d7

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

main/gridinit_cmd.c

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -658,53 +658,53 @@ struct command_s {
658658
static void
659659
help(void)
660660
{
661-
close(2);
662-
g_print("\n COMMANDS:\n");
663-
g_print(" status* : Displays the status of the given processes or groups\n");
664-
g_print(" start : Starts the given processes or groups, even if broken\n");
665-
g_print(" kill : Stops the given processes or groups, they won't be automatically\n");
666-
g_print(" restarted even after a configuration reload\n");
667-
g_print(" stop : Calls 'kill' until the children exit\n");
668-
g_print(" restart : Restarts the given processes or groups\n");
669-
g_print(" reload : Reloads the configuration, stopping obsolete processes, starting\n");
670-
g_print(" the newly discovered. Broken or stopped processes are not restarted\n");
671-
g_print(" repair : Removes the broken flag set on a process. Start must be called to\n");
672-
g_print(" restart the process.\n");
673-
g_print("with ID the key of a process, or '@GROUP', with GROUP the name of a process\n");
674-
g_print("group\n");
661+
675662
close(1);
676663
exit(0);
677664
}
678665

666+
static void
667+
usage(void)
668+
{
669+
GOptionContext *context;
670+
gchar description[] =
671+
"\n COMMANDS:\n"
672+
" status* : Displays the status of the given processes or groups\n"
673+
" start : Starts the given processes or groups, even if broken\n"
674+
" kill : Stops the given processes or groups, they won't be automatically\n"
675+
" restarted even after a configuration reload\n"
676+
" stop : Calls 'kill' until the children exit\n"
677+
" restart : Restarts the given processes or groups\n"
678+
" reload : Reloads the configuration, stopping obsolete processes, starting\n"
679+
" the newly discovered. Broken or stopped processes are not restarted\n"
680+
" repair : Removes the broken flag set on a process. Start must be called to\n"
681+
" restart the process.\n"
682+
"with ID the key of a process, or '@GROUP', with GROUP the name of a process\n"
683+
"group\n";
684+
context = g_option_context_new("(status{,2,3}|start|stop|reload|repair) [ID...]\n");
685+
g_option_context_add_main_entries(context, entries, NULL);
686+
g_option_context_set_summary(context, description);
687+
gchar *usage = g_option_context_get_help (context, TRUE, NULL);
688+
g_print("%s", usage);
689+
}
690+
679691
static int
680692
main_options(int argc, char **args)
681693
{
682694
GError *error = NULL;
683695
GOptionContext *context;
684696

685697
sock_path = g_strdup(GRIDINIT_SOCK_PATH);
686-
687698
context = g_option_context_new("(status{,2,3}|start|stop|reload|repair) [ID...]\n");
688699
g_option_context_add_main_entries(context, entries, NULL);
689-
if (!g_option_context_parse(context, &argc, &args, &error)) {
700+
if (!g_option_context_parse(context, &argc, &args, &error)) {
690701
g_print("option parsing failed: %s\n", error->message);
691-
gchar *usage = g_option_context_get_help (context, TRUE, NULL);
692-
g_print("%s", usage);
693-
help();
694702
}
695703

696704
return argc;
697705
}
698706

699-
static void
700-
usage(void)
701-
{
702-
GOptionContext *context;
703-
context = g_option_context_new("(status{,2,3}|start|stop|reload|repair) [ID...]\n");
704-
gchar *usage = g_option_context_get_help (context, TRUE, NULL);
705-
g_print("%s", usage);
706-
help();
707-
}
707+
708708
int
709709
main(int argc, char ** args)
710710
{
@@ -724,7 +724,12 @@ main(int argc, char ** args)
724724
close(2);
725725
return 0;
726726
}
727-
727+
if (!args[opt_index]) {
728+
usage();
729+
close(1);
730+
close(2);
731+
return 1;
732+
}
728733
for (cmd=COMMANDS; cmd->name ;cmd++) {
729734
if (0 == g_ascii_strcasecmp(cmd->name, args[opt_index])) {
730735
int rc = cmd->action(argc-(opt_index+1), args+(opt_index+1));
@@ -733,7 +738,7 @@ main(int argc, char ** args)
733738
return rc;
734739
}
735740
}
736-
741+
usage();
737742
close(1);
738743
close(2);
739744
return 1;

0 commit comments

Comments
 (0)