Skip to content

Commit cf8180c

Browse files
acornardmurlock
authored andcommitted
Update usage and add version option
1 parent 2d3eac0 commit cf8180c

File tree

2 files changed

+102
-123
lines changed

2 files changed

+102
-123
lines changed

main/gridinit.c

Lines changed: 36 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ static char *config_subdir = NULL;
9292
static char **groups_only_cli = NULL;
9393
static char **groups_only_cfg = NULL;
9494

95-
static volatile int flag_help = 0;
96-
static volatile int flag_quiet = 0;
97-
static volatile int flag_daemon = 0;
98-
static volatile int flag_running = ~0;
99-
static volatile int flag_check_socket = 0;
100-
static volatile int flag_more_verbose = 0;
95+
static volatile gboolean flag_quiet = FALSE;
96+
static volatile gboolean flag_daemon = FALSE;
97+
static volatile gboolean flag_running = TRUE;
98+
static volatile gboolean flag_check_socket = FALSE;
99+
static volatile gboolean flag_more_verbose = FALSE;
100+
static volatile gboolean flag_version = FALSE;
101101

102102
static volatile gint32 default_uid = -1;
103103
static volatile gint32 default_gid = -1;
@@ -110,6 +110,22 @@ static gboolean _cfg_reload(gboolean services_only, GError **err);
110110

111111
static void servers_ensure(void);
112112

113+
static GOptionEntry entries[] = {
114+
{"daemonize", 'd', 0, G_OPTION_ARG_NONE, (gboolean *)&flag_daemon,
115+
"Detaches then daemonizes the gridinit \n", NULL},
116+
{"group", 'g', 0, G_OPTION_ARG_STRING_ARRAY, &groups_only_cli,
117+
"limits the services loading to those belonging to the specified"
118+
"group. This option can be repeated\n", "GROUP"},
119+
{"quiet", 'q', 0, G_OPTION_ARG_NONE, (gboolean *)&flag_quiet,
120+
"quiet mode, suppress non-error output",NULL},
121+
{"version", 'V', 0, G_OPTION_ARG_NONE, (gboolean *)&flag_version,
122+
"Display the version of gridinit", NULL},
123+
{"verbose", 'v', 0, G_OPTION_ARG_NONE, (gboolean *)&flag_more_verbose,
124+
"verbose output mode", NULL},
125+
{"syslog", 's', 0, G_OPTION_ARG_STRING, &syslog_id,
126+
"enable logs using syslog with the given ID", "ID"},
127+
{NULL}
128+
};
113129

114130
/* ------------------------------------------------------------------------- */
115131

@@ -966,24 +982,6 @@ signals_clean(void)
966982

967983
/* Configuration ----------------------------------------------------------- */
968984

