summaryrefslogtreecommitdiff
path: root/src/options
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-08-24 00:02:17 -0500
committersanine <sanine.not@pm.me>2022-08-24 00:02:17 -0500
commit2cb3c3df4099297b0a0554bb482e2de04fe86b5c (patch)
tree7796b4064c16460d9d603707b5256027649aa8b6 /src/options
parent709e1b6e1ce86f8da4fc136747fcefbc6c6057bd (diff)
add command-line arguments
Diffstat (limited to 'src/options')
-rw-r--r--src/options/CMakeLists.txt5
-rw-r--r--src/options/options.c56
-rw-r--r--src/options/options.h16
3 files changed, 77 insertions, 0 deletions
diff --git a/src/options/CMakeLists.txt b/src/options/CMakeLists.txt
new file mode 100644
index 0000000..39ed7db
--- /dev/null
+++ b/src/options/CMakeLists.txt
@@ -0,0 +1,5 @@
+project(honey_engine)
+
+target_sources(honey PUBLIC
+ ${CMAKE_CURRENT_LIST_DIR}/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 <stdio.h>
+#include <cargs.h>
+#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;
+}
diff --git a/src/options/options.h b/src/options/options.h
new file mode 100644
index 0000000..cb07b1a
--- /dev/null
+++ b/src/options/options.h
@@ -0,0 +1,16 @@
+#ifndef HONEY_OPTIONS_H
+#define HONEY_OPTIONS_H
+
+struct honey_options {
+ const char *script_file; // main entry point
+};
+
+enum outcomes_t {
+ CONTINUE_SUCCESS,
+ EXIT_SUCCESS,
+ EXIT_FAILURE,
+};
+
+enum outcomes_t parse_options(struct honey_options *options, int argc, char **argv);
+
+#endif