scsl 0.2.4
Shimmering Clarity Standard Library
Flag.h
Go to the documentation of this file.
1
22
23
24#include <cstdint>
25#include <functional>
26#include <map>
27#include <string>
28#include <variant>
29#include <vector>
30
31
32namespace scsl {
33
34
38enum class FlagType : uint8_t {
39 Unknown = 0,
40 Boolean = 1,
41 Integer = 2,
42 UnsignedInteger = 3,
43 SizeT = 4,
44 String = 5,
45};
46
47
49typedef union {
50 unsigned int u;
51 int i;
52 std::size_t size;
53 std::string *s;
54 bool b;
55} FlagValue;
56
57
59typedef struct {
61 bool WasSet;
62 std::string Name;
63 std::string Description;
65} Flag;
66
73Flag *NewFlag(std::string fName, FlagType fType, std::string fDescription);
74
131class Flags {
132public:
135 enum class ParseStatus : uint8_t {
138 Unknown = 0,
139
141 OK = 1,
142
145 EndOfFlags = 2,
146
148 NotRegistered = 3,
149
154 NotEnoughArgs = 4,
155 };
156
159 static std::string ParseStatusToString(ParseStatus status);
160
164 Flags(std::string fName);
165
170 Flags(std::string fName, std::string fDescription);
171
172 ~Flags();
173
181 bool Register(std::string fName,
182 FlagType fType,
183 std::string fDescription);
184
197 bool Register(std::string fName,
198 bool defaultValue,
199 std::string fDescription);
200
208 bool Register(std::string fName,
209 int defaultValue,
210 std::string fDescription);
211
220 bool Register(std::string fName,
221 unsigned int defaultValue,
222 std::string fDescription);
223
231 bool Register(std::string fName,
232 size_t defaultValue,
233 std::string fDescription);
234
242 bool Register(std::string fName,
243 std::string defaultValue,
244 std::string fDescription);
245
247 size_t Size();
248
254 Flag *Lookup(std::string fName);
255
257 bool ValueOf(std::string fName, FlagValue &value);
258
268 ParseStatus Parse(int argc, char **argv, bool skipFirst=true);
269
271 void Usage(std::ostream &os, int exitCode);
272
275 size_t NumArgs();
276
278 std::vector<std::string> Args();
279
286 std::string Arg(size_t index);
287
294 bool GetBool(std::string fName, bool &flagValue);
295
302 bool GetUnsignedInteger(std::string fName, unsigned int &flagValue);
303
310 bool GetInteger(std::string fName, int &flagValue);
311
318 bool GetString(std::string fName, std::string &flagValue);
319
326 bool GetSizeT(std::string fName, std::size_t &flagValue);
327
328private:
329 ParseStatus parseArg(int argc, char **argv, int &index);
330 Flag *checkGetArg(std::string fName, FlagType eType);
331
332 std::string name;
333 std::string description;
334 std::vector<std::string> args;
335 std::map<std::string, Flag *> flags;
336};
337
338
339} // namespace scsl
Definition: Flag.h:131
bool Register(std::string fName, FlagType fType, std::string fDescription)
Definition: Flag.cc:95
std::string Arg(size_t index)
Definition: Flag.cc:359
void Usage(std::ostream &os, int exitCode)
Print a usage message to the output stream.
Definition: Flag.cc:296
bool GetString(std::string fName, std::string &flagValue)
Definition: Flag.cc:430
bool GetSizeT(std::string fName, std::size_t &flagValue)
Definition: Flag.cc:420
size_t NumArgs()
Definition: Flag.cc:345
ParseStatus Parse(int argc, char **argv, bool skipFirst=true)
Definition: Flag.cc:258
bool GetBool(std::string fName, bool &flagValue)
Definition: Flag.cc:390
size_t Size()
Return the number of registered flags.
Definition: Flag.cc:172
bool ValueOf(std::string fName, FlagValue &value)
ValueOf returns the value of the flag in the.
Definition: Flag.cc:190
std::vector< std::string > Args()
Return the arguments as a vector.
Definition: Flag.cc:352
Flags(std::string fName)
Definition: Flag.cc:71
ParseStatus
Definition: Flag.h:135
bool GetInteger(std::string fName, int &flagValue)
Definition: Flag.cc:400
~Flags()
Definition: Flag.cc:83
static std::string ParseStatusToString(ParseStatus status)
Definition: Flag.cc:39
Flag * Lookup(std::string fName)
Definition: Flag.cc:179
bool GetUnsignedInteger(std::string fName, unsigned int &flagValue)
Definition: Flag.cc:410
scsl is the top-level namespace containing all the code in this library.
Definition: scsl.h:43
FlagType
Definition: Flag.h:38
@ String
std::string
@ UnsignedInteger
uint32_t
@ Unknown
Unsupported value type.
@ Integer
int32_t
Flag * NewFlag(std::string fName, FlagType fType, std::string fDescription)
Definition: Flag.cc:57
Flag describes an individual command-line flag.
Definition: Flag.h:59
std::string Name
The name of the flag.
Definition: Flag.h:62
FlagType Type
The type of the value in the flag.
Definition: Flag.h:60
FlagValue Value
The flag's value.
Definition: Flag.h:64
bool WasSet
The flag was set on the command-line.
Definition: Flag.h:61
std::string Description
A description of the flag.
Definition: Flag.h:63
FlagValue holds the value of a command line flag.
Definition: Flag.h:49
std::size_t size
FlagType::SizeT.
Definition: Flag.h:52
std::string * s
FlagType::String.
Definition: Flag.h:53
unsigned int u
FlagType::UnsignedInteger.
Definition: Flag.h:50
bool b
FlagType::Boolean.
Definition: Flag.h:54
int i
FlagType::Integer.
Definition: Flag.h:51