]> git.itanic.dy.fi Git - log-plotter/blobdiff - options.c
Add command line parsing
[log-plotter] / options.c
diff --git a/options.c b/options.c
new file mode 100644 (file)
index 0000000..38b29a3
--- /dev/null
+++ b/options.c
@@ -0,0 +1,56 @@
+#include <getopt.h>
+#include <string.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "options.h"
+
+static void set_default_options(struct plotter_options *opts)
+{
+       bzero(opts, sizeof(*opts));
+
+       opts->baud_rate = 128000;
+       opts->device_path = "/dev/ttyUSB0";
+}
+
+int read_args(int argc, char *argv[], struct plotter_options *opts)
+{
+       int option_index = 0, c;
+       static struct option long_options[] = {
+               { .val = 'd', .name = "device", .has_arg = 1, },
+               { .val = 'o', .name = "output", .has_arg = 1 },
+               { .val = 'o', .name = "baud", .has_arg = 1 },
+       };
+       char short_options[] = "d:o:b:";
+
+       set_default_options(opts);
+
+       while (1) {
+               c = getopt_long(argc, argv, short_options, long_options,
+                               &option_index);
+
+               if (c == -1)
+                       break;
+
+               switch (c) {
+               case 'd':
+                       opts->device_path = optarg;
+                       break;
+               case 'o':
+                       opts->output_path = optarg;
+                       break;
+               case 'b':
+                       opts->baud_rate = atoi(optarg);
+                       break;
+               case '?':
+                       return -1;
+               }
+       }
+
+       while (optind < argc) {
+               /* The rest of the options, ignored */
+               optind++;
+       }
+
+       return 0;
+}