diff options
-rw-r--r-- | src/analyze/analyze.c | 27 | ||||
-rw-r--r-- | src/busctl/busctl.c | 31 | ||||
-rw-r--r-- | src/cgtop/cgtop.c | 8 | ||||
-rw-r--r-- | src/core/main.c | 42 | ||||
-rw-r--r-- | src/journal/cat.c | 16 | ||||
-rw-r--r-- | src/mount/mount-tool.c | 7 | ||||
-rw-r--r-- | src/partition/repart.c | 14 | ||||
-rw-r--r-- | src/resolve/resolvectl.c | 13 | ||||
-rw-r--r-- | src/shared/parse-argument.c | 19 | ||||
-rw-r--r-- | src/shared/parse-argument.h | 1 |
10 files changed, 72 insertions, 106 deletions
diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c index c25d11e0de..55d32fee22 100644 --- a/src/analyze/analyze.c +++ b/src/analyze/analyze.c @@ -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: diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c index 6a492ebd02..99ff5bf561 100644 --- a/src/busctl/busctl.c +++ b/src/busctl/busctl.c @@ -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': diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c index d0fa69ff88..cbae9897a5 100644 --- a/src/cgtop/cgtop.c +++ b/src/cgtop/cgtop.c @@ -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': diff --git a/src/core/main.c b/src/core/main.c index 8c7bcc2d3d..1a385c61d4 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -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: diff --git a/src/journal/cat.c b/src/journal/cat.c index 6599e64296..4ccc5e0a33 100644 --- a/src/journal/cat.c +++ b/src/journal/cat.c @@ -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; diff --git a/src/mount/mount-tool.c b/src/mount/mount-tool.c index 8b6fc520c5..5a8e8b1143 100644 --- a/src/mount/mount-tool.c +++ b/src/mount/mount-tool.c @@ -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: diff --git a/src/partition/repart.c b/src/partition/repart.c index 78c31187ea..ac905cab53 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -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: diff --git a/src/resolve/resolvectl.c b/src/resolve/resolvectl.c index e7ee735428..ec572cabf0 100644 --- a/src/resolve/resolvectl.c +++ b/src/resolve/resolvectl.c @@ -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': diff --git a/src/shared/parse-argument.c b/src/shared/parse-argument.c index cd1d0dde21..89951c4b3d 100644 --- a/src/shared/parse-argument.c +++ b/src/shared/parse-argument.c @@ -10,6 +10,25 @@ /* 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); diff --git a/src/shared/parse-argument.h b/src/shared/parse-argument.h index 28b58cc2e2..adad65e902 100644 --- a/src/shared/parse-argument.h +++ b/src/shared/parse-argument.h @@ -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); |