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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user