]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
delta: convert to the new option parser
authorZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Fri, 10 Apr 2026 19:41:34 +0000 (21:41 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Sat, 11 Apr 2026 11:30:42 +0000 (13:30 +0200)
--help for --diff= is changed from old-style "1|0" to "yes|no".

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

index df28a730a54e65fedbd2ef960396ad88646beb28..4a134ad2355f38fdcfc36da3770832bda6d4d40b 100644 (file)
@@ -1,6 +1,5 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
-#include <getopt.h>
 #include <unistd.h>
 
 #include "alloc-util.h"
 #include "errno-util.h"
 #include "extract-word.h"
 #include "fd-util.h"
+#include "format-table.h"
 #include "fs-util.h"
 #include "glyph-util.h"
 #include "hashmap.h"
 #include "log.h"
 #include "main-func.h"
 #include "nulstr-util.h"
+#include "options.h"
 #include "pager.h"
 #include "parse-argument.h"
 #include "path-util.h"
@@ -459,23 +460,26 @@ static int process_suffix_chop(const char *arg) {
 
 static int help(void) {
         _cleanup_free_ char *link = NULL;
+        _cleanup_(table_unrefp) Table *options = NULL;
         int r;
 
         r = terminal_urlify_man("systemd-delta", "1", &link);
         if (r < 0)
                 return log_oom();
 
+        r = option_parser_get_help_table(&options);
+        if (r < 0)
+                return r;
+
         printf("%s [OPTIONS...] [SUFFIX...]\n\n"
-               "Find overridden configuration files.\n\n"
-               "  -h --help           Show this help\n"
-               "     --version        Show package version\n"
-               "     --no-pager       Do not pipe output into a pager\n"
-               "     --diff[=1|0]     Show a diff when overridden files differ\n"
-               "  -t --type=LIST...   Only display a selected set of override types\n"
-               "\nSee the %s for details.\n",
-               program_invocation_short_name,
-               link);
+               "Find overridden configuration files.\n\n",
+               program_invocation_short_name);
 
+        r = table_print_or_warn(options);
+        if (r < 0)
+                return r;
+
+        printf("\nSee the %s for details.\n", link);
         return 0;
 }
 
@@ -509,66 +513,45 @@ static int parse_flags(const char *flag_str, int flags) {
         }
 }
 
-static int parse_argv(int argc, char *argv[]) {
-
-        enum {
-                ARG_NO_PAGER = 0x100,
-                ARG_DIFF,
-                ARG_VERSION
-        };
-
-        static const struct option options[] = {
-                { "help",      no_argument,       NULL, 'h'          },
-                { "version",   no_argument,       NULL, ARG_VERSION  },
-                { "no-pager",  no_argument,       NULL, ARG_NO_PAGER },
-                { "diff",      optional_argument, NULL, ARG_DIFF     },
-                { "type",      required_argument, NULL, 't'          },
-                {}
-        };
-
-        int c, r;
-
-        assert(argc >= 1);
+static int parse_argv(int argc, char *argv[], char ***ret_args) {
+        assert(argc >= 0);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "ht:", options, NULL)) >= 0)
+        OptionParser state = { argc, argv };
+        const char *arg;
+        int r;
 
+        FOREACH_OPTION(&state, c, &arg, /* on_error= */ return c)
                 switch (c) {
 
-                case 'h':
+                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 't': {
-                        int f;
-                        f = parse_flags(optarg, arg_flags);
-                        if (f < 0)
+                OPTION('t', "type", "TYPE...", "Only display a selected set of override types"):
+                        r = parse_flags(arg, arg_flags);
+                        if (r < 0)
                                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                        "Failed to parse flags field.");
-                        arg_flags = f;
+                        arg_flags = r;
                         break;
-                }
 
-                case ARG_DIFF:
-                        r = parse_boolean_argument("--diff", optarg, NULL);
+                OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "diff", "yes|no",
+                                  "Show a diff when overridden files differ"):
+                        r = parse_boolean_argument("--diff", arg, NULL);
                         if (r < 0)
                                 return r;
                         arg_diff = r;
                         break;
-
-                case '?':
-                        return -EINVAL;
-
-                default:
-                        assert_not_reached();
                 }
 
+        *ret_args = option_parser_get_args(&state);
         return 1;
 }
 
@@ -577,7 +560,8 @@ static int run(int argc, char *argv[]) {
 
         log_setup();
 
-        r = parse_argv(argc, argv);
+        char **args = NULL;
+        r = parse_argv(argc, argv, &args);
         if (r <= 0)
                 return r;
 
@@ -591,17 +575,16 @@ static int run(int argc, char *argv[]) {
 
         pager_open(arg_pager_flags);
 
-        if (optind < argc) {
-                for (int i = optind; i < argc; i++) {
-                        path_simplify(argv[i]);
+        if (!strv_isempty(args)) {
+                STRV_FOREACH(i, args) {
+                        path_simplify(*i);
 
-                        k = process_suffix_chop(argv[i]);
+                        k = process_suffix_chop(*i);
                         if (k < 0)
                                 r = k;
                         else
                                 n_found += k;
                 }
-
         } else {
                 k = process_suffixes(NULL);
                 if (k < 0)