summaryrefslogtreecommitdiff
path: root/src/options/options.c
blob: d2221aa893caf86857fc4891877052f248761960 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <cargs.h>
#include <common.h>
#include "logging/logging.h"
#include "options.h"

static struct cag_option opts[] = {
	{
		.identifier = 'd',
		.access_letters = "d",
		.access_name = "directory",
		.value_name = "WORKING_DIRECTORY",
		.description = "Set the working directory for honey. (default: .)",
	},
	{
		.identifier = 's',
		.access_letters = "s",
		.access_name = "script",
		.value_name = "SCRIPT_FILE",
		.description = "The filename of the main script. (default: main.lua)"
	},
	{
		.identifier = 'e',
		.access_letters = NULL,
		.access_name = "version",
		.value_name = NULL,
		.description = "Show the honey version"
	},
	{
		.identifier = 'h',
		.access_letters = "h",
		.access_name = "help",
		.value_name = NULL,
		.description = "Shows this help message"
	},
	{
		.identifier = 'v',
		.access_letters = "v",
		.access_name = NULL,
		.value_name = NULL,
		.description = "Increase verbosity (-vvv shows all messages)"
	},
	{
		.identifier = 'q',
		.access_letters = "q",
		.access_name = NULL,
		.value_name = NULL,
		.description = "Decrease verbosity (-qqq hides all messages)"
	},
};


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->working_dir = NULL;
	options->script_file = "main.lua";
	options->log_level = HONEY_WARN;

	/* 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) {
		/* set working directory */
		case 'd':
			options->working_dir = cag_option_get_value(&context);
			break;

		/* set main script */
		case 's':
			options->script_file = cag_option_get_value(&context);
			break;

		/* print help information & exit */
		case 'h':
			print_help(argv[0]);
			return EXIT_SUCCESS;

		/* print version information & exit */
		case 'e':
			printf(
				"honey v%d.%d.%d -- %s\n",
				HONEY_VERSION_MAJOR,
				HONEY_VERSION_MINOR,
				HONEY_VERSION_PATCH,
				HONEY_VERSION_STR
			);
			return EXIT_SUCCESS;

		/* verbosity options */
		case 'q':
			options->log_level -= 1;
			break;
		case 'v':
			options->log_level += 1;
			break;

		default:
			int index = cag_option_get_index(&context);
			fprintf(stderr, "unknown option: %s\n", argv[index-1]);
			print_help(argv[0]);
			return EXIT_FAILURE;
		}
	}

	return CONTINUE_SUCCESS;
}