]> git.cworth.org Git - ttt/blobdiff - src/ttt-args.c
* src/args.c: Renamed ttt-args.c
[ttt] / src / ttt-args.c
diff --git a/src/ttt-args.c b/src/ttt-args.c
new file mode 100644 (file)
index 0000000..302e301
--- /dev/null
@@ -0,0 +1,138 @@
+/* ttt-args.c - Parse command-line arguments for ttt using getopt
+ *
+ * Copyright © 2005 Carl Worth
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Author: Carl Worth <cworth@cworth.org>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <libgen.h>
+#include <getopt.h>
+
+#include "ttt-args.h"
+
+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[] = "<cworth@cworth.org>";
+
+/* XXX: SAMPLE: */
+static char TTT_ARGS_PROGRAM_ARGDOC[] = "<file>";
+
+/* XXX: getopt is rather annoying in that you must maintain a
+ * string-encoding of the options in addition to the table. For
+ * example, to enable the sample display option below, one would have
+ * to add "d:" to the optstring.
+ *
+ * A useful exercise would be to write a function to generate the
+ * optstring from the table.
+ *
+ * The other annoying thing is that the table does not include help
+ * strings, so the args_help function below must also be maintained
+ * manually.
+ */
+static char ttt_args_optstring[] = "hV";
+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'},
+    { 0 }
+};
+
+static void
+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");
+    */
+}
+
+static void
+ttt_args_usage (const char *argv0)
+{
+    printf ("Usage: %s [OPTION] %s\n", argv0, TTT_ARGS_PROGRAM_ARGDOC);
+    printf ("Try `%s --help' for more information.\n", argv0);
+}
+
+int
+ttt_args_parse(ttt_args_t *args, int argc, char *argv[], int *args_first)
+{
+    char *argv0_copy = strdup (argv[0]);
+    char *argv0 = basename (argv0_copy);
+
+    int c;
+
+    /* XXX: SAMPLE:
+    args->display = NULL;
+    */
+
+    while (1) {
+       c = getopt_long (argc, argv, ttt_args_optstring, ttt_args_options, NULL);
+       if (c == -1)
+           break;
+
+       switch (c) {
+       /* XXX: SAMPLE:
+       case 'd':
+           args->display = optarg;
+           break;
+       */
+       case 'V':
+           printf ("%s\n", VERSION);
+           exit (0);
+           break;
+
+       case 'h':
+           args_help (argv0);
+           exit (0);
+           break;
+
+       case '?':
+           args_help (argv0);
+           exit (1);
+           break;
+
+       default:
+           fprintf (stderr, "Unhandled option `%c'\n", c);
+           break;
+       }
+    }
+
+    /* XXX: SAMPLE:
+    if (argc - optind == 1) {
+       args->file = argv[optind];
+    } else {
+       args_usage (argv0);
+    }
+    */
+
+    if (args_first)
+       *args_first = optind;
+
+    free (argv0_copy);
+
+    return 0;
+}