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 "main-func.h"
#include "nulstr-util.h" #include "nulstr-util.h"
#include "pager.h" #include "pager.h"
#include "parse-argument.h"
#include "parse-util.h" #include "parse-util.h"
#include "path-util.h" #include "path-util.h"
#include "pretty-print.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(argc >= 1);
assert(argv); assert(argv);
@ -616,18 +617,10 @@ static int parse_argv(int argc, char *argv[]) {
} }
case ARG_DIFF: case ARG_DIFF:
if (!optarg) r = parse_boolean_argument("--diff", optarg, NULL);
arg_diff = 1; if (r < 0)
else { return r;
int b; arg_diff = r;
b = parse_boolean(optarg);
if (b < 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Failed to parse diff boolean.");
arg_diff = b;
}
break; break;
case '?': case '?':

View File

@ -288,9 +288,9 @@ static int parse_argv(int argc, char *argv[]) {
break; break;
case ARG_AUTOMOUNT: case ARG_AUTOMOUNT:
r = parse_boolean(optarg); r = parse_boolean_argument("--automount=", optarg, NULL);
if (r < 0) 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; arg_action = r ? ACTION_AUTOMOUNT : ACTION_MOUNT;
break; break;

View File

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

View File

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

View File

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

View File

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