]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: add a helper to parse boolean optarg
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 15 Feb 2021 18:32:42 +0000 (19:32 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 17 Feb 2021 20:06:31 +0000 (21:06 +0100)
This nicely covers the case when optarg is optional. The same parser can be
used when the option string passed to getopt_long() requires a parameter and
when it doesn't.

The error messages are made consistent.
Also fixes a log error c&p in --crash-reboot message.

src/analyze/analyze.c
src/busctl/busctl.c
src/cgtop/cgtop.c
src/core/main.c
src/journal/cat.c
src/mount/mount-tool.c
src/partition/repart.c
src/resolve/resolvectl.c
src/shared/parse-argument.c
src/shared/parse-argument.h

index c25d11e0de356824cd2cb47f42d9e3dff7d3c5d2..55d32fee22ad7bce09ae1670f1094f04cf9b5ae5 100644 (file)
@@ -37,6 +37,7 @@
 #include "main-func.h"
 #include "nulstr-util.h"
 #include "pager.h"
+#include "parse-argument.h"
 #include "parse-util.h"
 #include "path-util.h"
 #include "pretty-print.h"
@@ -2346,29 +2347,15 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_MAN:
-                        if (optarg) {
-                                r = parse_boolean(optarg);
-                                if (r < 0)
-                                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
-                                                               "Failed to parse --man= argument.");
-
-                                arg_man = r;
-                        } else
-                                arg_man = true;
-
+                        r = parse_boolean_argument("--man", optarg, &arg_man);
+                        if (r < 0)
+                                return r;
                         break;
 
                 case ARG_GENERATORS:
-                        if (optarg) {
-                                r = parse_boolean(optarg);
-                                if (r < 0)
-                                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
-                                                               "Failed to parse --generators= argument.");
-
-                                arg_generators = r;
-                        } else
-                                arg_generators = true;
-
+                        r = parse_boolean_argument("--generators", optarg, &arg_generators);
+                        if (r < 0)
+                                return r;
                         break;
 
                 case ARG_ITERATIONS:
index 6a492ebd02f4175d6fc08a085ce6e32cf32c2644..99ff5bf5619ad3fe802813e9e3b0109074878995 100644 (file)
@@ -2473,27 +2473,22 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_EXPECT_REPLY:
-                        r = parse_boolean(optarg);
+                        r = parse_boolean_argument("--expect-reply=", optarg, &arg_expect_reply);
                         if (r < 0)
-                                return log_error_errno(r, "Failed to parse --expect-reply= parameter '%s': %m", optarg);
-
-                        arg_expect_reply = r;
+                                return r;
                         break;
 
                 case ARG_AUTO_START:
-                        r = parse_boolean(optarg);
+                        r = parse_boolean_argument("--auto-start=", optarg, &arg_auto_start);
                         if (r < 0)
-                                return log_error_errno(r, "Failed to parse --auto-start= parameter '%s': %m", optarg);
-
-                        arg_auto_start = r;
+                                return r;
                         break;
 
                 case ARG_ALLOW_INTERACTIVE_AUTHORIZATION:
-                        r = parse_boolean(optarg);
+                        r = parse_boolean_argument("--allow-interactive-authorization=", optarg,
+                                                   &arg_allow_interactive_authorization);
                         if (r < 0)
-                                return log_error_errno(r, "Failed to parse --allow-interactive-authorization= parameter '%s': %m", optarg);
-
-                        arg_allow_interactive_authorization = r;
+                                return r;
                         break;
 
                 case ARG_TIMEOUT:
@@ -2504,19 +2499,15 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_AUGMENT_CREDS:
-                        r = parse_boolean(optarg);
+                        r = parse_boolean_argument("--augment-creds=", optarg, &arg_augment_creds);
                         if (r < 0)
-                                return log_error_errno(r, "Failed to parse --augment-creds= parameter '%s': %m", optarg);
-
-                        arg_augment_creds = r;
+                                return r;
                         break;
 
                 case ARG_WATCH_BIND:
-                        r = parse_boolean(optarg);
+                        r = parse_boolean_argument("--watch-bind=", optarg, &arg_watch_bind);
                         if (r < 0)
-                                return log_error_errno(r, "Failed to parse --watch-bind= parameter '%s': %m", optarg);
-
-                        arg_watch_bind = r;
+                                return r;
                         break;
 
                 case 'j':
index d0fa69ff88dca9deaafe10fe674e31db8a226d30..cbae9897a507c0fc532d8285e4956bf24af8c189 100644 (file)
@@ -19,6 +19,7 @@
 #include "hashmap.h"
 #include "main-func.h"
 #include "missing_sched.h"
