scsl 1.0.1
Shimmering Clarity Standard Library
Loading...
Searching...
No Matches
Flags.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
133class Flags {
134public:
137 enum class ParseStatus : uint8_t {
140 Unknown = 0,
141
143 OK = 1,
144
147 EndOfFlags = 2,
148
150 NotRegistered = 3,
151
156 NotEnoughArgs = 4,
157 };
158
161 static std::string ParseStatusToString(ParseStatus status);
162
166 Flags(std::string fName);
167
172 Flags(std::string fName, std::string fDescription);
173
175
183 bool Register(std::string fName,
184 FlagType fType,
185 std::string fDescription);
186
199 bool Register(std::string fName,
200 bool defaultValue,
201 std::string fDescription);
202
210 bool Register(std::string fName,
211 int defaultValue,
212 std::string fDescription);
213
222 bool Register(std::string fName,
223 unsigned int defaultValue,
224 std::string fDescription);
225
233 bool Register(std::string fName,
234 size_t defaultValue,
235 std::string fDescription);
236
244 bool Register(std::string fName,
245 std::string defaultValue,
246 std::string fDescription);
247
255 bool Register(std::string fName,
256 const char *defaultValue,
257 std::string fDescription);
258
260 size_t Size();
261
267 Flag *Lookup(std::string fName);
268
270 bool ValueOf(std::string fName, FlagValue &value);
271
281 ParseStatus Parse(int argc, char **argv, bool skipFirst=true);
282
284 void Usage(std::ostream &os, int exitCode);
285
288 size_t NumArgs();
289
291 std::vector<std::string> Args();
292
299 std::string Arg(size_t index);
300
307 bool GetBool(std::string fName, bool &flagValue);
308
315 bool GetUnsignedInteger(std::string fName, unsigned int &flagValue);
316
323 bool GetInteger(std::string fName, int &flagValue);
324
331 bool GetString(std::string fName, std::string &flagValue);
332
339 bool GetSizeT(std::string fName, std::size_t &flagValue);
340
341private:
342 ParseStatus parseArg(int argc, char **argv, int &index);
343 Flag *checkGetArg(std::string& fName, FlagType eType);
344
345 std::string name;
346 std::string description;
347 std::vector<std::string> args;
348 std::map<std::string, Flag *> flags;
349};
350
351
352} // namespace scsl
Basic facility for processing command line flags.
Definition Flags.h:133
bool Register(std::string fName, FlagType fType, std::string fDescription)
std::string Arg(size_t index)
void Usage(std::ostream &os, int exitCode)
Print a usage message to the output stream.
bool Register(std::string fName, const char *defaultValue, std::string fDescription)
bool GetString(std::string fName, std::string &flagValue)
bool GetSizeT(std::string fName, std::size_t &flagValue)
size_t NumArgs()
bool Register(std::string fName, int defaultValue, std::string fDescription)
ParseStatus Parse(int argc, char **argv, bool skipFirst=true)
bool Register(std::string fName, bool defaultValue, std::string fDescription)
bool GetBool(std::string fName, bool &flagValue)
bool Register(std::string fName, std::string defaultValue, std::string fDescription)
size_t Size()
Return the number of registered flags.
bool ValueOf(std::string fName, FlagValue &value)
ValueOf returns the value of the flag in the.
bool Register(std::string fName, size_t defaultValue, std::string fDescription)
std::vector< std::string > Args()
Return the arguments as a vector.
Flags(std::string fName)
ParseStatus
Definition Flags.h:137
@ NotRegistered
The command line flag provided isn't registered.
@ OK
Parsing succeeded.
bool GetInteger(std::string fName, int &flagValue)
bool Register(std::string fName, unsigned int defaultValue, std::string fDescription)
Flags(std::string fName, std::string fDescription)
Flag * Lookup(std::string fName)
bool GetUnsignedInteger(std::string fName, unsigned int &flagValue)
static std::string ParseStatusToString(ParseStatus status)
scsl is the top-level namespace containing all the code in this library.
Definition scsl.h:43
FlagType
Definition Flags.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)
NewFlag is a helper function for constructing a new flag.
Individual command-line flag.
Definition Flags.h:59
std::string Name
The name of the flag.
Definition Flags.h:62
FlagType Type
The type of the value in the flag.
Definition Flags.h:60
FlagValue Value
The flag's value.
Definition Flags.h:64
bool WasSet
The flag was set on the command-line.
Definition Flags.h:61
std::string Description
A description of the flag.
Definition Flags.h:63
FlagValue holds the value of a command line flag.
Definition Flags.h:49
std::size_t size
FlagType::SizeT.
Definition Flags.h:52
std::string * s
FlagType::String.
Definition Flags.h:53
unsigned int u
FlagType::UnsignedInteger.
Definition Flags.h:50
bool b
FlagType::Boolean.
Definition Flags.h:54
int i
FlagType::Integer.
Definition Flags.h:51