From 2cb3c3df4099297b0a0554bb482e2de04fe86b5c Mon Sep 17 00:00:00 2001 From: sanine Date: Wed, 24 Aug 2022 00:02:17 -0500 Subject: add command-line arguments --- src/options/options.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/options/options.c (limited to 'src/options/options.c') diff --git a/src/options/options.c b/src/options/options.c new file mode 100644 index 0000000..4488837 --- /dev/null +++ b/src/options/options.c @@ -0,0 +1,56 @@ +#include +#include +#include "options.h" + +static struct cag_option opts[] = { + { + .identifier = 's', + .access_letters = "s", + .access_name = "script", + .value_name = "SCRIPT_FILE", + .description = "The filename of the main script. (default: main.lua)" + }, + { + .identifier = 'h', + .access_letters = "h", + .access_name = "help", + .value_name = NULL, + .description = "Shows this help message" + }, +}; + + +void print_help(char *program_name) +{ + printf("usage: %s [OPTIONS]\n", program_name); + cag_option_print(opts, CAG_ARRAY_SIZE(opts), stdout); +} + + +enum outcomes_t parse_options(struct honey_options *options, int argc, char **argv) +{ + /* default values */ + options->script_file = "main.lua"; + + /* parse options */ + char id; + const char *value; + cag_option_context context; + + cag_option_prepare(&context, opts, CAG_ARRAY_SIZE(opts), argc, argv); + while(cag_option_fetch(&context)) { + id = cag_option_get(&context); + switch(id) { + case 's': + options->script_file = cag_option_get_value(&context); + break; + case 'h': + print_help(argv[0]); + return EXIT_SUCCESS; + default: + return EXIT_FAILURE; + } + } + + return CONTINUE_SUCCESS; +} -- cgit v1.2.1