]> git.cworth.org Git - grrobot/blob - src/args.c
1498dbb62ec4164634b408c36294c70dddbf4b56
[grrobot] / src / args.c
1 /* args.c - Parse command-line arguments for grrobot using argp
2  *
3  * Copyright © 2003 Carl Worth
4  *
5  * Permission to use, copy, modify, distribute, and sell this software
6  * and its documentation for any purpose is hereby granted without
7  * fee, provided that the above copyright notice appear in all copies
8  * and that both that copyright notice and this permission notice
9  * appear in supporting documentation, and that the name of Carl Worth
10  * not be used in advertising or publicity pertaining to distribution
11  * of the software without specific, written prior permission.
12  * Carl Worth makes no representations about the suitability of this
13  * software for any purpose.  It is provided "as is" without express
14  * or implied warranty.
15  * 
16  * CARL WORTH DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
18  * NO EVENT SHALL CARL WORTH BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
20  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
21  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Author: Carl Worth <carl@theworths.org>
25  */
26
27 #include <stdlib.h>
28 #include <argp.h>
29
30 #include "args.h"
31
32 const char *argp_program_version = "grrobot " VERSION;
33
34 const char *argp_program_bug_address = "<carl@theworths.org>";
35
36 static char doc[] = "grrobot - Ricochet Robot using GTK+ and Xr";
37
38 static char args_doc[] = "[file]";
39
40 static struct option options[] = {
41     {"host",            required_argument,      0,      'h'},
42     {"port",            required_argument,      0,      'p'},
43     {"user",            required_argument,      0,      'u'},
44     {"game",            required_argument,      0,      'g'},
45     {"watch",           no_argument,            0,      'w'},
46     {0,                 0,                      0,      0}
47 };
48
49 static error_t
50 parse_opt (int key, char *arg, struct argp_state *state)
51 {
52     struct args *args = state->input;
53
54     switch (key) {
55     case 'h':
56         args->host = arg;
57         break;
58     case 'p':
59         args->port = arg;
60         break;
61     case 'u':
62         args->user = arg;
63         break;
64     case 'g':
65         args->game = arg;
66         break;
67     case 'w':
68         args->watch = 1;
69         break;
70
71     case ARGP_KEY_ARG:
72         args->file = arg;
73         break;
74
75     default:
76         return ARGP_ERR_UNKNOWN;
77     }
78
79     return 0;
80 }
81             
82 static struct argp argp = { options, parse_opt, args_doc, doc };
83
84 error_t
85 args_parse(args_t *args, int argc, char *argv[])
86 {
87     args->host = getenv ("RR_HOST");
88     if (args->host == NULL)
89         args->host = ARGS_HOST_DEFAULT;
90     args->port = getenv ("RR_PORT");
91     if (args->port == NULL)
92         args->port = ARGS_PORT_DEFAULT;
93     args->user = getenv ("USER");
94     if (args->user == NULL)
95         args->user = ARGS_USER_DEFAULT;
96     args->game = getenv ("RR_GAME");
97     if (args->game == NULL)
98         args->game = ARGS_GAME_DEFAULT;
99     args->watch = 0;
100
101     args->file = NULL;
102     return 0;
103     return argp_parse (&argp, argc, argv,
104                        ARGP_LONG_ONLY,
105                        NULL, args);
106 }