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
|
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
* poptions.h
*
*/
#pragma once
#ifndef POPTIONS_H_
#define POPTIONS_H_
#include <cstddef>
#include "pstring.h"
#include "plists.h"
#include "putil.h"
namespace plib {
/***************************************************************************
Options
***************************************************************************/
class options;
class option
{
public:
option();
option(options &parent, pstring ashort, pstring along, pstring help, bool has_argument);
virtual ~option();
/* no_argument options will be called with "" argument */
virtual int parse(ATTR_UNUSED pstring argument) = 0;
pstring short_opt() { return m_short; }
pstring long_opt() { return m_long; }
pstring help() { return m_help; }
bool has_argument() { return m_has_argument ; }
private:
pstring m_short;
pstring m_long;
pstring m_help;
bool m_has_argument;
};
class option_str : public option
{
public:
option_str(options &parent, pstring ashort, pstring along, pstring defval, pstring help)
: option(parent, ashort, along, help, true), m_val(defval)
{}
virtual int parse(pstring argument) override;
pstring operator ()() { return m_val; }
private:
pstring m_val;
};
class option_str_limit : public option
{
public:
option_str_limit(options &parent, pstring ashort, pstring along, pstring defval, pstring limit, pstring help)
: option(parent, ashort, along, help, true), m_val(defval), m_limit(limit, ":")
{}
virtual int parse(pstring argument) override;
pstring operator ()() { return m_val; }
const plib::pstring_vector_t &limit() { return m_limit; }
private:
pstring m_val;
plib::pstring_vector_t m_limit;
};
class option_bool : public option
{
public:
option_bool(options &parent, pstring ashort, pstring along, pstring help)
: option(parent, ashort, along, help, false), m_val(false)
{}
virtual int parse(ATTR_UNUSED pstring argument) override;
bool operator ()() { return m_val; }
private:
bool m_val;
};
class option_double : public option
{
public:
option_double(options &parent, pstring ashort, pstring along, double defval, pstring help)
: option(parent, ashort, along, help, true), m_val(defval)
{}
virtual int parse(pstring argument) override;
double operator ()() { return m_val; }
private:
double m_val;
};
class options
{
public:
options();
explicit options(option *o[]);
~options();
void register_option(option *opt);
int parse(int argc, char *argv[]);
pstring help(pstring description, pstring usage,
unsigned width = 72, unsigned ident = 20);
pstring app() { return m_app; }
private:
static pstring split_paragraphs(pstring text, unsigned width, unsigned ident,
unsigned firstline_ident);
option *getopt_short(pstring arg);
option *getopt_long(pstring arg);
std::vector<option *> m_opts;
pstring m_app;
};
}
#endif /* POPTIONS_H_ */
|