X-Git-Url: https://git.cworth.org/git?p=ttt;a=blobdiff_plain;f=src%2Fttt-args.c;h=18a7d255405cd49c0fda6cbf6be7120dccdf5cf2;hp=2e4720594e6dee1c575f85c497d4afd0ee748735;hb=49f7f9e3dc198c3593021bff5514f19090a4834c;hpb=2cd70db8433cc9d02a4ca784190260889c835198 diff --git a/src/ttt-args.c b/src/ttt-args.c index 2e47205..18a7d25 100644 --- a/src/ttt-args.c +++ b/src/ttt-args.c @@ -29,10 +29,9 @@ static const char TTT_ARGS_PROGRAM_VERSION[] = VERSION; static const char TTT_ARGS_PROGRAM_DESCRIPTION[] = "client-server tic-tac-toe game"; -static const char TTT_ARGS_PROGRAM_BUG_ADDRESS[] = ""; +static const char TTT_ARGS_PROGRAM_BUG_ADDRESS[] = ""; -/* XXX: SAMPLE: */ -static char TTT_ARGS_PROGRAM_ARGDOC[] = ""; +static char TTT_ARGS_PROGRAM_ARGDOC[] = ""; /* XXX: getopt is rather annoying in that you must maintain a * string-encoding of the options in addition to the table. For @@ -46,14 +45,21 @@ static char TTT_ARGS_PROGRAM_ARGDOC[] = ""; * strings, so the args_help function below must also be maintained * manually. */ -static char ttt_args_optstring[] = "hV"; + +enum { + TTT_ARGS_VAL_LOG_FILE = 256, + TTT_ARGS_VAL_HELP, + TTT_ARGS_VAL_VERSION +}; + +static char ttt_args_optstring[] = "h:p:"; static struct option ttt_args_options[] = { /* name, has_arg, flag, val */ - /* XXX: SAMPLE: - {"display", 1, 0, 'd'}, - */ - {"help", 0, 0, 'h'}, - {"version", 0, 0, 'V'}, + {"host", 1, 0, 'h'}, + {"port", 1, 0, 'p'}, + {"log-file", 1, 0, TTT_ARGS_VAL_LOG_FILE}, + {"help", 0, 0, TTT_ARGS_VAL_HELP}, + {"version", 0, 0, TTT_ARGS_VAL_VERSION}, { 0 } }; @@ -63,11 +69,16 @@ ttt_args_help (const char *argv0) printf ("Usage: %s [OPTION] %s\n", argv0, TTT_ARGS_PROGRAM_ARGDOC); printf ("%s - %s\n", argv0, TTT_ARGS_PROGRAM_DESCRIPTION); puts (""); - printf (" -h, --help\t\tGive this help list\n"); - printf (" -V, --version\t\tPrint program version\n"); - /* XXX: SAMPLE: - printf (" -d, --display=DISPLAY\tX server to connect to"); - */ + printf ("Options that are common to both client and server:\n"); + puts (""); + printf (" -h HOST, --host=HOST\tHost to connect/bind to\n"); + printf (" -p PORT, --port=PORT\tPort to connect/bind to\n"); + printf (" --help\tGive this help list\n"); + printf (" --version\tPrint program version\n"); + puts (""); + printf ("Options that are specific to the server:\n"); + puts (""); + printf (" --log-file=FILE\tFile to use for logging\n"); } static void @@ -78,16 +89,16 @@ ttt_args_usage (const char *argv0) } int -ttt_args_parse(ttt_args_t *args, int argc, char *argv[], int *args_first) +ttt_args_parse(ttt_args_t *args, int argc, char *argv[]) { char *argv0_copy = strdup (argv[0]); char *argv0 = basename (argv0_copy); int c; - /* XXX: SAMPLE: - args->display = NULL; - */ + args->host = TTT_ARGS_HOST_DEFAULT; + args->port = TTT_ARGS_PORT_DEFAULT; + args->log_file = TTT_ARGS_LOG_FILE_DEFAULT; while (1) { c = getopt_long (argc, argv, ttt_args_optstring, ttt_args_options, NULL); @@ -95,17 +106,21 @@ ttt_args_parse(ttt_args_t *args, int argc, char *argv[], int *args_first) break; switch (c) { - /* XXX: SAMPLE: - case 'd': - args->display = optarg; + case 'h': + args->host = optarg; break; - */ - case 'V': + case 'p': + args->port = optarg; + break; + case TTT_ARGS_VAL_LOG_FILE: + args->log_file = optarg; + break; + case TTT_ARGS_VAL_VERSION: printf ("%s\n", VERSION); exit (0); break; - case 'h': + case TTT_ARGS_VAL_HELP: ttt_args_help (argv0); exit (0); break; @@ -121,17 +136,6 @@ ttt_args_parse(ttt_args_t *args, int argc, char *argv[], int *args_first) } } - /* XXX: SAMPLE: - if (argc - optind == 1) { - args->file = argv[optind]; - } else { - ttt_args_usage (argv0); - } - */ - - if (args_first) - *args_first = optind; - free (argv0_copy); return 0;