parse-argument: make parse_tristate_argument() do something useful
I expressed the issue I have with parse_tristate_argument() in #37751: it doesn't add any value to direct use of parse_tristate(); on the contrary, it doesn't support means to reset the arg to "auto"/-1 state. The mere reason it existed is that we need a int type ret param. Since the previous attempt to address this mess failed, let's try to make the function more useful by making it accept "auto". I figure this is useful on its own. As requested in https://github.com/systemd/systemd/pull/40652#discussion_r2831833996, the function name is suffixed with _with_auto() to establish that "auto" is handled internally.
This commit is contained in:
@@ -521,7 +521,7 @@ static int parse_argv(int argc, char *argv[]) {
|
||||
break;
|
||||
|
||||
case ARG_VARIABLES:
|
||||
r = parse_tristate_argument("--variables=", optarg, &arg_touch_variables);
|
||||
r = parse_tristate_argument_with_auto("--variables=", optarg, &arg_touch_variables);
|
||||
if (r < 0)
|
||||
return r;
|
||||
break;
|
||||
|
||||
@@ -637,7 +637,7 @@ static int parse_argv(int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
case ARG_COPY_OWNERSHIP:
|
||||
r = parse_tristate_argument("--copy-ownership=", optarg, &arg_copy_ownership);
|
||||
r = parse_tristate_argument_with_auto("--copy-ownership=", optarg, &arg_copy_ownership);
|
||||
if (r < 0)
|
||||
return r;
|
||||
break;
|
||||
|
||||
+1
-1
@@ -1083,7 +1083,7 @@ static int parse_argv_sudo_mode(int argc, char *argv[]) {
|
||||
break;
|
||||
|
||||
case ARG_LIGHTWEIGHT:
|
||||
r = parse_tristate_argument("--lightweight=", optarg, &arg_lightweight);
|
||||
r = parse_tristate_argument_with_auto("--lightweight=", optarg, &arg_lightweight);
|
||||
if (r < 0)
|
||||
return r;
|
||||
break;
|
||||
|
||||
+13
-15
@@ -20,10 +20,12 @@ int parse_boolean_argument(const char *optname, const char *s, bool *ret) {
|
||||
|
||||
/* Returns the result through *ret and the return value. */
|
||||
|
||||
assert(optname);
|
||||
|
||||
if (s) {
|
||||
r = parse_boolean(s);
|
||||
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;
|
||||
@@ -36,24 +38,20 @@ int parse_boolean_argument(const char *optname, const char *s, bool *ret) {
|
||||
}
|
||||
}
|
||||
|
||||
int parse_tristate_argument(const char *optname, const char *s, int *ret) {
|
||||
int parse_tristate_argument_with_auto(const char *optname, const char *s, int *ret) {
|
||||
int r;
|
||||
|
||||
if (s) {
|
||||
r = parse_boolean(s);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to parse boolean argument to %s: %s.", optname, s);
|
||||
assert(optname);
|
||||
assert(s); /* We refuse NULL optarg here, since that would be ambiguous on cmdline:
|
||||
for --enable-a[=BOOL], --enable-a is intuitively interpreted as true rather than "auto"
|
||||
(parse_boolean_argument() does exactly that). IOW, tristate options should require
|
||||
arguments. */
|
||||
|
||||
if (ret)
|
||||
*ret = r;
|
||||
r = parse_tristate_full(s, "auto", ret);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to parse tristate argument to '%s': %s", optname, s);
|
||||
|
||||
return r;
|
||||
} else {
|
||||
if (ret)
|
||||
*ret = -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int parse_json_argument(const char *s, sd_json_format_flags_t *ret) {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "shared-forward.h"
|
||||
|
||||
int parse_boolean_argument(const char *optname, const char *s, bool *ret);
|
||||
int parse_tristate_argument(const char *optname, const char *s, int *ret);
|
||||
int parse_tristate_argument_with_auto(const char *optname, const char *s, int *ret);
|
||||
int parse_json_argument(const char *s, sd_json_format_flags_t *ret);
|
||||
int parse_path_argument(const char *path, bool suppress_root, char **arg);
|
||||
int parse_signal_argument(const char *s, int *ret);
|
||||
|
||||
@@ -830,9 +830,9 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
|
||||
break;
|
||||
|
||||
case ARG_CHECK_INHIBITORS:
|
||||
r = parse_tristate_full(optarg, "auto", &arg_check_inhibitors);
|
||||
r = parse_tristate_argument_with_auto("--check-inhibitors=", optarg, &arg_check_inhibitors);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to parse --check-inhibitors= argument: %s", optarg);
|
||||
return r;
|
||||
break;
|
||||
|
||||
case ARG_PLAIN:
|
||||
|
||||
Reference in New Issue
Block a user