969-
static void
970-
main_usage(void)
971-
{
972-
if (flag_quiet)
973-
return;
974-
g_printerr("\n"
975-
"Usage: %s [OPTIONS] ... CONFIG_PATH [LOG4C_PATH]\n"
976-
" with OPTIONS:\n"
977-
" -d : Detaches then daemonizes the gridinit\n"
978-
" -h : displays this help section\n"
979-
" -g GROUP : limits the services loading to those belonging to\n"
980-
" the specified group. This option can be repeated.\n"
981-
" -q : quiet mode, suppress non-error output\n"
982-
" -v : verbose output mode.\n"
983-
" -s ID : enable logs using syslog with the given ID.\n"
984-
"\n", g_get_prgname());
985-
}
986-
987985
static gboolean
988986
_cfg_value_is_true(const gchar *val)
989987
{
@@ -1787,54 +1785,29 @@ logger_stderr(const gchar *log_domain, GLogLevelFlags log_level,
17871785
static void
17881786
__parse_options(int argc, char ** args)
17891787
{
1790-
int c;
17911788
GError *error_local = NULL;
1789+
GOptionContext *context;
17921790

1793-
while (-1 != (c = getopt(argc, args, "vqhdg:s:"))) {
1794-
switch (c) {
1795-
case 'd':
1796-
flag_daemon = ~0;
1797-
break;
1798-
case 'h':
1799-
flag_help = ~0;
1800-
break;
1801-
case 'g':
1802-
if (!optarg) {
1803-
g_printerr("Expected argument to the '-%c' option\n", c);
1804-
exit(1);
1805-
}
1806-
_str_set_array(TRUE, &groups_only_cli, optarg);
1807-
break;
1808-
case 's':
1809-
if (!optarg) {
1810-
g_printerr("Expected argument to the '-%c' option\n", c);
1811-
exit(1);
1812-
}
1813-
g_strlcpy(syslog_id, optarg, sizeof(syslog_id));
1814-
break;
1815-
case 'v':
1816-
logger_verbose_default();
1817-
break;
1818-
case 'q':
1819-
flag_quiet = ~0;
1820-
logger_init_level(GRID_LOGLVL_ERROR);
1821-
break;
1822-
default:
1823-
if (!flag_quiet)
1824-
g_printerr("Unexpected option : %c\n", c);
1825-
exit(1);
1826-
}
1791+
context = g_option_context_new(" CONFIG_PATH [LOG4C_PATH]");
1792+
g_option_context_add_main_entries(context, entries, NULL);
1793+
if (!g_option_context_parse(context, &argc, &args, &error_local)) {
1794+
g_print("option parsing failed: %s\n", error_local->message);
1795+
gchar *usage = g_option_context_get_help (context, TRUE, NULL);
1796+
g_print("%s", usage);
18271797
}
18281798

1829-
if (flag_help) {
1830-
main_usage();
1799+
if (flag_more_verbose)
1800+
logger_verbose_default();
1801+
1802+
if (flag_version) {
1803+
fprintf(stdout, "gridinit version: %s\n", API_VERSION);
18311804
exit(0);
18321805
return;
18331806
}
1834-
18351807
/* check for additionnal arguments */
18361808
if (optind >= argc) {
1837-
main_usage();
1809+
gchar *usage = g_option_context_get_help (context, TRUE, NULL);
1810+
g_print("%s", usage);
18381811
exit(1);
18391812
return;
18401813
}

main/gridinit_cmd.c

Lines changed: 66 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
4343
#define MINI 0
4444
#define MEDIUM 1
4545

46-
static gboolean flag_help = FALSE;
4746

48-
static gchar sock_path[1024];
47+
static gchar *sock_path;
4948
static gchar line[65536];
5049
static gboolean flag_color = FALSE;
51-
static gchar format[256];
52-
50+
static gchar *format;
51+
static gboolean flag_version = FALSE;
5352
#define BOOL(i) (i?1:0)
5453

5554
struct dump_as_is_arg_s {
@@ -117,6 +116,18 @@ static struct keyword_set_s KEYWORDS_COLOR = {
117116
"UP "
118117
};
119118

119+
static GOptionEntry entries[] = {
120+
{"color", 'c', 0, G_OPTION_ARG_NONE, &flag_color,
121+
"coloured display \n", NULL},
122+
{"sock-path", 'S', 0, G_OPTION_ARG_FILENAME, &sock_path,
123+
"explicit unix socket path\n", "SOCKET"},
124+
{"format", 'f', 0, G_OPTION_ARG_STRING, &format,
125+
"output result by given FORMAT","FORMAT"},
126+
{"version", 'v', 0, G_OPTION_ARG_NONE, &flag_version,
127+
"Display the version of gridinit_cmd", NULL},
128+
{NULL}
129+
};
130+
120131
static gint
121132
compare_child_info(gconstpointer p1, gconstpointer p2)
122133
{
@@ -553,7 +564,7 @@ command_kill(int argc, char **args)
553564
{
554565
struct dump_as_is_arg_s dump_args = {};
555566

556-
int rc = send_commandv(dump_as, &dump_args, "stop", argc, args);
567+
int rc = send_commandv(dump_as, &dump_args, "stop", argc, args);
557568
return !rc
558569
|| dump_args.count_errors != 0
559570
|| dump_args.count_success == 0;
@@ -641,75 +652,73 @@ struct command_s {
641652
{ NULL, NULL }
642653
};
643654

644-
static int
645-
main_options(int argc, char **args)
646-
{
647-
int opt;
648-
649-
g_strlcpy(sock_path, GRIDINIT_SOCK_PATH, sizeof(sock_path));
650-
651-
while ((opt = getopt(argc, args, "chf:S:")) != -1) {
652-
switch (opt) {
653-
case 'c':
654-
flag_color = TRUE;
655-
break;
656-
case 'S':
657-
if (optarg)
658-
g_strlcpy(sock_path, optarg, sizeof(sock_path));
659-
break;
660-
case 'h':
661-
flag_help = TRUE;
662-
break;
663-
case 'f':
664-
if (optarg)
665-
g_strlcpy(format, optarg, sizeof(format));
666-
break;
667-
}
668-
}
669-
670-
return optind;
671-
}
672-
673655
static void
674-
help(char **args)
656+
help(void)
675657
{
676658
close(2);
677-
g_print("Usage: %s [-h|-c|-f FORMAT|-S SOCK]... (status{,2,3}|start|stop|reload|repair) [ID...]\n", args[0]);
678-
g_print("\n OPTIONS:\n");
679-
g_print(" -c : coloured display\n");
680-
g_print(" -h : displays a little help section\n");
681-
g_print(" -S SOCK : explicit unix socket path\n");
682-
g_print(" -f FORMAT : output result by json\n");
683659
g_print("\n COMMANDS:\n");
684-
g_print(" status* : Displays the status of the given processes or groups\n");
685-
g_print(" start : Starts the given processes or groups, even if broken\n");
686-
g_print(" kill : Stops the given processes or groups, they won't be automatically\n");
687-
g_print(" restarted even after a configuration reload\n");
688-
g_print(" stop : Calls 'kill' until the children exit\n");
689-
g_print(" restart : Restarts the given processes or groups\n");
690-
g_print(" reload : Reloads the configuration, stopping obsolete processes, starting\n");
691-
g_print(" the newly discovered. Broken or stopped processes are not restarted\n");
692-
g_print(" repair : Removes the broken flag set on a process. Start must be called to\n");
693-
g_print(" restart the process.\n");
660+
g_print(" status* : Displays the status of the given processes or groups\n");
661+
g_print(" start : Starts the given processes or groups, even if broken\n");
662+
g_print(" kill : Stops the given processes or groups, they won't be automatically\n");
663+
g_print(" restarted even after a configuration reload\n");
664+
g_print(" stop : Calls 'kill' until the children exit\n");
665+
g_print(" restart : Restarts the given processes or groups\n");
666+
g_print(" reload : Reloads the configuration, stopping obsolete processes, starting\n");
667+
g_print(" the newly discovered. Broken or stopped processes are not restarted\n");
668+
g_print(" repair : Removes the broken flag set on a process. Start must be called to\n");
669+
g_print(" restart the process.\n");
694670
g_print("with ID the key of a process, or '@GROUP', with GROUP the name of a process\n");
695671
g_print("group\n");
696672
close(1);
697673
exit(0);
698674
}
699675

676+
static int
677+
main_options(int argc, char **args)
678+
{
679+
GError *error = NULL;
680+
GOptionContext *context;
681+
682+
context = g_option_context_new("(status{,2,3}|start|stop|reload|repair) [ID...]\n");
683+
g_option_context_add_main_entries(context, entries, NULL);
684+
if (!g_option_context_parse(context, &argc, &args, &error)) {
685+
g_print("option parsing failed: %s\n", error->message);
686+
gchar *usage = g_option_context_get_help (context, TRUE, NULL);
687+
g_print("%s", usage);
688+
help();
689+
}
690+
691+
return argc;
692+
}
693+
694+
static void
695+
usage(void)
696+
{
697+
GOptionContext *context;
698+
context = g_option_context_new("(status{,2,3}|start|stop|reload|repair) [ID...]\n");
699+
gchar *usage = g_option_context_get_help (context, TRUE, NULL);
700+
g_print("%s", usage);
701+
help();
702+
}
700703
int
701704
main(int argc, char ** args)
702705
{
703706
struct command_s *cmd;
704-
int opt_index;
707+
int opt_index = 1;
708+
709+
if (argc == 1)
710+
usage();
705711

706712
close(0);
707-
opt_index = main_options(argc, args);
708713

709-
if (flag_help)
710-
help(args);
711-
if (opt_index >= argc)
712-
help(args);
714+
argc = main_options(argc, args);
715+
716+
if (flag_version) {
717+
fprintf(stdout, "gridinit_cmd version: %s\n", API_VERSION);
718+
close(1);
719+
close(2);
720+
return 0;
721+
}
713722

714723
for (cmd=COMMANDS; cmd->name ;cmd++) {
715724
if (0 == g_ascii_strcasecmp(cmd->name, args[opt_index])) {
@@ -720,9 +729,6 @@ main(int argc, char ** args)
720729
}
721730
}
722731

723-
fprintf(stderr, "\n*** Invalid command %s ***\n\n", args[opt_index]);
724-
help(args);
725-
726732
close(1);
727733
close(2);
728734
return 1;

0 commit comments

Comments
 (0)