1 /* ttt-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 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[] = "<carl@theworths.org>";
34 static char TTT_ARGS_PROGRAM_ARGDOC[] = "";
36 /* XXX: getopt is rather annoying in that you must maintain a
37 * string-encoding of the options in addition to the table. For
38 * example, to enable the sample display option below, one would have
39 * to add "d:" to the optstring.
41 * A useful exercise would be to write a function to generate the
42 * optstring from the table.
44 * The other annoying thing is that the table does not include help
45 * strings, so the args_help function below must also be maintained
50 TTT_ARGS_VAL_LOG_FILE = 256,
55 static char ttt_args_optstring[] = "h:p:";
56 static struct option ttt_args_options[] = {
57 /* name, has_arg, flag, val */
60 {"log-file", 1, 0, TTT_ARGS_VAL_LOG_FILE},
61 {"help", 0, 0, TTT_ARGS_VAL_HELP},
62 {"version", 0, 0, TTT_ARGS_VAL_VERSION},
67 ttt_args_help (const char *argv0)
69 printf ("Usage: %s [OPTION] %s\n", argv0, TTT_ARGS_PROGRAM_ARGDOC);
70 printf ("%s - %s\n", argv0, TTT_ARGS_PROGRAM_DESCRIPTION);
72 printf ("Options that are common to both client and server:\n");
74 printf (" -h HOST, --host=HOST\tHost to connect/bind to\n");
75 printf (" -p PORT, --port=PORT\tPort to connect/bind to\n");
76 printf (" --help\tGive this help list\n");
77 printf (" --version\tPrint program version\n");
79 printf ("Options that are specific to the server:\n");
81 printf (" --log-file=FILE\tFile to use for logging\n");
85 ttt_args_usage (const char *argv0)
87 printf ("Usage: %s [OPTION] %s\n", argv0, TTT_ARGS_PROGRAM_ARGDOC);
88 printf ("Try `%s --help' for more information.\n", argv0);
92 ttt_args_parse(ttt_args_t *args, int argc, char *argv[])
94 char *argv0_copy = strdup (argv[0]);
95 char *argv0 = basename (argv0_copy);
99 args->host = TTT_ARGS_HOST_DEFAULT;
100 args->port = TTT_ARGS_PORT_DEFAULT;
101 args->log_file = TTT_ARGS_LOG_FILE_DEFAULT;
104 c = getopt_long (argc, argv, ttt_args_optstring, ttt_args_options, NULL);
115 case TTT_ARGS_VAL_LOG_FILE:
116 args->log_file = optarg;
118 case TTT_ARGS_VAL_VERSION:
119 printf ("%s\n", VERSION);
123 case TTT_ARGS_VAL_HELP:
124 ttt_args_help (argv0);
129 ttt_args_help (argv0);
134 fprintf (stderr, "Unhandled option `%c'\n", c);