diff options
Diffstat (limited to 'src/options.c')
-rw-r--r-- | src/options.c | 84 |
1 files changed, 60 insertions, 24 deletions
diff --git a/src/options.c b/src/options.c index fa6b999..61a74b6 100644 --- a/src/options.c +++ b/src/options.c @@ -1,18 +1,51 @@ #include <stdio.h> -#include <unistd.h> +#include "cargs/cargs.h" #include "options.h" #include "logging.h" +static struct cag_option options[] = { + { + .identifier = 'q', + .access_letters = "q", + .access_name = NULL, + .value_name = NULL, + .description = "Decrease output verbosity (-qqq suppresses even fatal errors)", + }, + { + .identifier = 'v', + .access_letters = "v", + .access_name = NULL, + .value_name = NULL, + .description = "Increase output verbosity (-vvv displays every log message)", + }, + { + .identifier = 'c', + .access_letters = "c", + .access_name = "config", + .value_name = "CONF_FILE", + .description = "Specify configuration file to read (default: config.lua)", + }, + { + .identifier = 's', + .access_letters = "s", + .access_name = "script", + .value_name = "SCRIPT_FILE", + .description = "Override the built-in build script", + }, + { + .identifier = 'h', + .access_letters = "h", + .access_name = "help", + .value_name = NULL, + .description = "Print this help message and exit", + }, +}; + static void print_usage(const char *progname) { - printf("Usage: %s [-c config_file] [-v[v[v]]] [-q[q[q]]] [-h] [-s script]\n" - " -v Increase output verbosity (-vvv displays every log message)\n" - " -q Decrease output verbosity (-qqq suppresses even fatal errors)\n" - " -c Specify configuration file to read (default 'config.lua')\n" - " -s Override the built-in Lua script\n" - " -h Print this help message and exit\n", - progname); + printf("Usage: %s [OPTIONS]\n", progname); + cag_option_print(options, CAG_ARRAY_SIZE(options), stdout); } int parse_options(struct argent_options *opts, int argc, char **argv) @@ -21,32 +54,35 @@ int parse_options(struct argent_options *opts, int argc, char **argv) opts->conf_filename = "config.lua"; opts->script_filename = NULL; - int opt; - while ((opt = getopt(argc, argv, "hqvc:s:")) != -1) { + char opt; + cag_option_context context; + cag_option_prepare(&context, options, CAG_ARRAY_SIZE(options), argc, argv); + while (cag_option_fetch(&context)) { + opt = cag_option_get(&context); switch (opt) { case 'q': - opts->log_level -= 1; - break; - + opts->log_level -= 1; + break; + case 'v': - opts->log_level += 1; - break; - + opts->log_level += 1; + break; + case 'c': - opts->conf_filename = optarg; - break; + opts->conf_filename = cag_option_get_value(&context); + break; case 's': - opts->script_filename = optarg; - break; + opts->script_filename = cag_option_get_value(&context); + break; case 'h': - print_usage(argv[0]); - return 2; + print_usage(argv[0]); + return 2; default: - print_usage(argv[0]); - return 1; + print_usage(argv[0]); + return 1; } } |