1 /* args.c - Parse command-line arguments for ttt using getopt
3 * Copyright © 2005 Carl Worth
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)
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.
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.
19 * Author: Carl Worth <cworth@cworth.org>
30 static const char ARGS_PROGRAM_VERSION[] = VERSION;
31 static const char ARGS_PROGRAM_DESCRIPTION[] = "client-server tic-tac-toe game";
32 static const char ARGS_PROGRAM_BUG_ADDRESS[] = "<cworth@cworth.org>";
35 static char ARGS_PROGRAM_ARGDOC[] = "<file>";
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.
42 * A useful exercise would be to write a function to generate the
43 * optstring from the table.
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
49 static char args_optstring[] = "hV";
50 static struct option args_options[] = {
51 /* name, has_arg, flag, val */
53 {"display", 1, 0, 'd'},
56 {"version", 0, 0, 'V'},
61 args_help (const char *argv0)
63 printf ("Usage: %s [OPTION] %s\n", argv0, ARGS_PROGRAM_ARGDOC);
64 printf ("%s - %s\n", argv0, ARGS_PROGRAM_DESCRIPTION);
66 printf (" -h, --help\t\tGive this help list\n");
67 printf (" -V, --version\t\tPrint program version\n");
69 printf (" -d, --display=DISPLAY\tX server to connect to");
74 args_usage (const char *argv0)
76 printf ("Usage: %s [OPTION] %s\n", argv0, ARGS_PROGRAM_ARGDOC);
77 printf ("Try `%s --help' for more information.\n", argv0);
81 args_parse(args_t *args, int argc, char *argv[], int *args_first)
83 char *argv0_copy = strdup (argv[0]);
84 char *argv0 = basename (argv0_copy);
93 c = getopt_long (argc, argv, args_optstring, args_options, NULL);
100 args->display = optarg;
104 printf ("%s\n", VERSION);
119 fprintf (stderr, "Unhandled option `%c'\n", c);
125 if (argc - optind == 1) {
126 args->file = argv[optind];
133 *args_first = optind;