]> git.itanic.dy.fi Git - log-plotter/blob - options.c
38b29a3abd48f645c378713e5c0e850c75897da4
[log-plotter] / options.c
1 #include <getopt.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "options.h"
7
8 static void set_default_options(struct plotter_options *opts)
9 {
10         bzero(opts, sizeof(*opts));
11
12         opts->baud_rate = 128000;
13         opts->device_path = "/dev/ttyUSB0";
14 }
15
16 int read_args(int argc, char *argv[], struct plotter_options *opts)
17 {
18         int option_index = 0, c;
19         static struct option long_options[] = {
20                 { .val = 'd', .name = "device", .has_arg = 1, },
21                 { .val = 'o', .name = "output", .has_arg = 1 },
22                 { .val = 'o', .name = "baud", .has_arg = 1 },
23         };
24         char short_options[] = "d:o:b:";
25
26         set_default_options(opts);
27
28         while (1) {
29                 c = getopt_long(argc, argv, short_options, long_options,
30                                 &option_index);
31
32                 if (c == -1)
33                         break;
34
35                 switch (c) {
36                 case 'd':
37                         opts->device_path = optarg;
38                         break;
39                 case 'o':
40                         opts->output_path = optarg;
41                         break;
42                 case 'b':
43                         opts->baud_rate = atoi(optarg);
44                         break;
45                 case '?':
46                         return -1;
47                 }
48         }
49
50         while (optind < argc) {
51                 /* The rest of the options, ignored */
52                 optind++;
53         }
54
55         return 0;
56 }