udev: implement --version in all builtins

Those are separate binaries, and occasionally people will get a misplaced
binary that doesn't match the rest of the installed system and be confused, so
it good to be able to check the version. It is also nice to have the same
interface in all binaries.

Note that we usually use a separate 'enum ARG_VERSION = 0x100' for an option
without a short name. We can use a less verbose approach of simply taking any
unused letter, which works just as well and even the compiler would warn us
if we tried to use the letter in another place. This way we avoid a few lines
of boilerplate.

The help texts are adjusted to have an empty line between the synopsis and
option list, and no empty lines after the option list.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2023-03-13 18:12:04 +01:00
parent bd36d0281a
commit 5356761da6
7 changed files with 49 additions and 19 deletions

View file

@ -739,6 +739,7 @@
</varlistentry>
<xi:include href="standard-options.xml" xpointer="help" />
<xi:include href="standard-options.xml" xpointer="version" />
</variablelist>
</refsect2>

View file

@ -23,6 +23,7 @@
#include <sys/types.h>
#include <unistd.h>
#include "build.h"
#include "device-nodes.h"
#include "fd-util.h"
#include "log.h"
@ -361,8 +362,9 @@ static int disk_identify(int fd,
static int parse_argv(int argc, char *argv[]) {
static const struct option options[] = {
{ "export", no_argument, NULL, 'x' },
{ "help", no_argument, NULL, 'h' },
{ "export", no_argument, NULL, 'x' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' },
{}
};
int c;
@ -375,9 +377,12 @@ static int parse_argv(int argc, char *argv[]) {
case 'h':
printf("%s [OPTIONS...] DEVICE\n\n"
" -x --export Print values as environment keys\n"
" -h --help Show this help text\n",
" -h --help Show this help text\n"
" --version Show package version\n",
program_invocation_short_name);
return 0;
case 'v':
return version();
case '?':
return -EINVAL;
default:

View file

@ -10,6 +10,7 @@
#include <sys/ioctl.h>
#include <unistd.h>
#include "build.h"
#include "fd-util.h"
#include "main-func.h"
#include "memory-util.h"
@ -897,13 +898,13 @@ static void print_properties(const Context *c) {
}
static int help(void) {
printf("Usage: %s [options] <device>\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 print this help text\n"
"\n",
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);
return 0;
@ -916,6 +917,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "eject-media", no_argument, NULL, 'e' },
{ "debug", no_argument, NULL, 'd' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' },
{}
};
int c;
@ -938,6 +940,8 @@ static int parse_argv(int argc, char *argv[]) {
break;
case 'h':
return help();
case 'v':
return version();
case '?':
return -EINVAL;
default:

View file

@ -45,6 +45,7 @@
#include <getopt.h>
#include "alloc-util.h"
#include "build.h"
#include "fileio.h"
#include "main-func.h"
#include "string-util.h"
@ -638,9 +639,10 @@ static int legacy_decode(const uint8_t *buf, const char *devmem, bool no_file_of
}
static int help(void) {
printf("Usage: %s [options]\n"
" -F,--from-dump FILE read DMI information from a binary file\n"
" -h,--help print this help text\n\n",
printf("%s [OPTIONS...]\n\n"
" -F --from-dump FILE Read DMI information from a binary file\n"
" -h --help Show this help text\n"
" --version Show package version\n",
program_invocation_short_name);
return 0;
}
@ -650,6 +652,7 @@ static int parse_argv(int argc, char * const *argv) {
{ "from-dump", required_argument, NULL, 'F' },
{ "version", no_argument, NULL, 'V' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' },
{}
};
int c;
@ -666,6 +669,8 @@ static int parse_argv(int argc, char * const *argv) {
return help();
case '?':
return -EINVAL;
case 'v':
return version();
default:
assert_not_reached();
}

View file

@ -15,6 +15,7 @@
#include <sys/types.h>
#include <unistd.h>
#include "build.h"
#include "device-private.h"
#include "device-util.h"
#include "fd-util.h"
@ -30,7 +31,8 @@ static const char *arg_device = NULL;
static int parse_argv(int argc, char *argv[]) {
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' },
{}
};
int c;
@ -39,9 +41,12 @@ static int parse_argv(int argc, char *argv[]) {
switch (c) {
case 'h':
printf("%s [OPTIONS...] SYSFS_PATH\n\n"
" -h --help Show this help text\n",
" -h --help Show this help text\n"
" --version Show package version\n",
program_invocation_short_name);
return 0;
case 'v':
return version();
case '?':
return -EINVAL;
default:

View file

@ -30,6 +30,7 @@
#include <unistd.h>
#include "alloc-util.h"
#include "build.h"
#include "fd-util.h"
#include "main-func.h"
#include "mtd_probe.h"
@ -38,7 +39,8 @@ static const char *arg_device = NULL;
static int parse_argv(int argc, char *argv[]) {
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' },
{}
};
int c;
@ -47,9 +49,12 @@ static int parse_argv(int argc, char *argv[]) {
switch (c) {
case 'h':
printf("%s /dev/mtd[n]\n\n"
" -h --help Show this help text\n",
" -h --help Show this help text\n"
" --version Show package version\n",
program_invocation_short_name);
return 0;
case 'v':
return version();
case '?':
return -EINVAL;
default:

View file

@ -26,6 +26,7 @@
#include <unistd.h>
#include <linux/videodev2.h>
#include "build.h"
#include "fd-util.h"
#include "main-func.h"
@ -33,7 +34,8 @@ static const char *arg_device = NULL;
static int parse_argv(int argc, char *argv[]) {
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' },
{}
};
int c;
@ -43,9 +45,12 @@ static int parse_argv(int argc, char *argv[]) {
case 'h':
printf("%s [OPTIONS...] DEVICE\n\n"
"Video4Linux device identification.\n\n"
" -h --help Show this help text\n",
" -h --help Show this help text\n"
" --version Show package version\n",
program_invocation_short_name);
return 0;
case 'v':
return version();
case '?':
return -EINVAL;
default: