/* SPDX-License-Identifier: LGPL-2.1-or-later */
-#include <getopt.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "extract-word.h"
#include "fd-util.h"
#include "fileio.h"
+#include "format-table.h"
#include "format-util.h"
#include "fs-util.h"
#include "hashmap.h"
#include "loop-util.h"
#include "main-func.h"
#include "mount-util.h"
+#include "options.h"
#include "pager.h"
#include "parse-argument.h"
#include "path-util.h"
static int help(void) {
_cleanup_free_ char *link = NULL;
+ _cleanup_(table_unrefp) Table *cmds = NULL, *opts = NULL;
int r;
r = terminal_urlify_man("systemd-sysusers.service", "8", &link);
if (r < 0)
return log_oom();
- printf("%1$s [OPTIONS...] [CONFIGURATION FILE...]\n"
- "\n%2$sCreates system user and group accounts.%4$s\n"
- "\n%3$sCommands:%4$s\n"
- " --cat-config Show configuration files\n"
- " --tldr Show non-comment parts of configuration\n"
- " -h --help Show this help\n"
- " --version Show package version\n"
- "\n%3$sOptions:%4$s\n"
- " --root=PATH Operate on an alternate filesystem root\n"
- " --image=PATH Operate on disk image as filesystem root\n"
- " --image-policy=POLICY Specify disk image dissection policy\n"
- " --replace=PATH Treat arguments as replacement for PATH\n"
- " --dry-run Just print what would be done\n"
- " --inline Treat arguments as configuration lines\n"
- " --no-pager Do not pipe output into a pager\n"
- "\nSee the %5$s for details.\n",
+ r = option_parser_get_help_table(&cmds);
+ if (r < 0)
+ return r;
+
+ r = option_parser_get_help_table_group("Options", &opts);
+ if (r < 0)
+ return r;
+
+ (void) table_sync_column_widths(0, cmds, opts);
+
+ printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n"
+ "\n%sCreates system user and group accounts.%s\n"
+ "\nCommands:\n",
program_invocation_short_name,
ansi_highlight(),
- ansi_underline(),
- ansi_normal(),
- link);
+ ansi_normal());
- return 0;
-}
+ r = table_print_or_warn(cmds);
+ if (r < 0)
+ return r;
-static int parse_argv(int argc, char *argv[]) {
-
- enum {
- ARG_VERSION = 0x100,
- ARG_CAT_CONFIG,
- ARG_TLDR,
- ARG_ROOT,
- ARG_IMAGE,
- ARG_IMAGE_POLICY,
- ARG_REPLACE,
- ARG_DRY_RUN,
- ARG_INLINE,
- ARG_NO_PAGER,
- };
+ printf("\nOptions:\n");
- static const struct option options[] = {
- { "help", no_argument, NULL, 'h' },
- { "version", no_argument, NULL, ARG_VERSION },
- { "cat-config", no_argument, NULL, ARG_CAT_CONFIG },
- { "tldr", no_argument, NULL, ARG_TLDR },
- { "root", required_argument, NULL, ARG_ROOT },
- { "image", required_argument, NULL, ARG_IMAGE },
- { "image-policy", required_argument, NULL, ARG_IMAGE_POLICY },
- { "replace", required_argument, NULL, ARG_REPLACE },
- { "dry-run", no_argument, NULL, ARG_DRY_RUN },
- { "inline", no_argument, NULL, ARG_INLINE },
- { "no-pager", no_argument, NULL, ARG_NO_PAGER },
- {}
- };
+ r = table_print_or_warn(opts);
+ if (r < 0)
+ return r;
+
+ printf("\nSee the %s for details.\n", link);
+ return 0;
+}
- int c, r;
+static int parse_argv(int argc, char *argv[], char ***ret_args) {
+ int r;
assert(argc >= 0);
assert(argv);
- while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
+ OptionParser state = { argc, argv };
+ const char *arg;
+ FOREACH_OPTION(&state, c, &arg, /* on_error= */ return c)
switch (c) {
- case 'h':
- return help();
-
- case ARG_VERSION:
- return version();
-
- case ARG_CAT_CONFIG:
+ OPTION_COMMON_CAT_CONFIG:
arg_cat_flags = CAT_CONFIG_ON;
break;
- case ARG_TLDR:
+ OPTION_COMMON_TLDR:
arg_cat_flags = CAT_TLDR;
break;
- case ARG_ROOT:
- r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root);
+ OPTION_COMMON_HELP:
+ return help();
+
+ OPTION_COMMON_VERSION:
+ return version();
+
+ OPTION_GROUP("Options"):
+ break;
+
+ OPTION_LONG("root", "PATH", "Operate on an alternate filesystem root"):
+ r = parse_path_argument(arg, /* suppress_root= */ false, &arg_root);
if (r < 0)
return r;
break;
- case ARG_IMAGE:
- r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
+ OPTION_LONG("image", "PATH", "Operate on disk image as filesystem root"):
+ r = parse_path_argument(arg, /* suppress_root= */ false, &arg_image);
if (r < 0)
return r;
break;
- case ARG_IMAGE_POLICY:
- r = parse_image_policy_argument(optarg, &arg_image_policy);
+ OPTION_LONG("image-policy", "POLICY", "Specify disk image dissection policy"):
+ r = parse_image_policy_argument(arg, &arg_image_policy);
if (r < 0)
return r;
break;
- case ARG_REPLACE:
- if (!path_is_absolute(optarg))
+ OPTION_LONG("replace", "PATH", "Treat arguments as replacement for PATH"):
+ if (!path_is_absolute(arg))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"The argument to --replace= must be an absolute path.");
- if (!endswith(optarg, ".conf"))
+ if (!endswith(arg, ".conf"))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"The argument to --replace= must have the extension '.conf'.");
- arg_replace = optarg;
+ arg_replace = arg;
break;
- case ARG_DRY_RUN:
+ OPTION_LONG("dry-run", NULL, "Just print what would be done"):
arg_dry_run = true;
break;
- case ARG_INLINE:
+ OPTION_LONG("inline", NULL, "Treat arguments as configuration lines"):
arg_inline = true;
break;
- case ARG_NO_PAGER:
+ OPTION_COMMON_NO_PAGER:
arg_pager_flags |= PAGER_DISABLE;
break;
-
- case '?':
- return -EINVAL;
-
- default:
- assert_not_reached();
}
+ char **args = option_parser_get_args(&state);
+ size_t n_args = option_parser_get_n_args(&state);
+
if (arg_replace && arg_cat_flags != CAT_CONFIG_OFF)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Option --replace= is not supported with --cat-config/--tldr.");
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Option --inline is not supported with --cat-config/--tldr.");
- if (arg_replace && optind >= argc)
+ if (arg_replace && n_args == 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"When --replace= is given, some configuration items must be specified.");
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Use either --root= or --image=, the combination of both is not supported.");
+ *ret_args = args;
return 1;
}
Item *i;
int r;
- r = parse_argv(argc, argv);
+ char **args = NULL;
+ r = parse_argv(argc, argv, &args);
if (r <= 0)
return r;
* insert the positional arguments at the specified place. Otherwise, if command line arguments are
* specified, execute just them, and finally, without --replace= or any positional arguments, just
* read configuration and execute it. */
- if (arg_replace || optind >= argc)
- r = read_config_files(&c, argv + optind);
+ if (arg_replace || strv_isempty(args))
+ r = read_config_files(&c, args);
else
- r = parse_arguments(&c, argv + optind);
+ r = parse_arguments(&c, args);
if (r < 0)
return r;