From 255dc9aa19508f3b982e16b3991d17c372f23fde Mon Sep 17 00:00:00 2001 From: John Zaitseff Date: Wed, 29 Jun 2011 13:15:26 +1000 Subject: [PATCH] Add the process_cmdline(), show_version() and show_usage() functions --- src/trader.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 153 insertions(+), 3 deletions(-) diff --git a/src/trader.c b/src/trader.c index 10f0fb0..0e07d57 100644 --- a/src/trader.c +++ b/src/trader.c @@ -34,6 +34,16 @@ #include "trader.h" +/************************************************************************ +* Internal function declarations * +************************************************************************/ + +int main (int argc, char *argv[]); +static void process_cmdline (int argc, char *argv[]); +static void show_version (void); +static void show_usage (int status); + + /************************************************************************ * Main program * ************************************************************************/ @@ -42,10 +52,150 @@ int main (int argc, char *argv[]) { init_program_name(argv); + // Process command line arguments + process_cmdline(argc, argv); + + // Testing... - printf("program_name: %s\n", program_name()); - printf("home_directory: %s\n", home_directory()); - printf("data_directory: %s\n", data_directory()); + printf(_("Program name: %s\n"), program_name()); + printf(_("Home directory: %s\n"), home_directory()); + printf(_("Data directory: %s\n"), data_directory()); return EXIT_SUCCESS; } + + +/************************************************************************ +* Command line processing * +************************************************************************/ + +/* Constants for command line options */ + +static const char options_short[] = "hV?"; + /* -h, -? --help + -V --version + */ + +static struct option const options_long[] = { + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } +}; + + +/*----------------------------------------------------------------------- + Function: process_cmdline - Process command line arguments + Arguments: argc - Same as passed to main() + argv - Same as passed to main() + Returns: (nothing) + + This function processes the command line arguments passed through argc + and argv, setting global variables as appropriate. +*/ + +static void process_cmdline (int argc, char *argv[]) +{ + int c; + + // Process arguments starting with "-" or "--" + opterr = TRUE; + while (TRUE) { + c = getopt_long(argc, argv, options_short, options_long, NULL); + if (c == EOF) + break; + + switch (c) { + case 'h': + case '?': + /* -h, -?, --help: show help */ + show_usage(EXIT_SUCCESS); + + case 'V': + /* -V, --version: show version information */ + show_version(); + + default: + show_usage(EXIT_FAILURE); + } + } + + // Process remaining arguments + + if ((optind < argc) && (argv[optind] != NULL)) { + } +} + + +/*----------------------------------------------------------------------- + Function: show_version - Show program version information + Arguments: (none) + Returns: (nothing) + + This function displays version information about this program, then + terminates. +*/ + +static void show_version (void) +{ + printf(_("\ +" PACKAGE_NAME " (%s) %s\n\ +Copyright (C) %s, John Zaitseff.\n\ +\n\ +Star Traders is a simple game of interstellar trading, where the object\n\ +of the game is to create companies, buy and sell shares, borrow and repay\n\ +money, in order to become the wealthiest player (the winner).\n\ +\n\ +This program is free software that is distributed under the terms of the\n\ +GNU General Public License, version 3 or later. You are welcome to\n\ +modify and/or distribute it under certain conditions. This program has\n\ +NO WARRANTY, to the extent permitted by law; see the License for details.\n\ +"), program_name(), PACKAGE_VERSION, "1990-2011"); + + exit(EXIT_SUCCESS); +} + + +/*----------------------------------------------------------------------- + Function: show_usage - Show command line usage information + Arguments: status - Exit status + Returns: (nothing) + + This function displays usage information to standard output or standard + error, then terminates. +*/ + +static void show_usage (int status) +{ + if (status != EXIT_SUCCESS) { + fprintf(stderr, _("Try `%s --help' for more information.\n"), + program_name()); + } else { + printf(_("Usage: %s [OPTION ...] [GAME]\n"), program_name()); + printf(_("\ +Play Star Traders, a simple game of interstellar trading.\n\n\ +")); + printf(_("\ +Options:\n\ + -V, --version output version information and exit\n\ + -h, --help display this help and exit\n\n\ +")); + printf(_("\ +If GAME is specified as a number between 1 and 9, load and continue\n\ +playing that game. If GAME is not specified, start a new game.\n\n\ +")); + +#ifdef PACKAGE_AUTHOR + printf(_("Report bugs to %s <%s>.\n"), PACKAGE_AUTHOR, PACKAGE_BUGREPORT); +#else + printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT); +#endif +#ifdef PACKAGE_PACKAGER_BUG_REPORTS + printf(_("Report %s bugs to <%s>.\n"), PACKAGE_PACKAGER, PACKAGE_PACKAGER_BUG_REPORTS); +#endif +#ifdef PACKAGE_URL + printf(_("Star Traders home page: <%s>.\n"), PACKAGE_URL); +#endif + } + + exit(status); +}