]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
battery-check: convert to the new option parser
authorZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Sun, 5 Apr 2026 15:38:46 +0000 (17:38 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Tue, 7 Apr 2026 08:55:53 +0000 (10:55 +0200)
--help is identical except for whitespace changes.

Co-developed-by: Claude Opus 4.6 <noreply@anthropic.com>
src/battery-check/battery-check.c

index 43ff0e53e0386e652c4122c2a76988e27fdb699e..706a7d869c53ad670c8f4a26a003b5084fbe0328 100644 (file)
@@ -1,6 +1,5 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
-#include <getopt.h>
 #include <sys/socket.h>         /* IWYU pragma: keep */
 
 #include "sd-messages.h"
@@ -9,9 +8,11 @@
 #include "battery-util.h"
 #include "build.h"
 #include "fd-util.h"
+#include "format-table.h"
 #include "glyph-util.h"
 #include "log.h"
 #include "main-func.h"
+#include "options.h"
 #include "plymouth-util.h"
 #include "pretty-print.h"
 #include "proc-cmdline.h"
@@ -27,22 +28,28 @@ static bool arg_doit = true;
 
 static int help(void) {
         _cleanup_free_ char *link = NULL;
+        _cleanup_(table_unrefp) Table *options = NULL;
         int r;
 
         r = terminal_urlify_man("systemd-battery-check", "8", &link);
         if (r < 0)
                 return log_oom();
 
+        r = option_parser_get_help_table(&options);
+        if (r < 0)
+                return r;
+
         printf("%s\n\n"
-               "%sCheck battery level to see whether there's enough charge.%s\n\n"
-               "   -h --help            Show this help\n"
-               "      --version         Show package version\n"
-               "\nSee the %s for details.\n",
+               "%sCheck battery level to see whether there's enough charge.%s\n\n",
                program_invocation_short_name,
                ansi_highlight(),
-               ansi_normal(),
-               link);
+               ansi_normal());
+
+        r = table_print_or_warn(options);
+        if (r < 0)
+                return r;
 
+        printf("\nSee the %s for details.\n", link);
         return 0;
 }
 
@@ -70,41 +77,23 @@ static int plymouth_send_message(const char *mode, const char *message) {
         return 0;
 }
 
-static int parse_argv(int argc, char * argv[]) {
-
-        enum {
-                ARG_VERSION = 0x100,
-        };
-
-        static const struct option options[] = {
-                { "help",    no_argument, NULL, 'h'         },
-                { "version", no_argument, NULL, ARG_VERSION },
-                {}
-        };
-
-        int c;
-
+static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
+        OptionParser state = { argc, argv };
 
+        FOREACH_OPTION(&state, c, /* ret_a= */ NULL, /* on_error= */ return c)
                 switch (c) {
 
-                case 'h':
+                OPTION_COMMON_HELP:
                         return help();
 
-                case ARG_VERSION:
+                OPTION_COMMON_VERSION:
                         return version();
-
-                case '?':
-                        return -EINVAL;
-
-                default:
-                        assert_not_reached();
                 }
 
-        if (optind < argc)
+        if (option_parser_get_n_args(&state) != 0)
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                        "%s takes no argument.",
                                        program_invocation_short_name);