20
20
namespace pgm
21
21
{
22
22
23
- // pull in often-used names from std::
24
- using std::move;
25
- using std::size_t ;
26
- using std::string;
27
- using std::string_view;
28
-
29
23
// //////////////////////////////////////////////////////////////////////////////
30
24
// parsed values for an option or positional parameter
31
25
struct argval
@@ -37,15 +31,15 @@ struct argval
37
31
38
32
auto const & values () const { return data_; }
39
33
auto const & value () const { return data_.at (0 ); }
40
- auto const & value (size_t n) const { return data_.at (n); }
34
+ auto const & value (std:: size_t n) const { return data_.at (n); }
41
35
42
- auto value_or (string_view def) const { return empty () ? string{def} : value (); }
36
+ auto value_or (std:: string_view def) const { return empty () ? std:: string{def} : value (); }
43
37
44
38
private:
45
- std::vector<string> data_;
39
+ std::vector<std:: string> data_;
46
40
47
41
friend class args ;
48
- void add (string val) { data_.push_back (move (val)); }
42
+ void add (std:: string val) { data_.push_back (std:: move (val)); }
49
43
};
50
44
51
45
// //////////////////////////////////////////////////////////////////////////////
@@ -64,48 +58,48 @@ constexpr auto operator|(spec lhs, spec rhs);
64
58
// program option
65
59
struct option
66
60
{
67
- string short_; // short option name
68
- string long_; // long option name
69
- string valname_; // name of the option value; eg, --opt-name=<value>
70
- string description_; // description
61
+ std:: string short_; // short option name
62
+ std:: string long_; // long option name
63
+ std:: string valname_; // name of the option value; eg, --opt-name=<value>
64
+ std:: string description_; // description
71
65
72
- bool req_ = false ; // mandatory (required) option
73
- bool mul_ = false ; // can be specified multiple times
74
- bool optval_ = false ; // option value is optional
66
+ bool req_ = false ; // mandatory (required) option
67
+ bool mul_ = false ; // can be specified multiple times
68
+ bool optval_ = false ; // option value is optional
75
69
76
- argval values_; // parsed value(s)
70
+ argval values_; // parsed value(s)
77
71
};
78
72
79
73
// //////////////////////////////////////////////////////////////////////////////
80
74
// positional parameter
81
75
struct param
82
76
{
83
- string name_; // param name
84
- string description_; // description
77
+ std:: string name_; // param name
78
+ std:: string description_; // description
85
79
86
- bool opt_ = false ; // optional param
87
- bool mul_ = false ; // can be specified multiple times
80
+ bool opt_ = false ; // optional param
81
+ bool mul_ = false ; // can be specified multiple times
88
82
89
- argval values_; // parsed value(s)
83
+ argval values_; // parsed value(s)
90
84
};
91
85
92
86
// //////////////////////////////////////////////////////////////////////////////
93
87
// program argument (either option or param)
94
88
struct arg
95
89
{
96
- arg (string name1, string description) :
97
- arg{move (name1), spec{ }, move (description)}
90
+ arg (std:: string name1, std:: string description) :
91
+ arg{std:: move (name1), spec{ }, std:: move (description)}
98
92
{ }
99
- arg (string name1, string name2, string description) :
100
- arg{move (name1), move (name2), spec{ }, move (description)}
93
+ arg (std:: string name1, std:: string name2, std:: string description) :
94
+ arg{std:: move (name1), std:: move (name2), spec{ }, std:: move (description)}
101
95
{ }
102
- arg (string name1, string name2, string name3, string description) :
103
- arg{move (name1), move (name2), move (name3), spec{ }, move (description)}
96
+ arg (std:: string name1, std:: string name2, std:: string name3, std:: string description) :
97
+ arg{std:: move (name1), std:: move (name2), std:: move (name3), spec{ }, std:: move (description)}
104
98
{ }
105
99
106
- arg (string name1, spec, string description);
107
- arg (string name1, string name2, spec, string description);
108
- arg (string name1, string name2, string name3, spec, string description);
100
+ arg (std:: string name1, spec, std:: string description);
101
+ arg (std:: string name1, std:: string name2, spec, std:: string description);
102
+ arg (std:: string name1, std:: string name2, std:: string name3, spec, std:: string description);
109
103
110
104
auto is_option () const { return std::holds_alternative<option>(val_); }
111
105
auto is_param () const { return std::holds_alternative<param>(val_); }
@@ -124,18 +118,18 @@ struct args
124
118
args () = default ;
125
119
explicit args (std::initializer_list<arg> il)
126
120
{
127
- for (auto & el : il) add (move (el));
121
+ for (auto & el : il) add (std:: move (el));
128
122
}
129
123
130
124
void add (arg);
131
125
132
126
template <typename ... Ts>
133
127
void add (Ts&&... vs) { add (arg{ std::forward<Ts>(vs)... }); }
134
128
135
- argval const & operator [](string_view) const ;
129
+ argval const & operator [](std:: string_view) const ;
136
130
137
131
void parse (int argc, char * argv[]);
138
- string usage (string_view program, string_view preamble = { }, string_view prologue = { }, string_view epilogue = { }) const ;
132
+ std:: string usage (std:: string_view program, std:: string_view preamble = { }, std:: string_view prologue = { }, std:: string_view epilogue = { }) const ;
139
133
140
134
private:
141
135
std::vector<option> options_;
@@ -148,28 +142,28 @@ struct args
148
142
// //////////////////////////////////////////////////////////////////////////////
149
143
struct argument_exception : std::invalid_argument
150
144
{
151
- argument_exception (string_view what, string_view why) :
152
- std::invalid_argument{string{what}+" : " +string{why}+" ." }
145
+ argument_exception (std:: string_view what, std:: string_view why) :
146
+ std::invalid_argument{std:: string{what}+" : " +std:: string{why}+" ." }
153
147
{ }
154
148
};
155
149
156
150
struct invalid_definition : argument_exception
157
151
{
158
- invalid_definition (string_view why) :
152
+ invalid_definition (std:: string_view why) :
159
153
argument_exception{" Invalid definition" , why}
160
154
{ }
161
155
};
162
156
163
157
struct invalid_argument : argument_exception
164
158
{
165
- invalid_argument (string_view why) :
159
+ invalid_argument (std:: string_view why) :
166
160
argument_exception{" Invalid argument" , why}
167
161
{ }
168
162
};
169
163
170
164
struct missing_argument : argument_exception
171
165
{
172
- missing_argument (string_view why) :
166
+ missing_argument (std:: string_view why) :
173
167
argument_exception{" Missing argument" , why}
174
168
{ }
175
169
};
0 commit comments