summaryrefslogtreecommitdiff
path: root/src/cliargsparser.cpp
blob: c5ad77410832c609d63b874e631461c8c2727870 (plain) (blame)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "cliargsparser.h"

#include <getopt.h>
#include <cstdlib>

#include "globals.h"
#include "ruststring.h"
#include "strprintf.h"

extern "C" {
	void* create_rs_cliargsparser(int argc, char* argv[]);

	void destroy_rs_cliargsparser(void*);

	bool rs_cliargsparser_do_import(void* rs_cliargsparser);

	bool rs_cliargsparser_do_export(void* rs_cliargsparser);

	bool rs_cliargsparser_do_vacuum(void* rs_cliargsparser);

	bool rs_cliargsparser_do_cleanup(void* rs_cliargsparser);

	char* rs_cliargsparser_importfile(void* rs_cliargsparser);

	char* rs_cliargsparser_program_name(void* rs_cliargsparser);

	bool rs_cliargsparser_do_read_import(void* rs_cliargsparser);

	char* rs_cliargsparser_readinfo_import_file(void* rs_cliargsparser);

	bool rs_cliargsparser_do_read_export(void* rs_cliargsparser);

	char* rs_cliargsparser_readinfo_export_file(void* rs_cliargsparser);

	unsigned int rs_cliargsparser_show_version(void* rs_cliargsparser);

	bool rs_cliargsparser_silent(void* rs_cliargsparser);

	bool rs_cliargsparser_using_nonstandard_configs(void* rs_cliargsparser);

	bool rs_cliargsparser_should_return(void* rs_cliargsparser);

	int rs_cliargsparser_return_code(void* rs_cliargsparser);

	char* rs_cliargsparser_display_msg(void* rs_cliargsparser);

	bool rs_cliargsparser_should_print_usage(void* rs_cliargsparser);

	bool rs_cliargsparser_refresh_on_start(void* rs_cliargsparser);

	bool rs_cliargsparser_set_url_file(void* rs_cliargsparser);

	char* rs_cliargsparser_url_file(void* rs_cliargsparser);

	bool rs_cliargsparser_set_lock_file(void* rs_cliargsparser);

	char* rs_cliargsparser_lock_file(void* rs_cliargsparser);

	bool rs_cliargsparser_set_cache_file(void* rs_cliargsparser);

	char* rs_cliargsparser_cache_file(void* rs_cliargsparser);

	bool rs_cliargsparser_set_config_file(void* rs_cliargsparser);

	char* rs_cliargsparser_config_file(void* rs_cliargsparser);

	bool rs_cliargsparser_execute_cmds(void* rs_cliargsparser);

	unsigned int rs_cliargsparser_cmds_to_execute_count(void* rs_cliargsparser);
	char* rs_cliargsparser_cmd_to_execute_n(void* rs_cliargsparser, unsigned int n);

	bool rs_cliargsparser_set_log_file(void* rs_cliargsparser);

	char* rs_cliargsparser_log_file(void* rs_cliargsparser);

	bool rs_cliargsparser_set_log_level(void* rs_cliargsparser);

	char rs_cliargsparser_log_level(void* rs_cliargsparser);
}

#define GET_VALUE(NAME, DEFAULT) \
	if (rs_cliargsparser) { \
		return rs_cliargsparser_ ## NAME (rs_cliargsparser); \
	} else { \
		return DEFAULT; \
	}

