Elementa v8.0.0
Minimalistic library for any C++ application (C++11 and up)
Loading...
Searching...
No Matches
commandline.h
Go to the documentation of this file.
1
3#include "elementa/license.inc"
4#include "elementa/checks.inc"
5
6#ifndef ELEMENTA_UTILS_COMMANDLINE
7#define ELEMENTA_UTILS_COMMANDLINE
8
9#include <vector>
11#include "elementa/base/enums.h"
13#include "elementa/base/serial_channels.h"
14
15namespace elementa
16{
17
18namespace utils
19{
20
39/* **********************************************************************
40
41 Class: CLOption
42
43*************************************************************************/
44
46
48{
49 public:
50
52
55 kFreeText,
56 kNatural,
57 kPositive,
58 kInteger,
59 kNumber,
60 kEnum,
61 kNone
62 );
63
65 ELE_CLASS_ENUM(PresenceKind,
66 kMandatory,
67 kOptional,
68 kAlone
71 );
72
73
75 class StringGroups: public std::vector<elementa::base::Strings>
76 {
77 public:
78
79 using Base = std::vector<elementa::base::Strings>;
80
81 using Base::Base;
82
84
86 void check(elementa::base::Strings::size_type minnumelems = 0) const;
87
89 std::string to_string(const std::string & preffix = "",
90 const std::string & sep = " ") const;
91 };
92
94 struct CLData
95 {
97 class Vector: public std::vector<CLData>
98 {
99 public:
100
101 using Base = std::vector<CLData>;
102
103 using Base::Base;
104
105 Base::const_iterator find(const std::string & shortname) const;
106 std::string to_string(const std::string & preff,
107 const std::string & sep = " ") const;
108 };
109
110 std::string shortname;
111 std::string value;
112 };
113
114
116 class Vector: public std::vector<CLOption>
117 {
118 public:
119
121 using Base = std::vector<CLOption>;
122
124 using Base::Base;
125
127 Base::const_iterator find(const std::string & n,
128 bool shortn = true) const;
129
131
133 void checkSpec(void) const;
134
136
137 bool therearedeps(void) const;
138 };
139
140
143 {
144 public:
145
146 Error(const std::string & explanation):
148 std::string{"Command line error: "} +
150 {}
151
153 };
154
155 class InvOptSpec: public Error
156 {
157 public:
158
159 InvOptSpec(const std::string & details):
161 "Invalid option specification",details)}
162 {}
163
165 };
166
167 class InvOptValue: public Error
168 {
169 public:
170
171 InvOptValue(const std::string & details):
173 "Invalid value for option",details)}
174 {}
175
177 };
178
179 class InvStringGroup: public Error
180 {
181 public:
182
183 InvStringGroup(const std::string & details):
185 "Invalid group of strings",details)}
186 {}
187
189 };
190
191
192 std::string shortname;
193 std::string longname;
194 PresenceKind presence;
195 ValKind valkind;
201 std::string dflt;
209 std::string help;
210
211
213 void checkSpec(void) const;
214
216 void checkValue(const std::string & v) const;
217
219
220 elementa::base::Strings::size_type whichIsDflt(void) const;
221
223 std::string helpValues(void) const;
224
226 bool dependsOn(const CLOption & other) const;
227
229 bool operator==(const CLOption & oo) const
230 { return(shortname == oo.shortname); }
231
233 bool operator!=(const CLOption & oo) const
234 { return(!operator==(oo)); }
235};
236
237
238/* **********************************************************************
239
240 Class: CLOptionManager
241
242*************************************************************************/
243
246{
247 public:
248
250 {
251 public:
252
253 InvPreffix(const std::string & details):
255 "Invalid preffix in a CL argument",
256 details)}
257 {}
258
260 };
261
263 {
264 public:
265
266 UnknownOption(const std::string & details):
268 "Unknown option",details)}
269 {}
270
272 };
273
275 {
276 public:
277
278 MandatoryOption(const std::string & details):
280 "Mandatory option not found",
281 details)}
282 {}
283
285 };
286
288 {
289 public:
290
291 MandatoryValue(const std::string & details):
293 "Mandatory value not found",details)}
294 {}
295
297 };
298
300 {
301 public:
302
303 MustBeAlone(const std::string & details):
305 "Option must be alone",details)}
306 {}
307
309 };
310
312 {
313 public:
314
315 UnmetDeps(const std::string & details):
317 "Option dependencies are not met",
318 details)}
319 {}
320
322 };
323
325 {
326 public:
327
328 IncompOpts(const std::string & details):
330 "Incompatible options",details)}
331 {}
332
334 };
335
336
338
349 CLOptionManager(const std::string & progname,
350 const CLOption::Vector & opts,
351 const CLOption::StringGroups & incomps,
352 const std::string preff = "-");
353
355 virtual ~CLOptionManager(void) = default;
356
357
359 const std::string & progName(void) const noexcept
360 { return(nameofprog_); }
361
363 const std::string & preffix(void) const noexcept
364 { return(preff_); }
365
367
375 std::string help(const std::string & shortname = "",
376 unsigned charwidth = 80);
377
379
384 void process(int argc, char *argv[]);
385
387
391 bool present(const std::string & shortn, std::string & val) const;
392
394
398 bool present(const std::string & shortn) const;
399
401
402 const CLOption & optDef(const std::string & shortn) const;
403
405
406 elementa::base::Strings depOpts(const std::string & shortn) const;
407
409
412 std::string processed_tostring(void) const
413 { return(argvdata_.to_string(preff_,", ")); }
414
415
416 private:
417
418 const std::string nameofprog_;
419 const CLOption::Vector & opts_;
420 const CLOption::StringGroups & optincs_;
421 const std::string preff_;
422 CLOption::CLData::Vector argvdata_;
423
424 std::string helpOpt(const CLOption & opt, bool withdepinfo, size_t depth,
425 unsigned charwidth);
426 void recursivehelp(std::vector<CLOption::Vector::size_type> parents,
427 std::string & h, unsigned charwidth, size_t depth = 0);
428 std::string formHelp(bool hierarchized, unsigned charwidth);
429 std::string preffixedOpt(const CLOption & opt, bool shortn = true);
430 std::string getoptfromtxt(const std::string & o) const;
431};
432
433 // CommandLine
435
436} // End os namespace
437
438} // End elementa namespace
439
440
441#endif
442
Base class for all errors that can be thrown about CL options.
Definition: commandline.h:143
A number of groups of strings.
Definition: commandline.h:76
std::string to_string(const std::string &preffix="", const std::string &sep=" ") const
Return a string with all the groups as sets.
void check(elementa::base::Strings::size_type minnumelems=0) const
Check that no group of strings intersects other or has repeated strs.
A number of options as they are found in a command line.
Definition: commandline.h:117
Base::const_iterator find(const std::string &n, bool shortn=true) const
Find an option through its name N, either SHORTN or not.
bool therearedeps(void) const
Return TRUE if any option in the vector has dependencies.
void checkSpec(void) const
Check that the vector of options is valid.
std::vector< CLOption > Base
Shortcut.
Definition: commandline.h:121
ELE_CLASS_ENUM(ValKind, kFreeText, kNatural, kPositive, kInteger, kNumber, kEnum, kNone)
Kind of values that the option may have.
std::string shortname
Shortname of the option.
Definition: commandline.h:110
const std::string & progName(void) const noexcept
Return a reference to the program name.
Definition: commandline.h:359
StringGroups dependencies
Dependencies.
Definition: commandline.h:203
bool operator!=(const CLOption &oo) const
The opposite to operator==.
Definition: commandline.h:233
ELE_CLASS_ENUM(PresenceKind, kMandatory, kOptional, kAlone)
Kind of presence an option may have.
bool present(const std::string &shortn, std::string &val) const
If that option is present after processing, return TRUE and fill VAL.
PresenceKind presence
Presence of the option.
Definition: commandline.h:194
void checkValue(const std::string &v) const
Check that the value is valid for the option. Throw InvOptValue if not.
bool dependsOn(const CLOption &other) const
Return TRUE if this option depends on OTHER.
std::string help
Description of the option for showing help.
Definition: commandline.h:209
bool present(const std::string &shortn) const
If that option is present after processing, return TRUE.
void checkSpec(void) const
Check that the option specification is valid. Throw InvOptSpec if not.
std::string helpValues(void) const
Return a string with the help about the values of the option.
ValKind valkind
Kind of values it may have.
Definition: commandline.h:195
const CLOption & optDef(const std::string &shortn) const
Return a reference to the definition of the given option.
std::string value
Value given to the option, or "" if none.
Definition: commandline.h:111
elementa::base::Strings depOpts(const std::string &shortn) const
Return all the options dependent on SHORTN, or empty if none.
elementa::base::Strings values
Only used if valkind==ValKind::kEnum.
Definition: commandline.h:196
std::string dflt
Default value. If empty, opt cannot have empty value.
Definition: commandline.h:201
std::string help(const std::string &shortname="", unsigned charwidth=80)
Return a string with formatted help.
const std::string & preffix(void) const noexcept
Return a reference to the preffix.
Definition: commandline.h:363
elementa::base::Strings::size_type whichIsDflt(void) const
If it is an enum, return the index in VALUES of DFLT.
CLOptionManager(const std::string &progname, const CLOption::Vector &opts, const CLOption::StringGroups &incomps, const std::string preff="-")
Constructor from the options.
void process(int argc, char *argv[])
Read and process the command line parameters according to the options.
std::string longname
Long alternative name (may be empty).
Definition: commandline.h:193
std::string processed_tostring(void) const
Return the content of the command line after processing, as a text.
Definition: commandline.h:412
std::string shortname
Short option name; its unique identifier.
Definition: commandline.h:192
virtual ~CLOptionManager(void)=default
Just to assure polymorphic deletions.
bool mandatoryval
TRUE if a value must be provided for the option.
Definition: commandline.h:199
bool operator==(const CLOption &oo) const
Return TRUE if both options have the same shortname.
Definition: commandline.h:229
Specification for an option in the command line.
Definition: commandline.h:48
A class in charge of parsing and managing command line options.
Definition: commandline.h:246
Data provided in the command line about an option.
Definition: commandline.h:95
const char * explanation(void) const noexcept
Return the explanation only. It will live as long as this exception.
Definition: exceptions.h:164
Base class for all errors / exceptions in Elementa. Just derive from it.
Definition: exceptions.h:113
#define ELE_CLASS_EXCOVERRIDE(C)
Shortening macro that must be used inside classes derived from Exc.
Definition: exceptions.h:64
A number of strings.
Definition: strings.h:564
std::string concatWithMiddle(const std::string &s1, const std::string &s2, const std::string &m=". ")
Concatenate two strings putting a middle one only if the second is not empty.