id128: use the new option parser
--version was not documented. Fixup for 0d1d512f7f.
This commit is contained in:
+53
-91
@@ -1,6 +1,5 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <getopt.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
@@ -11,6 +10,7 @@
|
||||
#include "id128-util.h"
|
||||
#include "log.h"
|
||||
#include "main-func.h"
|
||||
#include "options.h"
|
||||
#include "parse-argument.h"
|
||||
#include "pretty-print.h"
|
||||
#include "string-util.h"
|
||||
@@ -24,10 +24,12 @@ static PagerFlags arg_pager_flags = 0;
|
||||
static bool arg_legend = true;
|
||||
static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
|
||||
|
||||
VERB_NOARG(verb_new, "new", "Generate a new ID");
|
||||
static int verb_new(int argc, char *argv[], uintptr_t _data, void *userdata) {
|
||||
return id128_print_new(arg_mode);
|
||||
}
|
||||
|
||||
VERB_NOARG(verb_machine_id, "machine-id", "Print the ID of current machine");
|
||||
static int verb_machine_id(int argc, char *argv[], uintptr_t _data, void *userdata) {
|
||||
sd_id128_t id;
|
||||
int r;
|
||||
@@ -43,6 +45,7 @@ static int verb_machine_id(int argc, char *argv[], uintptr_t _data, void *userda
|
||||
return id128_pretty_print(id, arg_mode);
|
||||
}
|
||||
|
||||
VERB_NOARG(verb_boot_id, "boot-id", "Print the ID of current boot");
|
||||
static int verb_boot_id(int argc, char *argv[], uintptr_t _data, void *userdata) {
|
||||
sd_id128_t id;
|
||||
int r;
|
||||
@@ -58,6 +61,7 @@ static int verb_boot_id(int argc, char *argv[], uintptr_t _data, void *userdata)
|
||||
return id128_pretty_print(id, arg_mode);
|
||||
}
|
||||
|
||||
VERB_NOARG(verb_invocation_id, "invocation-id", "Print the ID of current invocation");
|
||||
static int verb_invocation_id(int argc, char *argv[], uintptr_t _data, void *userdata) {
|
||||
sd_id128_t id;
|
||||
int r;
|
||||
@@ -73,6 +77,7 @@ static int verb_invocation_id(int argc, char *argv[], uintptr_t _data, void *use
|
||||
return id128_pretty_print(id, arg_mode);
|
||||
}
|
||||
|
||||
VERB_NOARG(verb_var_uuid, "var-partition-uuid", "Print the UUID for the /var/ partition");
|
||||
static int verb_var_uuid(int argc, char *argv[], uintptr_t _data, void *userdata) {
|
||||
sd_id128_t id;
|
||||
int r;
|
||||
@@ -130,6 +135,7 @@ static int show_one(Table **table, const char *name, sd_id128_t uuid, bool first
|
||||
arg_mode == ID128_PRINT_ID128 ? TABLE_ID128 : TABLE_UUID, uuid);
|
||||
}
|
||||
|
||||
VERB(verb_show, "show", "[NAME|UUID]", VERB_ANY, VERB_ANY, 0, "Print one or more UUIDs");
|
||||
static int verb_show(int argc, char *argv[], uintptr_t _data, void *userdata) {
|
||||
_cleanup_(table_unrefp) Table *table = NULL;
|
||||
int r;
|
||||
@@ -186,158 +192,114 @@ static int verb_show(int argc, char *argv[], uintptr_t _data, void *userdata) {
|
||||
|
||||
static int help(void) {
|
||||
_cleanup_free_ char *link = NULL;
|
||||
_cleanup_(table_unrefp) Table *options = NULL, *verbs = NULL;
|
||||
int r;
|
||||
|
||||
r = terminal_urlify_man("systemd-id128", "1", &link);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
|
||||
r = option_parser_get_help_table(&options);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = verbs_get_help_table(&verbs);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
printf("%s [OPTIONS...] COMMAND\n\n"
|
||||
"%sGenerate and print 128-bit identifiers.%s\n"
|
||||
"\nCommands:\n"
|
||||
" new Generate a new ID\n"
|
||||
" machine-id Print the ID of current machine\n"
|
||||
" boot-id Print the ID of current boot\n"
|
||||
" invocation-id Print the ID of current invocation\n"
|
||||
" var-partition-uuid Print the UUID for the /var/ partition\n"
|
||||
" show [NAME|UUID] Print one or more UUIDs\n"
|
||||
" help Show this help\n"
|
||||
"\nOptions:\n"
|
||||
" -h --help Show this help\n"
|
||||
" --no-pager Do not pipe output into a pager\n"
|
||||
" --no-legend Do not show the headers and footers\n"
|
||||
" --json=FORMAT Output inspection data in JSON (takes one of\n"
|
||||
" pretty, short, off)\n"
|
||||
" -j Equivalent to --json=pretty (on TTY) or\n"
|
||||
" --json=short (otherwise)\n"
|
||||
" -p --pretty Generate samples of program code\n"
|
||||
" -P --value Only print the value\n"
|
||||
" -a --app-specific=ID Generate app-specific IDs\n"
|
||||
" -u --uuid Output in UUID format\n"
|
||||
"\nSee the %s for details.\n",
|
||||
"\nCommands:\n",
|
||||
program_invocation_short_name,
|
||||
ansi_highlight(),
|
||||
ansi_normal(),
|
||||
link);
|
||||
ansi_normal());
|
||||
table_print(verbs, stdout);
|
||||
|
||||
printf("\nOptions:\n");
|
||||
table_print(options, stdout);
|
||||
|
||||
printf("\nSee the %s for details.\n", link);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int verb_help(int argc, char *argv[], uintptr_t _data, void *userdata) {
|
||||
return help();
|
||||
}
|
||||
VERB_COMMON_HELP(help);
|
||||
|
||||
static int parse_argv(int argc, char *argv[]) {
|
||||
enum {
|
||||
ARG_VERSION = 0x100,
|
||||
ARG_NO_PAGER,
|
||||
ARG_NO_LEGEND,
|
||||
ARG_JSON,
|
||||
};
|
||||
|
||||
static const struct option options[] = {
|
||||
{ "help", no_argument, NULL, 'h' },
|
||||
{ "version", no_argument, NULL, ARG_VERSION },
|
||||
{ "no-pager", no_argument, NULL, ARG_NO_PAGER },
|
||||
{ "no-legend", no_argument, NULL, ARG_NO_LEGEND },
|
||||
{ "json", required_argument, NULL, ARG_JSON },
|
||||
{ "pretty", no_argument, NULL, 'p' },
|
||||
{ "value", no_argument, NULL, 'P' },
|
||||
{ "app-specific", required_argument, NULL, 'a' },
|
||||
{ "uuid", no_argument, NULL, 'u' },
|
||||
{},
|
||||
};
|
||||
|
||||
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, "hpa:uPj", options, NULL)) >= 0)
|
||||
switch (c) {
|
||||
OptionParser state = {};
|
||||
const char *arg;
|
||||
|
||||
case 'h':
|
||||
FOREACH_OPTION(&state, c, argc, argv, &arg, /* on_error= */ return c)
|
||||
switch (c) {
|
||||
OPTION_COMMON_HELP:
|
||||
return help();
|
||||
|
||||
case ARG_VERSION:
|
||||
OPTION_COMMON_VERSION:
|
||||
return version();
|
||||
|
||||
case ARG_NO_PAGER:
|
||||
OPTION_COMMON_NO_PAGER:
|
||||
arg_pager_flags |= PAGER_DISABLE;
|
||||
break;
|
||||
|
||||
case ARG_NO_LEGEND:
|
||||
OPTION_COMMON_NO_LEGEND:
|
||||
arg_legend = false;
|
||||
break;
|
||||
|
||||
case 'j':
|
||||
OPTION_LONG("json", "FORMAT",
|
||||
"Output inspection data in JSON (takes one of pretty, short, off)"):
|
||||
r = parse_json_argument(arg, &arg_json_format_flags);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
break;
|
||||
|
||||
OPTION_SHORT('j', NULL,
|
||||
"Equivalent to --json=pretty (on TTY) or --json=short (otherwise)"):
|
||||
arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO;
|
||||
break;
|
||||
|
||||
case ARG_JSON:
|
||||
r = parse_json_argument(optarg, &arg_json_format_flags);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
|
||||
break;
|
||||
case 'p':
|
||||
OPTION('p', "pretty", NULL, "Generate samples of program code"):
|
||||
arg_mode = ID128_PRINT_PRETTY;
|
||||
arg_value = false;
|
||||
break;
|
||||
|
||||
case 'P':
|
||||
OPTION('P', "value", NULL, "Only print the value"):
|
||||
arg_value = true;
|
||||
if (arg_mode == ID128_PRINT_PRETTY)
|
||||
arg_mode = ID128_PRINT_ID128;
|
||||
break;
|
||||
|
||||
case 'a':
|
||||
r = id128_from_string_nonzero(optarg, &arg_app);
|
||||
OPTION('a', "app-specific", "ID", "Generate app-specific IDs"):
|
||||
r = id128_from_string_nonzero(arg, &arg_app);
|
||||
if (r == -ENXIO)
|
||||
return log_error_errno(r, "Application ID cannot be all zeros.");
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to parse \"%s\" as application-ID: %m", optarg);
|
||||
return log_error_errno(r, "Failed to parse \"%s\" as application ID: %m", arg);
|
||||
break;
|
||||
|
||||
case 'u':
|
||||
OPTION('u', "uuid", NULL, "Output in UUID format"):
|
||||
arg_mode = ID128_PRINT_UUID;
|
||||
break;
|
||||
|
||||
case '?':
|
||||
return -EINVAL;
|
||||
|
||||
default:
|
||||
assert_not_reached();
|
||||
}
|
||||
|
||||
*ret_args = option_parser_get_args(&state, argc, argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int id128_main(int argc, char *argv[]) {
|
||||
static const Verb verbs[] = {
|
||||
{ "new", VERB_ANY, 1, 0, verb_new },
|
||||
{ "machine-id", VERB_ANY, 1, 0, verb_machine_id },
|
||||
{ "boot-id", VERB_ANY, 1, 0, verb_boot_id },
|
||||
{ "invocation-id", VERB_ANY, 1, 0, verb_invocation_id },
|
||||
{ "var-partition-uuid", VERB_ANY, 1, 0, verb_var_uuid },
|
||||
{ "show", VERB_ANY, VERB_ANY, 0, verb_show },
|
||||
{ "help", VERB_ANY, VERB_ANY, 0, verb_help },
|
||||
{}
|
||||
};
|
||||
|
||||
return dispatch_verb(argc, argv, verbs, NULL);
|
||||
}
|
||||
|
||||
static int run(int argc, char *argv[]) {
|
||||
int r;
|
||||
|
||||
log_setup();
|
||||
|
||||
r = parse_argv(argc, argv);
|
||||
char **args = NULL; /* unnecessary initialization to appease gcc <= 13 */
|
||||
r = parse_argv(argc, argv, &args);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
|
||||
return id128_main(argc, argv);
|
||||
return dispatch_verb_with_args(args, NULL);
|
||||
}
|
||||
|
||||
DEFINE_MAIN_FUNCTION(run);
|
||||
|
||||
Reference in New Issue
Block a user