]> git.itanic.dy.fi Git - log-plotter/blob - options.c
bal.plot: Convert volts to millivolts correctly
[log-plotter] / options.c
1 #include <getopt.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "config.h"
7 #include "trace.h"
8
9 void print_help_and_die(const char *exec_name)
10 {
11         pr_info("Usage: %s [option]\n", exec_name);
12         pr_info("  -d, --device=DEVICE     path to the serial device\n");
13         pr_info("                          default: /dev/ttyUSB0\n");
14         pr_info("  -b, --baud=BAUD         device baud rate, default 128000\n");
15         pr_info("  -o, --output            logfile path, stdout only if omited\n");
16         pr_info("  -v, --verbose           increase verbosity\n");
17         pr_info("  -q, --quiet             decrease verbosity\n");
18         pr_info("  -c, --config=PATH       config file path\n");
19         pr_info("  -h, --help              show this help\n");
20
21         exit(0);
22 }
23
24 static void set_default_options(struct plotter_config *opts)
25 {
26         bzero(opts, sizeof(*opts));
27
28         opts->baudrate = 128000;
29         opts->device_path = "/dev/ttyUSB0";
30 }
31
32 int read_args(int argc, char *argv[], struct plotter_config *opts)
33 {
34         int option_index = 0, c;
35         static struct option long_options[] = {
36                 { .val = 'd', .name = "device", .has_arg = 1, },
37                 { .val = 'o', .name = "output", .has_arg = 1 },
38                 { .val = 'b', .name = "baud", .has_arg = 1 },
39                 { .val = 'v', .name = "verbose", .has_arg = 2 },
40                 { .val = 'q', .name = "quiet", },
41                 { .val = 'c', .name = "config", .has_arg = 1 },
42                 { .val = 'h', .name = "help", },
43         };
44         char short_options[] = "d:o:b:vqc:h";
45
46         set_default_options(opts);
47
48         while (1) {
49                 c = getopt_long(argc, argv, short_options, long_options,
50                                 &option_index);
51
52                 if (c == -1)
53                         break;
54
55                 switch (c) {
56                 case 'd':
57                         opts->device_path = optarg;
58                         break;
59                 case 'o':
60                         opts->log_path = optarg;
61                         break;
62                 case 'b':
63                         opts->baudrate = atoi(optarg);
64                         break;
65                 case 'v':
66                         trace_level++;
67                         pr_debug("Increased trace level to %d\n", trace_level);
68                         break;
69                 case 'q':
70                         trace_level--;
71                         pr_debug("Degreased trace level to %d\n", trace_level);
72                         break;
73                 case 'c':
74                         opts->config_file_path = optarg;
75                         break;
76                 case 'h':
77                         print_help_and_die(argv[0]);
78                         break;
79                 case '?':
80                         return -1;
81                 }
82         }
83
84         while (optind < argc) {
85                 /* The rest of the options, ignored */
86                 optind++;
87         }
88
89         return 0;
90 }