#define GET_STRING(NAME) \
	if (rs_cliargsparser) { \
		return RustString(rs_cliargsparser_ ## NAME (rs_cliargsparser)); \
	} else { \
		return {}; \
	}

#define GET_OPTIONAL_VALUE(CHECKER, GETTER, DEFAULT) \
	if (rs_cliargsparser) { \
		if (rs_cliargsparser_ ## CHECKER (rs_cliargsparser)) { \
			return rs_cliargsparser_ ## GETTER (rs_cliargsparser); \
		} else { \
			return nonstd::nullopt; \
		} \
	} else { \
		return DEFAULT; \
	}

#define GET_OPTIONAL_STRING(CHECKER, GETTER) \
	if (rs_cliargsparser) { \
		if (rs_cliargsparser_ ## CHECKER (rs_cliargsparser)) { \
			return RustString(rs_cliargsparser_ ## GETTER (rs_cliargsparser)); \
		} else { \
			return nonstd::nullopt; \
		} \
	} else { \
		return {}; \
	}

namespace newsboat {

CliArgsParser::CliArgsParser(int argc, char* argv[])
{
	rs_cliargsparser = create_rs_cliargsparser(argc, argv);
}

CliArgsParser::~CliArgsParser()
{
	if (rs_cliargsparser) {
		destroy_rs_cliargsparser(rs_cliargsparser);
	}
}

bool CliArgsParser::do_import() const
{
	GET_VALUE(do_import, false);
}

bool CliArgsParser::do_export() const
{
	GET_VALUE(do_export, false);
}

bool CliArgsParser::do_vacuum() const
{
	GET_VALUE(do_vacuum, false);
}

bool CliArgsParser::do_cleanup() const
{
	GET_VALUE(do_cleanup, false);
}

std::string CliArgsParser::importfile() const
{
	GET_STRING(importfile);
}

nonstd::optional<std::string> CliArgsParser::readinfo_import_file() const
{
	GET_OPTIONAL_STRING(do_read_import, readinfo_import_file);
}

nonstd::optional<std::string> CliArgsParser::readinfo_export_file() const
{
	GET_OPTIONAL_STRING(do_read_export, readinfo_export_file);
}

std::string CliArgsParser::program_name() const
{
	GET_STRING(program_name);
}

unsigned int CliArgsParser::show_version() const
{
	GET_VALUE(show_version, 0);
}

bool CliArgsParser::silent() const
{
	GET_VALUE(silent, false);
}

bool CliArgsParser::using_nonstandard_configs() const
{
	GET_VALUE(using_nonstandard_configs, false);
}

nonstd::optional<int> CliArgsParser::return_code() const
{
	GET_OPTIONAL_VALUE(should_return, return_code, 0);
}

std::string CliArgsParser::display_msg() const
{
	GET_STRING(display_msg);
}

bool CliArgsParser::should_print_usage() const
{
	GET_VALUE(should_print_usage, false);
}

bool CliArgsParser::refresh_on_start() const
{
	GET_VALUE(refresh_on_start, false);
}

nonstd::optional<std::string> CliArgsParser::url_file() const
{
	GET_OPTIONAL_STRING(set_url_file, url_file);
}

nonstd::optional<std::string> CliArgsParser::lock_file() const
{
	GET_OPTIONAL_STRING(set_lock_file, lock_file);
}

nonstd::optional<std::string> CliArgsParser::cache_file() const
{
	GET_OPTIONAL_STRING(set_cache_file, cache_file);
}

nonstd::optional<std::string> CliArgsParser::config_file() const
{
	GET_OPTIONAL_STRING(set_config_file, config_file);
}

nonstd::optional<std::vector<std::string>> CliArgsParser::cmds_to_execute()
	const
{
	if (rs_cliargsparser) {
		if (rs_cliargsparser_execute_cmds(rs_cliargsparser)) {
			std::vector<std::string> result;

			const auto count = rs_cliargsparser_cmds_to_execute_count(rs_cliargsparser);
			for (unsigned int i = 0; i < count; ++i) {
				result.push_back(RustString(rs_cliargsparser_cmd_to_execute_n(rs_cliargsparser,
							i)));
			}

			return result;
		} else {
			return nonstd::nullopt;
		}
	} else {
		return {};
	}
}

nonstd::optional<std::string> CliArgsParser::log_file() const
{
	GET_OPTIONAL_STRING(set_log_file, log_file);
}

nonstd::optional<Level> CliArgsParser::log_level() const
{
	if (rs_cliargsparser) {
		if (rs_cliargsparser_set_log_level(rs_cliargsparser)) {
			return static_cast<Level>(rs_cliargsparser_log_level(rs_cliargsparser));
		} else {
			return nonstd::nullopt;
		}
	} else {
		return nonstd::nullopt;
	}
}

void* CliArgsParser::get_rust_pointer() const
{
	return rs_cliargsparser;
}

} // namespace newsboat