cdrom_id: convert to the new option parser

The commandline check is tightened to reject extra arguments.

Co-developed-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-04-26 12:34:13 +02:00
parent 9286f1f299
commit 3ad76abc43
+42 -36
View File
@@ -4,7 +4,6 @@
*/
#include <fcntl.h>
#include <getopt.h>
#include <linux/cdrom.h>
#include <scsi/sg.h>
#include <sys/ioctl.h>
@@ -13,11 +12,15 @@
#include "build.h"
#include "errno-util.h"
#include "fd-util.h"
#include "format-table.h"
#include "help-util.h"
#include "main-func.h"
#include "options.h"
#include "random-util.h"
#include "sort-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "time-util.h"
#include "udev-util.h"
#include "unaligned.h"
@@ -898,60 +901,63 @@ static void print_properties(const Context *c) {
}
static int help(void) {
printf("%s [OPTIONS...] DEVICE\n\n"
" -l --lock-media Lock the media (to enable eject request events)\n"
" -u --unlock-media Unlock the media\n"
" -e --eject-media Eject the media\n"
" -d --debug Print debug messages to stderr\n"
" -h --help Show this help text\n"
" --version Show package version\n",
program_invocation_short_name);
_cleanup_(table_unrefp) Table *options = NULL;
int r;
return 0;
r = option_parser_get_help_table(&options);
if (r < 0)
return r;
help_cmdline("[OPTIONS...] DEVICE");
help_section("Options:");
return table_print_or_warn(options);
}
static int parse_argv(int argc, char *argv[]) {
static const struct option options[] = {
{ "lock-media", no_argument, NULL, 'l' },
{ "unlock-media", no_argument, NULL, 'u' },
{ "eject-media", no_argument, NULL, 'e' },
{ "debug", no_argument, NULL, 'd' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' },
{}
};
int c;
assert(argc >= 0);
assert(argv);
while ((c = getopt_long(argc, argv, "deluh", options, NULL)) >= 0)
OptionParser state = { argc, argv };
const char *arg;
FOREACH_OPTION(&state, c, &arg, /* on_error= */ return c)
switch (c) {
case 'l':
OPTION_COMMON_HELP:
return help();
OPTION_COMMON_VERSION:
return version();
OPTION('l', "lock-media", NULL,
"Lock the media (to enable eject request events)"):
arg_lock = true;
break;
case 'u':
OPTION('u', "unlock-media", NULL,
"Unlock the media"):
arg_unlock = true;
break;
case 'e':
OPTION('e', "eject-media", NULL,
"Eject the media"):
arg_eject = true;
break;
case 'd':
OPTION('d', "debug", NULL,
"Print debug messages to stderr"):
log_set_target(LOG_TARGET_CONSOLE);
log_set_max_level(LOG_DEBUG);
log_open();
break;
case 'h':
return help();
case 'v':
return version();
case '?':
return -EINVAL;
default:
assert_not_reached();
}
arg_node = argv[optind];
if (!arg_node)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No device specified.");
char **args = option_parser_get_args(&state);
if (strv_length(args) != 1)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Need exactly one DEVICE argument.");
arg_node = args[0];
return 1;
}