]> git.itanic.dy.fi Git - glucose/blob - options.c
7bd5b9bbfd9700a081a8753d46dc379c01903bf2
[glucose] / options.c
1 /*
2  * Copyright (C) 2012 Timo Kokkonen <timo.t.kokkonen@iki.fi>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */
19
20 #include <getopt.h>
21 #include <string.h>
22 #include <stdlib.h>
23
24 #include "options.h"
25 #include "utils.h"
26
27 int read_args(int argc, char *argv[], struct user_options *opts)
28 {
29         int option_index = 0, c;
30         static struct option long_options[] = {
31                 { .val = 'd', .name = "device", .has_arg = 1, },
32                 { .val = 'v', .name = "verbose", .has_arg = 2 },
33                 { .val = 'o', .name = "output", .has_arg = 1 },
34                 { .val = 'f', .name = "format", .has_arg = 1, },
35         };
36         char short_options[] = "d:v:o:f:";
37
38         memset(opts, 0, sizeof(*opts));
39
40         while (1) {
41                 c = getopt_long(argc, argv, short_options, long_options,
42                                 &option_index);
43
44                 if (c == -1)
45                         break;
46
47                 switch (c) {
48                 case 'd':
49                         opts->usbdev = optarg;
50                         break;
51                 case 'v':
52                         if (optarg)
53                                 opts->trace_level = atoi(optarg);
54                         else
55                                 opts->trace_level++;
56                 case 'o':
57                         opts->output_path = optarg;
58                         break;
59                 case 'f':
60                         if ( strcmp(optarg, "raw") == 0 ) {
61                                 opts->output_format = RAW;
62                         } else
63                         if ( strcmp(optarg, "clean") == 0 ) {
64                                 opts->output_format = CLEAN;
65                         } else
66                         if ( strcmp(optarg, "csv") == 0 ) {
67                                 opts->output_format = CSV;
68                         } else {
69                                 trace(0, "Unknown format type \"%s\", must be one of \"raw\", \"clean\", or \"csv\"\n", optarg );
70                                 return -1;
71                         }
72                         break;
73                 case '?':
74                         return -1;
75                 }
76         }
77
78         while (optind < argc) {
79                 /*
80                  * Some day we do something useful here with the rest
81                  * of the options.. Maybe
82                  */
83                 optind++;
84         }
85
86         return 0;
87 }