summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/plib/poptions.h
blob: ba39ba240507b53cbf1a57bd9daca6aa2a69e1b0 (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
273
274
// license:GPL-2.0+
// copyright-holders:Couriersud

#ifndef POPTIONS_H_
#define POPTIONS_H_

///
/// \file poptions.h
///

#include "pfmtlog.h"
#include "pstonum.h"
#include "pstring.h"
#include "putil.h"

namespace plib {

	/// \brief options base class
	///
	class options;

	class option_base
	{
	public:
		option_base(options &parent, const pstring &help);
		virtual ~option_base() = default;

		PCOPYASSIGNMOVE(option_base, delete)

		virtual pstring help() const { return m_help; }
	private:
		pstring m_help;
	};

	class option_group : public option_base
	{
	public:
		option_group(options &parent, const pstring &group, const pstring &help)
		: option_base(parent, help), m_group(group) { }

		pstring group() const { return m_group; }
	private:
		pstring m_group;
	};

	class option_example : public option_base
	{
	public:
		option_example(options &parent, const pstring &group, const pstring &help)
		: option_base(parent, help), m_example(group) { }

		pstring example() const { return m_example; }
	private:
		pstring m_example;
	};


	class option : public option_base
	{
	public:
		option(options &parent, const pstring &ashort, const pstring &along, const pstring &help, bool has_argument);

		// no_argument options will be called with "" argument

		pstring short_opt() const { return m_short; }
		pstring long_opt() const { return m_long; }
		bool has_argument() const { return m_has_argument ; }
		bool was_specified() const { return m_specified; }

		int do_parse(const pstring &argument)
		{
			m_specified = true;
			return parse(argument);
		}

	protected:
		virtual int parse(const pstring &argument) = 0;

	private:
		pstring m_short;
		pstring m_long;
		bool m_has_argument;
		bool m_specified;
	};

	class option_str : public option
	{
	public:
		option_str(options &parent, const pstring &ashort, const pstring &along, const pstring &defval, const pstring &help)
		: option(parent, ashort, along, help, true), m_val(defval)
		{}

		pstring operator ()() const { return m_val; }

	protected:
		int parse(const pstring &argument) override;

	private:
		pstring m_val;
	};

	class option_str_limit_base : public option
	{
	public:
		option_str_limit_base(options &parent, const pstring &ashort, const pstring &along, std::vector<pstring> &&limit, const pstring &help)
		: option(parent, ashort, along, help, true)
		, m_limit(limit)
		{
		}
		const std::vector<pstring> &limit() const { return m_limit; }

	protected:

	private:
		std::vector<pstring> m_limit;
	};


	template <typename T>
	class option_str_limit : public option_str_limit_base
	{
	public:
		option_str_limit(options &parent, const pstring &ashort, const pstring &along, const T &defval, std::vector<pstring> &&limit, const pstring &help)
		: option_str_limit_base(parent, ashort, along, std::move(limit), help), m_val(defval)
		{
		}

		T operator ()() const { return m_val; }

		pstring as_string() const { return limit()[m_val]; }

	protected:
		int parse(const pstring &argument) override
		{
			auto raw = plib::container::indexof(limit(), argument);

			if (raw != plib::container::npos)
			{
				m_val = static_cast<T>(raw);
				return 0;
			}

			return 1;
		}

	private:
		T m_val;
	};

	class option_bool : public option
	{
	public:
		option_bool(options &parent, const pstring &ashort, const pstring &along, const pstring &help)
		: option(parent, ashort, along, help, false), m_val(false)
		{}

		bool operator ()() const { return m_val; }

	protected:
		int parse(const pstring &argument) override;

	private:
		bool m_val;
	};

	template <typename T>
	class option_num : public option
	{
	public:
		option_num(options &parent, const pstring &ashort, const pstring &along, T defval,
				const pstring &help,
				T minval = std::numeric_limits<T>::lowest(),
				T maxval = std::numeric_limits<T>::max() )
		: option(parent, ashort, along, help, true)
		, m_val(defval)
		, m_min(minval)
		, m_max(maxval)
		, m_def(defval)
		{}

		T operator ()() const { return m_val; }

		pstring help() const override
		{
			auto hs(option::help());
			return plib::pfmt(hs)(m_def, m_min, m_max);
		}

	protected:
		int parse(const pstring &argument) override
		{
			bool err(false);
			m_val = pstonum_ne<T>(argument, err);
			return (err ? 1 : (m_val < m_min || m_val > m_max));
		}

	private:
		T m_val;
		T m_min;
		T m_max;
		T m_def;
	};

	class option_vec : public option
	{
	public:
		option_vec(options &parent, const pstring &ashort, const pstring &along, const pstring &help)
		: option(parent, ashort, along, help, true)
		{}

		const std::vector<pstring> &operator ()() const { return m_val; }

	protected:
		int parse(const pstring &argument) override;

	private:
		std::vector<pstring> m_val;
	};

	class option_args : public option_vec
	{
	public:
		option_args(options &parent, const pstring &help)
		: option_vec(parent, "", "", help)
		{}
	};

	class options
	{
	public:

		options();
		explicit options(option **o);

		~options() = default;

		PCOPYASSIGNMOVE(options, delete)

		void register_option(option_base *opt);
		std::size_t parse(const std::vector<putf8string> &argv);

		pstring help(const pstring &description, const pstring &usage,
				unsigned width = 72, unsigned indent = 20) const;

		pstring app() const { return m_app; }

	private:
		static pstring split_paragraphs(const pstring &text, unsigned width, unsigned indent,
				unsigned firstline_indent, const pstring &line_end = "\n");

		void check_consistency() noexcept(false);

		template <typename T>
		T *getopt_type() const
		{
			for (const auto & optbase : m_opts )
			{
				if (auto opt = dynamic_cast<T *>(optbase))
					return opt;
			}
		return nullptr;
	}

	option *getopt_short(const pstring &arg) const;
	option *getopt_long(const pstring &arg) const;

	std::vector<option_base *> m_opts;
	pstring m_app;
	option_args * m_other_args;
};

} // namespace plib

#endif // POPTIONS_H_