+#include "parse-argument.h"
 #include "parse-util.h"
 #include "path-util.h"
 #include "pretty-print.h"
@@ -867,12 +868,11 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_RECURSIVE:
-                        r = parse_boolean(optarg);
+                        r = parse_boolean_argument("--recursive=", optarg, &arg_recursive);
                         if (r < 0)
-                                return log_error_errno(r, "Failed to parse --recursive= argument '%s': %m", optarg);
+                                return r;
 
-                        arg_recursive = r;
-                        arg_recursive_unset = r == 0;
+                        arg_recursive_unset = !r;
                         break;
 
                 case 'M':
index 8c7bcc2d3d98c9ece854d224b5607ae42acef4c9..1a385c61d4ed157c4e55c982a9e3325c7fa6da5c 100644 (file)
@@ -938,15 +938,9 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_DUMP_CORE:
-                        if (!optarg)
-                                arg_dump_core = true;
-                        else {
-                                r = parse_boolean(optarg);
-                                if (r < 0)
-                                        return log_error_errno(r, "Failed to parse dump core boolean: \"%s\": %m",
-                                                               optarg);
-                                arg_dump_core = r;
-                        }
+                        r = parse_boolean_argument("--dump-core", optarg, &arg_dump_core);
+                        if (r < 0)
+                                return r;
                         break;
 
                 case ARG_CRASH_CHVT:
@@ -957,27 +951,15 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_CRASH_SHELL:
-                        if (!optarg)
-                                arg_crash_shell = true;
-                        else {
-                                r = parse_boolean(optarg);
-                                if (r < 0)
-                                        return log_error_errno(r, "Failed to parse crash shell boolean: \"%s\": %m",
-                                                               optarg);
-                                arg_crash_shell = r;
-                        }
+                        r = parse_boolean_argument("--crash-shell", optarg, &arg_crash_shell);
+                        if (r < 0)
+                                return r;
                         break;
 
                 case ARG_CRASH_REBOOT:
-                        if (!optarg)
-                                arg_crash_reboot = true;
-                        else {
-                                r = parse_boolean(optarg);
-                                if (r < 0)
-                                        return log_error_errno(r, "Failed to parse crash shell boolean: \"%s\": %m",
-                                                               optarg);
-                                arg_crash_reboot = r;
-                        }
+                        r = parse_boolean_argument("--crash-reboot", optarg, &arg_crash_reboot);
+                        if (r < 0)
+                                return r;
                         break;
 
                 case ARG_CONFIRM_SPAWN:
@@ -990,11 +972,9 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_SERVICE_WATCHDOGS:
-                        r = parse_boolean(optarg);
+                        r = parse_boolean_argument("--service-watchdogs=", optarg, &arg_service_watchdogs);
                         if (r < 0)
-                                return log_error_errno(r, "Failed to parse service watchdogs boolean: \"%s\": %m",
-                                                       optarg);
-                        arg_service_watchdogs = r;
+                                return r;
                         break;
 
                 case ARG_SHOW_STATUS:
index 6599e64296a79303aa78bf1be593099d0cbfdecc..4ccc5e0a330aab3922939206aaa6f20cc680f9e1 100644 (file)
@@ -12,6 +12,7 @@
 #include "alloc-util.h"
 #include "fd-util.h"
 #include "main-func.h"
+#include "parse-argument.h"
 #include "parse-util.h"
 #include "pretty-print.h"
 #include "string-util.h"
@@ -67,7 +68,7 @@ static int parse_argv(int argc, char *argv[]) {
                 {}
         };
 
-        int c;
+        int c, r;
 
         assert(argc >= 0);
         assert(argv);
@@ -104,16 +105,11 @@ static int parse_argv(int argc, char *argv[]) {
                                                        "Failed to parse stderr priority value.");
                         break;
 
-                case ARG_LEVEL_PREFIX: {
-                        int k;
-
-                        k = parse_boolean(optarg);
-                        if (k < 0)
-                                return log_error_errno(k, "Failed to parse level prefix value.");
-
-                        arg_level_prefix = k;
+                case ARG_LEVEL_PREFIX:
+                        r = parse_boolean_argument("--level-prefix=", optarg, &arg_level_prefix);
+                        if (r < 0)
+                                return r;
                         break;
-                }
 
                 case '?':
                         return -EINVAL;
