commit: 1691cd389846d34cdcba39514d71a5fb5b16a2f5
parent 1505a3cf836653af01e83dafe26e952be39bccab
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Thu, 24 Jul 2025 22:34:31 +0200
cmd/cut: add support for long options
Diffstat:
1 file changed, 22 insertions(+), 0 deletions(-)
diff --git a/cmd/cut.c b/cmd/cut.c
@@ -4,6 +4,7 @@
#define _POSIX_C_SOURCE 202405L
+#include "../config.h"
#include "../lib/reallocarray.h"
#include "../libutils/getopt_nolong.h"
@@ -17,6 +18,9 @@
#include <string.h> // strerror
#include <unistd.h> // getopt
#include <wchar.h>
+#ifdef HAS_GETOPT_LONG
+#include <getopt.h>
+#endif
#undef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
@@ -381,7 +385,25 @@ main(int argc, char *argv[])
}
errno = 0;
+#ifdef HAS_GETOPT_LONG
+ // Strictly for GNUisms compatibility so no long-only options
+ // clang-format off
+ static struct option opts[] = {
+ {"bytes", required_argument, NULL, 'b'},
+ {"characters", required_argument, NULL, 'c'},
+ {"delimiter", required_argument, NULL, 'd'},
+ {"fields", required_argument, NULL, 'f'},
+ {"only-delimited", no_argument, NULL, 's'},
+ {"zero-terminated", no_argument, NULL, 'z'},
+ {0, 0, 0, 0},
+ };
+ // clang-format on
+
+ // Need + as first character to get POSIX-style option parsing
+ for(int c = -1; (c = getopt_long(argc, argv, "+:b:c:d:f:nsz", opts, NULL)) != -1;)
+#else
for(int c = -1; (c = getopt_nolong(argc, argv, ":b:c:d:f:nsz")) != -1;)
+#endif
{
switch(c)
{