aboutsummaryrefslogtreecommitdiffstats
path: root/src/opt.c
blob: 40743cb49560ca06278b86efa3ff3122e8276623 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "opt.h"

#include "version.h"
#include "err.h"

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

#include <stdlib.h>
#include <string.h>

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", "FILE" },
	{ SIMPLE_OPT_STRING, 'e', "editor", true,
		"text editor for editing calendars", "CMD" },
	{ SIMPLE_OPT_CHAR, '\0', "line-term", true,
		"line terminator for scripting output" },
	{ SIMPLE_OPT_CHAR, '\0', "col-delim", true,
		"column delimiter for scripting output" },
	{ SIMPLE_OPT_END }
};

static struct simple_opt_result result;

void opt_parse(int argc, char **argv)
{
	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] [-e TEXT_EDITOR] [COMMAND]",
			"every is a simple console-based event calendar",
			options);

		printf(
			"\n"
			"Commands\n"
			"  c  (default) print upcoming events in interactive console mode\n"
			"  s  print upcoming events in a format appropriate for scripting\n"
			"  e  open calendar with text editor\n"
		);

		exit(EXIT_SUCCESS);
	}

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

	/* bad command */
	if (result.argc > 0) {
		if (result.argc > 1)
			ERR("too many commands");

		if (strlen(result.argv[0]) > 1)
			ERR("unrecognised command `%s`", result.argv[0]);

		switch (result.argv[0][0]) {
		case 'c':
		case 's':
		case 'e':
			break;
		default:
			ERR("unrecognised command `%s`", result.argv[0]);
		}
	}

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

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

	return NULL;
}

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

	return getenv("EDITOR");
}

char opt_line_term(void)
{
	if (options[4].was_seen)
		return options[4].val.v_char;

	return '\n';
}

char opt_col_delim(void)
{
	if (options[5].was_seen)
		return options[5].val.v_char;

	return '\t';
}

enum opt_command_e opt_command(void)
{
	if (result.argc > 0) {
		switch (result.argv[0][0]) {
		case 'c':
			return OPT_COMMAND_CONSOLE;
		case 's':
			return OPT_COMMAND_SCRIPT;
		case 'e':
			return OPT_COMMAND_EDIT;
		default:
			break;
		}
	}

	return OPT_COMMAND_CONSOLE;
}