From cd1a55a0d1f77aedb078ab9fef96dac94b41c86b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Wed, 11 Feb 2015 02:07:52 +0100 Subject: Import option handler from ponymap --- zyklonb.c | 81 ++++++++++++++++++++++++++------------------------------------- 1 file changed, 33 insertions(+), 48 deletions(-) (limited to 'zyklonb.c') diff --git a/zyklonb.c b/zyklonb.c index 257de7d..f9624b9 100644 --- a/zyklonb.c +++ b/zyklonb.c @@ -1,7 +1,7 @@ /* * zyklonb.c: the experimental IRC bot * - * Copyright (c) 2014, Přemysl Janouch + * Copyright (c) 2014 - 2015, Přemysl Janouch * All rights reserved. * * Permission to use, copy, modify, and/or distribute this software for any @@ -2150,65 +2150,50 @@ on_signal_pipe_readable (const struct pollfd *fd, struct bot_context *ctx) } } -static void -print_usage (const char *program_name) -{ - fprintf (stderr, - "Usage: %s [OPTION]...\n" - "Experimental IRC bot.\n" - "\n" - " -d, --debug run in debug mode\n" - " -h, --help display this help and exit\n" - " -V, --version output version information and exit\n" - " --write-default-cfg [filename]\n" - " write a default configuration file and exit\n", - program_name); -} - int main (int argc, char *argv[]) { - const char *invocation_name = argv[0]; str_vector_init (&g_original_argv); str_vector_add_vector (&g_original_argv, argv); - static struct option opts[] = + static const struct opt opts[] = { - { "debug", no_argument, NULL, 'd' }, - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'V' }, - { "write-default-cfg", optional_argument, NULL, 'w' }, - { NULL, 0, NULL, 0 } + { 'd', "debug", NULL, 0, "run in debug mode" }, + { 'h', "help", NULL, 0, "display this help and exit" }, + { 'V', "version", NULL, 0, "output version information and exit" }, + { 'w', "write-default-cfg", "FILENAME", + OPT_OPTIONAL_ARG | OPT_LONG_ONLY, + "write a default configuration file and exit" }, + { 0, NULL, NULL, 0, NULL } }; - while (1) - { - int c, opt_index; + struct opt_handler oh; + opt_handler_init (&oh, argc, argv, opts, NULL, "Experimental IRC bot."); - c = getopt_long (argc, argv, "dhV", opts, &opt_index); - if (c == -1) - break; - - switch (c) - { - case 'd': - g_debug_mode = true; - break; - case 'h': - print_usage (invocation_name); - exit (EXIT_SUCCESS); - case 'V': - printf (PROGRAM_NAME " " PROGRAM_VERSION "\n"); - exit (EXIT_SUCCESS); - case 'w': - call_write_default_config (optarg, g_config_table); - exit (EXIT_SUCCESS); - default: - print_error ("wrong options"); - exit (EXIT_FAILURE); - } + int c; + while ((c = opt_handler_get (&oh)) != -1) + switch (c) + { + case 'd': + g_debug_mode = true; + break; + case 'h': + opt_handler_usage (&oh, stdout); + exit (EXIT_SUCCESS); + case 'V': + printf (PROGRAM_NAME " " PROGRAM_VERSION "\n"); + exit (EXIT_SUCCESS); + case 'w': + call_write_default_config (optarg, g_config_table); + exit (EXIT_SUCCESS); + default: + print_error ("wrong options"); + opt_handler_usage (&oh, stderr); + exit (EXIT_FAILURE); } + opt_handler_free (&oh); + print_status (PROGRAM_NAME " " PROGRAM_VERSION " starting"); setup_signal_handlers (); -- cgit v1.2.3