Welcome to little lamb

Code » ssp » commit 0e13c02

export: Add -C/--no-comments

author Olivier Brunel
2023-04-25 12:22:12 UTC
committer Olivier Brunel
2023-04-25 15:02:08 UTC
parent fa435ffbd0c9a1fd3df217c4c8264d51faca3214

export: Add -C/--no-comments

src/ssp/export.c +48 -4

diff --git a/src/ssp/export.c b/src/ssp/export.c
index 5b43c71..9554c3b 100644
--- a/src/ssp/export.c
+++ b/src/ssp/export.c
@@ -5,17 +5,61 @@
 #include <limb/command.h>
 #include <limb/exitcode.h>
 #include <limb/output.h>
+#include <limb/parseopt.h>
 #include "ssp.h"
 
+enum {
+    OPT_NO_COMMENTS = 1 << 0,
+};
 
-COMMAND(export, "Export entries", "[<pattern>]", NULL);
+struct export {
+    int options;
+};
+
+COMMAND(export, "Export entries", "[OPTION..] [<pattern>]",
+" -C, --no-comments                     Do not export comments\n"
+);
+
+static int
+parse_cmdline(int argc, const char *argv[], const char usage[], struct export *ctx)
+{
+    const struct option options[] = {
+        OPTION_ARG_NONE('C', "no-comments",             0, OPTID_SHORTOPT),
+        OPTION_DONE
+    };
+    struct parseopt po = { 0 };
+
+    int c;
+    while ((c = parseopt(argc, argv, options, 0, &po))) switch (c) {
+        case 'C':
+            ctx->options |= OPT_NO_COMMENTS;
+            break;
+        case -1:
+            diecmdusage(EX_USAGE, usage, &command_export);
+        default:
+            die(EX_SOFTWARE, "unexpected return value ", PUTMSG_INT(c), " from parseopt");
+    };
+
+    return PO_CUR(&po);
+}
 
 int
 export_main(int argc, const char *argv[], const char *env[], const char usage[], void *ctx_)
 {
-    if (argc > 2)
+    struct export export = { 0 };
+
+    int i = parse_cmdline(argc, argv, usage, &export);
+
+    if (argc > i + 1) {
+        warn("too many arguments");
         diecmdusage(EX_USAGE, usage, &command_export);
+    } else if (argc == i) {
+        argc = i = 0;
+    } else {
+        argc = 1;
+    }
 
-    const char *av[] = { NULL, "--format", argv[1] };
-    return run_command("list", 1 + argc, av, env, ctx_);
+    int C = !!(export.options & OPT_NO_COMMENTS);
+    const char *av[] = { NULL, "--format", (C) ? "--no-comments" : argv[i], argv[i] };
+    return run_command("list", 2 + C + argc, av, env, ctx_);
 }