]> git.itanic.dy.fi Git - log-plotter/blob - options.c
data: Print fancy status line
[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 #include "debug.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("  -h, --help              show this help\n");
19
20         exit(0);
21 }
22
23 static void set_default_options(struct plotter_options *opts)
24 {
25         bzero(opts, sizeof(*opts));
26
27         opts->baud_rate = 128000;
28         opts->device_path = "/dev/ttyUSB0";
29 }
30
31 int read_args(int argc, char *argv[], struct plotter_options *opts)
32 {
33         int option_index = 0, c;
34         static struct option long_options[] = {
35                 { .val = 'd', .name = "device", .has_arg = 1, },
36                 { .val = 'o', .name = "output", .has_arg = 1 },
37                 { .val = 'b', .name = "baud", .has_arg = 1 },
38                 { .val = 'v', .name = "verbose", .has_arg = 2 },
39                 { .val = 'q', .name = "quiet", },
40                 { .val = 'h', .name = "help", },
41         };
42         char short_options[] = "d:o:b:vqh";
43
44         set_default_options(opts);
45
46         while (1) {
47                 c = getopt_long(argc, argv, short_options, long_options,
48                                 &option_index);
49
50                 if (c == -1)
51                         break;
52
53                 switch (c) {
54                 case 'd':
55                         opts->device_path = optarg;
56                         break;
57                 case 'o':
58                         opts->output_path = optarg;
59                         break;
60                 case 'b':
61                         opts->baud_rate = atoi(optarg);
62                         break;
63                 case 'v':
64                         trace_level++;
65                         pr_debug("Increased trace level to %d\n", trace_level);
66                         break;
67                 case 'q':
68                         trace_level--;
69                         pr_debug("Degreased trace level to %d\n", trace_level);
70                         break;
71                 case 'h':
72                         print_help_and_die(argv[0]);
73                         break;
74                 case '?':
75                         return -1;
76                 }
77         }
78
79         while (optind < argc) {
80                 /* The rest of the options, ignored */
81                 optind++;
82         }
83
84         return 0;
85 }