tree-wide: add a helper to parse boolean optarg

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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2021-02-15 19:32:42 +01:00
parent 79dbbb261d
commit 599c7c545f
10 changed files with 72 additions and 106 deletions

View 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:

View 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':

View 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':

View 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:

View 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;

View 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:

View 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:

View 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':

View File

@@ -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);

View 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);