aboutsummaryrefslogtreecommitdiffstats
path: root/src/opt.c
blob: eb796e79ab780bb704f9d257a162079755b778b2 (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
#include "version.h"
#include "opt.h"

#include "../reqs/simple-opt/simple-opt.h"

#include <stdlib.h>

const char *set[] = {
	[EVERY_OPT_OUTPUTMODE_CONSOLE] = "console",
	[EVERY_OPT_OUTPUTMODE_SCRIPT]  = "script",
	NULL,
};

static struct simple_opt options[] = {
	{ SIMPLE_OPT_FLAG, 'h', "help", false,
		"print this help message and exit" },
	{ SIMPLE_OPT_FLAG, 'v', "version", false,
		"print the version of every in use and exit" },
	{ SIMPLE_OPT_STRING, 'c', "calendar", true,
		"path to calendar", "<file>" },
	{ SIMPLE_OPT_STRING_SET, 'm', "output-mode", true,
		"output mode (default is console)",
		"(console|script)", set },
	{ SIMPLE_OPT_END }
};

void opt_parse(int argc, char **argv)
{
	struct simple_opt_result result;

	result = simple_opt_parse(argc, argv, options);

	/* parse err */
	if (result.result_type != SIMPLE_OPT_RESULT_SUCCESS) {
		simple_opt_print_error(stderr, 80, argv[0], result);
		exit(EXIT_FAILURE);
	}

	/* help */
	if (options[0].was_seen) {
		simple_opt_print_usage(stdout, 80, argv[0],
			"[-c CALENDAR_FILE] [-m OUTPUT_MODE]",
			"every is a flexible, console-based event calendar",
			options);
		exit(EXIT_SUCCESS);
	}

	/* version */
	if (options[1].was_seen) {
		puts(VERSION);
		exit(EXIT_SUCCESS);
	}

	/* bad calendar path*/
	if (options[2].was_seen && options[2].val.v_string[0] == '\0') {
		printf("err: could not read calendar at ``\n");
		exit(EXIT_FAILURE);
	}
}

char* opt_calpath(void)
{
	if (options[2].was_seen)
		return options[2].val.v_string;

	return NULL;
}

enum every_opt_outputmode opt_outputmode(void)
{
	if (options[3].was_seen)
		return options[3].val.v_string_set_idx;

	return EVERY_OPT_OUTPUTMODE_CONSOLE;
}