]> git.itanic.dy.fi Git - glucose/blob - options.c
Fixed option handling.
[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         while (1) {
39                 c = getopt_long(argc, argv, short_options, long_options,
40                                 &option_index);
41
42                 if (c == -1)
43                         break;
44
45                 switch (c) {
46                 case 'd':
47                         opts->usbdev = optarg;
48                         break;
49                 case 'v':
50                         if (optarg)
51                                 opts->trace_level = atoi(optarg);
52                         else
53                                 opts->trace_level++;
54                 case 'o':
55                         opts->output_path = optarg;
56                         break;
57                 case 'f':
58                         if ( strcmp(optarg, "raw") == 0 ) {
59                                 opts->output_format = RAW;
60                         } else
61                         if ( strcmp(optarg, "clean") == 0 ) {
62                                 opts->output_format = CLEAN;
63                         } else
64                         if ( strcmp(optarg, "csv") == 0 ) {
65                                 opts->output_format = CSV;
66                         } else {
67                                 trace(0, "Unknown format type \"%s\", must be one of \"raw\", \"clean\", or \"csv\"\n", optarg );
68                                 return -1;
69                         }
70                         break;
71                 case '?':
72                         return -1;
73                 }
74         }
75
76         while (optind < argc) {
77                 /*
78                  * Some day we do something useful here with the rest
79                  * of the options.. Maybe
80                  */
81                 optind++;
82         }
83
84         return 0;
85 }