]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
userdbctl: use table to parse --output=
authorDavid Tardon <dtardon@redhat.com>
Fri, 26 Sep 2025 14:11:17 +0000 (16:11 +0200)
committerDavid Tardon <dtardon@redhat.com>
Thu, 2 Oct 2025 14:13:40 +0000 (16:13 +0200)
src/userdb/userdbctl.c

index da3901a07eb91667bb7195c22df958d78651bf3d..4fbf8f671cdfe2d78e724fcd98ddec991fb790ea 100644 (file)
@@ -26,6 +26,7 @@
 #include "pretty-print.h"
 #include "recurse-dir.h"
 #include "socket-util.h"
+#include "string-table.h"
 #include "string-util.h"
 #include "strv.h"
 #include "uid-classification.h"
 #include "verbs.h"
 #include "virt.h"
 
-static enum {
+typedef enum {
         OUTPUT_CLASSIC,
         OUTPUT_TABLE,
         OUTPUT_FRIENDLY,
         OUTPUT_JSON,
+        _OUTPUT_MAX,
         _OUTPUT_INVALID = -EINVAL,
-} arg_output = _OUTPUT_INVALID;
+} Output;
 
+static Output arg_output = _OUTPUT_INVALID;
 static PagerFlags arg_pager_flags = 0;
 static bool arg_legend = true;
 static char** arg_services = NULL;
@@ -61,6 +64,15 @@ static sd_json_variant *arg_from_file = NULL;
 STATIC_DESTRUCTOR_REGISTER(arg_services, strv_freep);
 STATIC_DESTRUCTOR_REGISTER(arg_from_file, sd_json_variant_unrefp);
 
+static const char *output_table[_OUTPUT_MAX] = {
+        [OUTPUT_CLASSIC]  = "classic",
+        [OUTPUT_TABLE]    = "table",
+        [OUTPUT_FRIENDLY] = "friendly",
+        [OUTPUT_JSON]     = "json",
+};
+
+DEFINE_PRIVATE_STRING_TABLE_LOOKUP(output, Output);
+
 static const char *user_disposition_to_color(UserDisposition d) {
         assert(d >= 0);
         assert(d < _USER_DISPOSITION_MAX);
@@ -1652,22 +1664,12 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_OUTPUT:
-                        if (streq(optarg, "classic"))
-                                arg_output = OUTPUT_CLASSIC;
-                        else if (streq(optarg, "friendly"))
-                                arg_output = OUTPUT_FRIENDLY;
-                        else if (streq(optarg, "json"))
-                                arg_output = OUTPUT_JSON;
-                        else if (streq(optarg, "table"))
-                                arg_output = OUTPUT_TABLE;
-                        else if (streq(optarg, "help")) {
-                                puts("classic\n"
-                                     "friendly\n"
-                                     "json\n"
-                                     "table");
-                                return 0;
-                        } else
-                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid --output= mode: %s", optarg);
+                        if (streq(optarg, "help"))
+                                return DUMP_STRING_TABLE(output, Output, _OUTPUT_MAX);
+
+                        arg_output = output_from_string(optarg);
+                        if (arg_output < 0)
+                                return log_error_errno(arg_output, "Invalid --output= mode: %s", optarg);
 
                         arg_json_format_flags = arg_output == OUTPUT_JSON ? SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO : SD_JSON_FORMAT_OFF;
                         break;