Make it easier to print the help for select options.
authorAndre Noll <maan@systemlinux.org>
Sat, 1 Nov 2008 21:06:16 +0000 (22:06 +0100)
committerAndre Noll <maan@systemlinux.org>
Sat, 1 Nov 2008 21:06:16 +0000 (22:06 +0100)
It was always a bit awkward to print the help for the select options
because one had to specify a valid mode (-C -S or -I) and the required
-d option just to make gengetopt not bail out early.

Fix this flaw by not using gengetopt's internal help.

Makefile
adu.c
adu.h
interactive.c

index 06d4132..187dd88 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -34,7 +34,7 @@ cmdline.o: cmdline.c cmdline.h
        $(CC) -c $(CPPFLAGS) $<
 
 cmdline.c cmdline.h: adu.ggo
        $(CC) -c $(CPPFLAGS) $<
 
 cmdline.c cmdline.h: adu.ggo
-       gengetopt --conf-parser < $<
+       gengetopt --no-handle-error --no-handle-help --conf-parser < $<
 
 
 %.o: %.c Makefile
 
 
 %.o: %.c Makefile
diff --git a/adu.c b/adu.c
index 757054d..8ce7317 100644 (file)
--- a/adu.c
+++ b/adu.c
@@ -468,18 +468,55 @@ static int check_args(void)
        return 1;
 }
 
        return 1;
 }
 
+static int print_complete_help_and_die(void)
+{
+       const char **line;
+       select_cmdline_parser_init(&select_conf);
+
+       if (conf.help_given)
+               line = gengetopt_args_info_help;
+       else
+               line = gengetopt_args_info_detailed_help;
+
+       printf("adu command line options:\n");
+       printf("=========================\n");
+       for (; *line; line++)
+               printf("%s\n", *line);
+       if (conf.help_given)
+               line = select_args_info_help;
+       else
+               line  = select_args_info_detailed_help;
+       printf("select options:\n");
+       printf("===============\n");
+       for (; *line; line++)
+               printf("%s\n", *line);
+       printf("interactive commands:\n");
+       printf("=====================\n");
+       print_interactive_help();
+       exit(EXIT_FAILURE);
+}
+
 int main(int argc, char **argv)
 {
        int ret;
        struct cmdline_parser_params params = {
                .override = 0,
                .initialize = 1,
 int main(int argc, char **argv)
 {
        int ret;
        struct cmdline_parser_params params = {
                .override = 0,
                .initialize = 1,
-               .check_required = 1,
-               .check_ambiguity = 1,
-               .print_errors = 1
+               .check_required = 0,
+               .check_ambiguity = 0,
+               .print_errors = 0
        };
        };
+       /* ignore errors and print complete help if --help was given */
+       cmdline_parser_ext(argc, argv, &conf, &params);
+       if (conf.help_given || conf.detailed_help_given)
+               print_complete_help_and_die();
+       params.check_required = 1;
+       params.check_ambiguity = 1;
+       params.print_errors = 1;
+       ret = cmdline_parser_ext(argc, argv, &conf, &params);
+       if (ret)
+               exit(EXIT_FAILURE);
 
 
-       cmdline_parser_ext(argc, argv, &conf, &params); /* aborts on errors */
        ret = check_args();
        if (ret < 0)
                goto out;
        ret = check_args();
        if (ret < 0)
                goto out;
diff --git a/adu.h b/adu.h
index 20cef2f..85ab63a 100644 (file)
--- a/adu.h
+++ b/adu.h
@@ -194,6 +194,9 @@ int for_each_admissible_user(int (*func)(struct user_info *, void *),
                void *data);
 void sort_hash_table(int (*comp)(const void *, const void *));
 
                void *data);
 void sort_hash_table(int (*comp)(const void *, const void *));
 
-/* create.h */
+/* create.c */
 int com_create(void);
 int com_create(void);
+
+/* interactive.c */
+void print_interactive_help(void);
 int com_interactive(void);
 int com_interactive(void);
index 036f7a8..93e4a36 100644 (file)
@@ -62,6 +62,11 @@ static int icom_help(__a_unused char *line)
        return 1;
 }
 
        return 1;
 }
 
+void print_interactive_help(void)
+{
+       icom_help(NULL);
+}
+
 static int icom_reset(__a_unused char *line)
 {
        select_cmdline_parser_init(&select_conf);
 static int icom_reset(__a_unused char *line)
 {
        select_cmdline_parser_init(&select_conf);