diff mbox series

[10/25] efw-downloader: add parser for sub commands

Message ID 20200821073111.134857-11-o-takashi@sakamocchi.jp (mailing list archive)
State New, archived
Headers show
Series alsa-tools: efw-downloader: add initial version of firmwre downloader for Echo Audio Fireworks devices | expand

Commit Message

Takashi Sakamoto Aug. 21, 2020, 7:30 a.m. UTC
This tool consists of sub commands for different functionalities.

This commit adds parser for sub commands. Actual implementation for
each sub command will be added in future commits.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
---
 efw-downloader/src/main.c | 42 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/efw-downloader/src/main.c b/efw-downloader/src/main.c
index 31ac349..499e67e 100644
--- a/efw-downloader/src/main.c
+++ b/efw-downloader/src/main.c
@@ -1,8 +1,48 @@ 
 // SPDX-License-Identifier: GPL-3.0-or-later
 // Copyright (c) 2020 Takashi Sakamoto
+#include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+
+static void print_help()
+{
+    printf("Usage\n"
+           "  efw-downloader SUBCOMMAND OPTIONS\n"
+           "\n"
+           "where:\n"
+           "  SUBCOMMAND:\n"
+           "    help:       print help\n"
+           "  OPTIONS:      optional arguments dependent on the subcommand\n");
+}
 
 int main(int argc, char **argv)
 {
-    return EXIT_SUCCESS;
+    static const struct {
+        const char *name;
+    size_t size;
+        int (*op)(int argc, char **argv);
+    } *entry, entries[] = {
+    };
+    const char *subcmd;
+    int i;
+
+    if (argc < 2) {
+        print_help();
+        return EXIT_FAILURE;
+    }
+    subcmd = argv[1];
+
+    for (i = 0; i < sizeof(entries) / sizeof(entries[0]); ++i) {
+        entry = entries + i;
+        if (strncmp(subcmd, entry->name, entry->size) == 0) {
+            entry = &entries[i];
+            break;
+        }
+    }
+    if (i == sizeof(entries) / sizeof(entries[0])) {
+        print_help();
+        return EXIT_FAILURE;
+    }
+
+    return entry->op(argc, argv);
 }