index 8b6fc520c5ebf4c7fb76059a31e2f6cfa16d2904..5a8e8b1143c5a5a7f3a2659bcf33ab3f2c929146 100644 (file)
@@ -23,6 +23,7 @@
 #include "mount-util.h"
 #include "mountpoint-util.h"
 #include "pager.h"
+#include "parse-argument.h"
 #include "parse-util.h"
 #include "path-util.h"
 #include "pretty-print.h"
@@ -265,11 +266,9 @@ static int parse_argv(int argc, char *argv[]) {
                 }
 
                 case ARG_FSCK:
-                        r = parse_boolean(optarg);
+                        r = parse_boolean_argument("--fsck=", optarg, &arg_fsck);
                         if (r < 0)
-                                return log_error_errno(r, "Failed to parse --fsck= argument: %s", optarg);
-
-                        arg_fsck = r;
+                                return r;
                         break;
 
                 case ARG_DESCRIPTION:
index 78c31187ea4142e0d47dba879bb3390ada9bc6ff..ac905cab5315fbc2b87619b5e291fbfb41505580 100644 (file)
@@ -44,8 +44,8 @@
 #include "mkdir.h"
 #include "mkfs-util.h"
 #include "mount-util.h"
-#include "parse-util.h"
 #include "parse-argument.h"
+#include "parse-util.h"
 #include "path-util.h"
 #include "pretty-print.h"
 #include "proc-cmdline.h"
@@ -3575,11 +3575,9 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_DRY_RUN:
-                        r = parse_boolean(optarg);
+                        r = parse_boolean_argument("--dry-run=", optarg, &arg_dry_run);
                         if (r < 0)
-                                return log_error_errno(r, "Failed to parse --dry-run= parameter: %s", optarg);
-
-                        dry_run = r;
+                                return r;
                         break;
 
                 case ARG_EMPTY:
@@ -3604,11 +3602,9 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_DISCARD:
-                        r = parse_boolean(optarg);
+                        r = parse_boolean_argument("--discard=", optarg, &arg_discard);
                         if (r < 0)
-                                return log_error_errno(r, "Failed to parse --discard= parameter: %s", optarg);
-
-                        arg_discard = r;
+                                return r;
                         break;
 
                 case ARG_FACTORY_RESET:
index e7ee7354287630ee71121bd00f1c441725e4f3c2..ec572cabf08f05cffc37877ee5c8450c1237c011 100644 (file)
@@ -23,6 +23,7 @@
 #include "missing_network.h"
 #include "netlink-util.h"
 #include "pager.h"
+#include "parse-argument.h"
 #include "parse-util.h"
 #include "pretty-print.h"
 #include "resolvconf-compat.h"
@@ -2766,11 +2767,9 @@ static int compat_parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_LEGEND:
-                        r = parse_boolean(optarg);
+                        r = parse_boolean_argument("--legend=", optarg, &arg_legend);
                         if (r < 0)
-                                return log_error_errno(r, "Failed to parse --legend= argument");
-
-                        arg_legend = r;
+                                return r;
                         break;
 
                 case 'p':
@@ -3062,11 +3061,9 @@ static int native_parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_LEGEND:
-                        r = parse_boolean(optarg);
+                        r = parse_boolean_argument("--legend=", optarg, &arg_legend);
                         if (r < 0)
-                                return log_error_errno(r, "Failed to parse --legend= argument");
-
-                        arg_legend = r;
+                                return r;
                         break;
 
                 case 'p':
index cd1d0dde21a54cbbe0b0074ab2a504f3a23c3db6..89951c4b3d9e45a0b2e5585e753bc0fac1d17bf4 100644 (file)
 
 /* All functions in this file emit warnigs. */
 
+int parse_boolean_argument(const char *optname, const char *s, bool *ret) {
+        int r;
+
+        /* Returns the result through *ret and the return value. */
+
+        if (s) {
+                r = parse_boolean(s);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to parse boolean argument to %s: %s.", optname, s);
+
+                *ret = r;
+                return r;
+        } else {
+                /* s may be NULL. This is controlled by getopt_long() parameters. */
+                *ret = true;
+                return true;
+        }
+}
+
 int parse_json_argument(const char *s, JsonFormatFlags *ret) {
         assert(s);
         assert(ret);
index 28b58cc2e29042ba97320f8a45c695d62113108b..adad65e90257a48f71c30a4d13143b7a9c07f9cc 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "json.h"
 
+int parse_boolean_argument(const char *optname, const char *s, bool *ret);
 int parse_json_argument(const char *s, JsonFormatFlags *ret);
 int parse_path_argument(const char *path, bool suppress_root, char **arg);
 int parse_signal_argument(const char *s, int *ret);