]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-master, doveadm: Progress top level --help option
authorKarl Fleischmann <karl.fleischmann@open-xchange.com>
Thu, 22 Jun 2023 09:58:31 +0000 (11:58 +0200)
committerKarl Fleischmann <karl.fleischmann@open-xchange.com>
Mon, 10 Jul 2023 10:10:42 +0000 (12:10 +0200)
Print usage to stdout and exit successfully if requested via "doveadm
help" or "doveadm --help".

src/doveadm/doveadm.c
src/lib-master/master-service.c
src/lib-master/master-service.h

index 7628fee456cb318aabb8346af28c7a3732e7fb1a..ead2e1a94a30f4159c9d4b80113b7d7b0247893d 100644 (file)
@@ -18,6 +18,7 @@
 #include "doveadm-dsync.h"
 #include "doveadm.h"
 
+#include <getopt.h>
 #include <unistd.h>
 
 const struct doveadm_print_vfuncs *doveadm_print_vfuncs_all[] = {
@@ -147,7 +148,7 @@ static void cmd_help(struct doveadm_cmd_context *cctx)
        const char *cmd, *man_argv[3];
 
        if (!doveadm_cmd_param_str(cctx, "cmd", &cmd))
-               usage();
+               usage_prefix(stdout, "", EX_OK);
 
        env_put("MANPATH", MANDIR);
        man_argv[0] = "man";
@@ -270,13 +271,24 @@ int main(int argc, char *argv[])
           others just accept -+ option. */
        master_service = master_service_init("doveadm", service_flags,
                                             &argc, &argv, "+Df:hv");
+       const struct option longopts[] = {
+               master_service_helpopt,
+               {NULL, 0, NULL, 0},
+       };
+       master_service_register_long_options(master_service, longopts);
        struct doveadm_cmd_context *cctx = doveadm_cmd_context_create(
                DOVEADM_CONNECTION_TYPE_CLI, doveadm_verbose || doveadm_debug);
 
        i_set_failure_exit_callback(failure_exit_callback);
 
-       while ((c = master_getopt(master_service)) > 0) {
+       bool help_requested = FALSE;
+       const char *longopt = NULL;
+       while ((c = master_getopt_long(master_service, &longopt)) >= 0) {
                switch (c) {
+               case 0:
+                       if (strcmp(longopt, "help") == 0)
+                               help_requested = TRUE;
+                       break;
                case 'D':
                        doveadm_debug = TRUE;
                        doveadm_verbose = TRUE;
@@ -342,9 +354,11 @@ int main(int argc, char *argv[])
                   mail_plugins have been loaded. */
                doveadm_load_modules();
 
+               /* show usage after registering all plugins */
                if (cmd_name == NULL) {
-                       /* show usage after registering all plugins */
-                       usage();
+                       FILE *out = help_requested ? stdout : stderr;
+                       int exit_code = help_requested ? EX_OK : EX_USAGE;
+                       usage_prefix(out, "", exit_code);
                }
        }
 
index 02ea9ab9655732815a30f2e6f02cad1865ab59ba..5d835d605a91b869d352b28abc02b990f3d2c563 100644 (file)
@@ -51,6 +51,8 @@
 
 struct master_service *master_service;
 
+const struct option master_service_helpopt = {"help", no_argument, NULL, 0};
+
 static struct event_category master_service_category = {
        .name = NULL, /* set dynamically later */
 };
index ab9874eeb9ed6edcd7bd9764e915cc7c765dd20d..21d8e4c85e94c8732cd1048835e45f7e814ea1a3 100644 (file)
@@ -110,6 +110,8 @@ master_service_avail_overflow_callback_t(bool kill, struct timeval *created_r);
 
 extern struct master_service *master_service;
 
+extern const struct option master_service_helpopt;
+
 const char *master_service_getopt_string(void);
 
 /* Start service initialization. */