1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-23 17:34:00 +03:00

tree-wide: use parse_boolean_argument() for variables with non-boolean type

This still works nicely, but we need to assign the return value ourselves.
As before, one nice effect is that error messages are uniform.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2021-02-15 19:41:39 +01:00
parent 599c7c545f
commit c3470872c6
6 changed files with 49 additions and 55 deletions

View File

@ -15,6 +15,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"
@ -586,7 +587,7 @@ static int parse_argv(int argc, char *argv[]) {
{}
};
int c;
int c, r;
assert(argc >= 1);
assert(argv);
@ -616,18 +617,10 @@ static int parse_argv(int argc, char *argv[]) {
}
case ARG_DIFF:
if (!optarg)
arg_diff = 1;
else {
int b;
b = parse_boolean(optarg);
if (b < 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Failed to parse diff boolean.");
arg_diff = b;
}
r = parse_boolean_argument("--diff", optarg, NULL);
if (r < 0)
return r;
arg_diff = r;
break;
case '?':

View File

@ -288,9 +288,9 @@ static int parse_argv(int argc, char *argv[]) {
break;
case ARG_AUTOMOUNT:
r = parse_boolean(optarg);
r = parse_boolean_argument("--automount=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "--automount= expects a valid boolean parameter: %s", optarg);
return r;
arg_action = r ? ACTION_AUTOMOUNT : ACTION_MOUNT;
break;

View File

@ -3608,10 +3608,9 @@ static int parse_argv(int argc, char *argv[]) {
break;
case ARG_FACTORY_RESET:
r = parse_boolean(optarg);
r = parse_boolean_argument("--factory-reset=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse --factory-reset= parameter: %s", optarg);
return r;
arg_factory_reset = r;
break;
@ -3642,10 +3641,9 @@ static int parse_argv(int argc, char *argv[]) {
break;
case ARG_PRETTY:
r = parse_boolean(optarg);
r = parse_boolean_argument("--pretty=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse --pretty= parameter: %s", optarg);
return r;
arg_pretty = r;
break;

View File

@ -2831,30 +2831,30 @@ static int compat_parse_argv(int argc, char *argv[]) {
break;
case ARG_CNAME:
r = parse_boolean(optarg);
r = parse_boolean_argument("--cname=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse --cname= argument.");
return r;
SET_FLAG(arg_flags, SD_RESOLVED_NO_CNAME, r == 0);
break;
case ARG_SERVICE_ADDRESS:
r = parse_boolean(optarg);
r = parse_boolean_argument("--service-address=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse --service-address= argument.");
return r;
SET_FLAG(arg_flags, SD_RESOLVED_NO_ADDRESS, r == 0);
break;
case ARG_SERVICE_TXT:
r = parse_boolean(optarg);
r = parse_boolean_argument("--service-txt=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse --service-txt= argument.");
return r;
SET_FLAG(arg_flags, SD_RESOLVED_NO_TXT, r == 0);
break;
case ARG_SEARCH:
r = parse_boolean(optarg);
r = parse_boolean_argument("--search=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse --search argument.");
return r;
SET_FLAG(arg_flags, SD_RESOLVED_NO_SEARCH, r == 0);
break;
@ -3109,72 +3109,72 @@ static int native_parse_argv(int argc, char *argv[]) {
break;
case ARG_CNAME:
r = parse_boolean(optarg);
r = parse_boolean_argument("--cname=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse --cname= argument.");
return r;
SET_FLAG(arg_flags, SD_RESOLVED_NO_CNAME, r == 0);
break;
case ARG_VALIDATE:
r = parse_boolean(optarg);
r = parse_boolean_argument("--validate=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse --validate= argument.");
return r;
SET_FLAG(arg_flags, SD_RESOLVED_NO_VALIDATE, r == 0);
break;
case ARG_SYNTHESIZE:
r = parse_boolean(optarg);
r = parse_boolean_argument("--synthesize=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse --synthesize= argument.");
return r;
SET_FLAG(arg_flags, SD_RESOLVED_NO_SYNTHESIZE, r == 0);
break;
case ARG_CACHE:
r = parse_boolean(optarg);
r = parse_boolean_argument("--cache=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse --cache= argument.");
return r;
SET_FLAG(arg_flags, SD_RESOLVED_NO_CACHE, r == 0);
break;
case ARG_ZONE:
r = parse_boolean(optarg);
r = parse_boolean_argument("--zone=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse --zone= argument.");
return r;
SET_FLAG(arg_flags, SD_RESOLVED_NO_ZONE, r == 0);
break;
case ARG_TRUST_ANCHOR:
r = parse_boolean(optarg);
r = parse_boolean_argument("--trust-anchor=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse --trust-anchor= argument.");
return r;
SET_FLAG(arg_flags, SD_RESOLVED_NO_TRUST_ANCHOR, r == 0);
break;
case ARG_NETWORK:
r = parse_boolean(optarg);
r = parse_boolean_argument("--network=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse --network= argument.");
return r;
SET_FLAG(arg_flags, SD_RESOLVED_NO_NETWORK, r == 0);
break;
case ARG_SERVICE_ADDRESS:
r = parse_boolean(optarg);
r = parse_boolean_argument("--service-address=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse --service-address= argument.");
return r;
SET_FLAG(arg_flags, SD_RESOLVED_NO_ADDRESS, r == 0);
break;
case ARG_SERVICE_TXT:
r = parse_boolean(optarg);
r = parse_boolean_argument("--service-txt=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse --service-txt= argument.");
return r;
SET_FLAG(arg_flags, SD_RESOLVED_NO_TXT, r == 0);
break;
case ARG_SEARCH:
r = parse_boolean(optarg);
r = parse_boolean_argument("--search=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse --search argument.");
return r;
SET_FLAG(arg_flags, SD_RESOLVED_NO_SEARCH, r == 0);
break;

View File

@ -20,10 +20,12 @@ int parse_boolean_argument(const char *optname, const char *s, bool *ret) {
if (r < 0)
return log_error_errno(r, "Failed to parse boolean argument to %s: %s.", optname, s);
if (ret)
*ret = r;
return r;
} else {
/* s may be NULL. This is controlled by getopt_long() parameters. */
if (ret)
*ret = true;
return true;
}

View File

@ -10,6 +10,7 @@
#include "format-util.h"
#include "main-func.h"
#include "pager.h"
#include "parse-argument.h"
#include "parse-util.h"
#include "pretty-print.h"
#include "socket-util.h"
@ -720,17 +721,17 @@ static int parse_argv(int argc, char *argv[]) {
break;
case ARG_WITH_NSS:
r = parse_boolean(optarg);
r = parse_boolean_argument("--with-nss=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse --with-nss= parameter: %s", optarg);
return r;
SET_FLAG(arg_userdb_flags, USERDB_AVOID_NSS, !r);
break;
case ARG_SYNTHESIZE:
r = parse_boolean(optarg);
r = parse_boolean_argument("--synthesize=", optarg, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse --synthesize= parameter: %s", optarg);
return r;
SET_FLAG(arg_userdb_flags, USERDB_DONT_SYNTHESIZE, !r);
break;