]> git.cworth.org Git - ttt/blob - src/ttt-args.c
2005-11-07 Carl Worth <cworth@cworth.org>
[ttt] / src / ttt-args.c
1 /* ttt-args.c - Parse command-line arguments for ttt using getopt
2  *
3  * Copyright © 2005 Carl Worth
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Author: Carl Worth <cworth@cworth.org>
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <libgen.h>
26 #include <getopt.h>
27
28 #include "ttt-args.h"
29
30 static const char TTT_ARGS_PROGRAM_VERSION[] = VERSION;
31 static const char TTT_ARGS_PROGRAM_DESCRIPTION[] = "client-server tic-tac-toe game";
32 static const char TTT_ARGS_PROGRAM_BUG_ADDRESS[] = "<cworth@cworth.org>";
33
34 /* XXX: SAMPLE: */
35 static char TTT_ARGS_PROGRAM_ARGDOC[] = "<file>";
36
37 /* XXX: getopt is rather annoying in that you must maintain a
38  * string-encoding of the options in addition to the table. For
39  * example, to enable the sample display option below, one would have
40  * to add "d:" to the optstring.
41  *
42  * A useful exercise would be to write a function to generate the
43  * optstring from the table.
44  *
45  * The other annoying thing is that the table does not include help
46  * strings, so the args_help function below must also be maintained
47  * manually.
48  */
49 static char ttt_args_optstring[] = "hV";
50 static struct option ttt_args_options[] = {
51     /* name,            has_arg,        flag,   val */
52     /* XXX: SAMPLE:
53     {"display",         1,              0,      'd'},
54     */
55     {"help",            0,              0,      'h'},
56     {"version",         0,              0,      'V'},
57     { 0 }
58 };
59
60 static void
61 ttt_args_help (const char *argv0)
62 {
63     printf ("Usage: %s [OPTION] %s\n", argv0, TTT_ARGS_PROGRAM_ARGDOC);
64     printf ("%s - %s\n", argv0, TTT_ARGS_PROGRAM_DESCRIPTION);
65     puts ("");
66     printf ("  -h, --help\t\tGive this help list\n");
67     printf ("  -V, --version\t\tPrint program version\n");
68     /* XXX: SAMPLE:
69     printf ("  -d, --display=DISPLAY\tX server to connect to");
70     */
71 }
72
73 static void
74 ttt_args_usage (const char *argv0)
75 {
76     printf ("Usage: %s [OPTION] %s\n", argv0, TTT_ARGS_PROGRAM_ARGDOC);
77     printf ("Try `%s --help' for more information.\n", argv0);
78 }
79
80 int
81 ttt_args_parse(ttt_args_t *args, int argc, char *argv[], int *args_first)
82 {
83     char *argv0_copy = strdup (argv[0]);
84     char *argv0 = basename (argv0_copy);
85
86     int c;
87
88     /* XXX: SAMPLE:
89     args->display = NULL;
90     */
91
92     while (1) {
93         c = getopt_long (argc, argv, ttt_args_optstring, ttt_args_options, NULL);
94         if (c == -1)
95             break;
96
97         switch (c) {
98         /* XXX: SAMPLE:
99         case 'd':
100             args->display = optarg;
101             break;
102         */
103         case 'V':
104             printf ("%s\n", VERSION);
105             exit (0);
106             break;
107
108         case 'h':
109             ttt_args_help (argv0);
110             exit (0);
111             break;
112
113         case '?':
114             ttt_args_help (argv0);
115             exit (1);
116             break;
117
118         default:
119             fprintf (stderr, "Unhandled option `%c'\n", c);
120             break;
121         }
122     }
123
124     /* XXX: SAMPLE:
125     if (argc - optind == 1) {
126         args->file = argv[optind];
127     } else {
128         ttt_args_usage (argv0);
129     }
130     */
131
132     if (args_first)
133         *args_first = optind;
134
135     free (argv0_copy);
136
137     return 0;
138 }