summaryrefslogtreecommitdiffstats
path: root/src/shared/parse-argument.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2021-02-15 19:32:42 +0100
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2021-02-17 21:06:31 +0100
commit599c7c545f689f7d19a73238eecc69bf94fa6a74 (patch)
tree3d7cc8218ea1771ada8c36d094cda46a51c81291 /src/shared/parse-argument.c
parentMerge pull request #18593 from keszybz/fuzz-more-systemctl-paths (diff)
downloadsystemd-599c7c545f689f7d19a73238eecc69bf94fa6a74.tar.xz
systemd-599c7c545f689f7d19a73238eecc69bf94fa6a74.zip
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.
Diffstat (limited to 'src/shared/parse-argument.c')
-rw-r--r--src/shared/parse-argument.c19
1 files changed, 19 insertions, 0 deletions
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);