scsl  0.2.5
Shimmering Clarity Standard Library
Flag.h
Go to the documentation of this file.
1 
23 
24 #include <cstdint>
25 #include <functional>
26 #include <map>
27 #include <string>
28 #include <variant>
29 #include <vector>
30 
31 
32 namespace scsl {
33 
34 
38 enum 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 
49 typedef union {
50  unsigned int u;
51  int i;
52  std::size_t size;
53  std::string *s;
54  bool b;
55 } FlagValue;
56 
57 
59 typedef struct {
61  bool WasSet;
62  std::string Name;
63  std::string Description;
65 } Flag;
66 
73 Flag *NewFlag(std::string fName, FlagType fType, std::string fDescription);
74 
133 class Flags {
134 public:
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 
249  size_t Size();
250 
256  Flag *Lookup(std::string fName);
257 
259  bool ValueOf(std::string fName, FlagValue &value);
260 
270  ParseStatus Parse(int argc, char **argv, bool skipFirst=true);
271 
273  void Usage(std::ostream &os, int exitCode);
274 
277  size_t NumArgs();
278 
280  std::vector<std::string> Args();
281 
288  std::string Arg(size_t index);
289 
296  bool GetBool(std::string fName, bool &flagValue);
297 
304  bool GetUnsignedInteger(std::string fName, unsigned int &flagValue);
305 
312  bool GetInteger(std::string fName, int &flagValue);
313 
320  bool GetString(std::string fName, std::string &flagValue);
321 
328  bool GetSizeT(std::string fName, std::size_t &flagValue);
329 
330 private:
331  ParseStatus parseArg(int argc, char **argv, int &index);
332  Flag *checkGetArg(std::string fName, FlagType eType);
333 
334  std::string name;
335  std::string description;
336  std::vector<std::string> args;
337  std::map<std::string, Flag *> flags;
338 };
339 
340 
341 } // namespace scsl
Definition: Flag.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 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)
Flags(std::string fName)
ParseStatus
Definition: Flag.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)
std::vector< std::string > Args()
Return the arguments as a vector.
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
Flag * NewFlag(std::string fName, FlagType fType, std::string fDescription)
FlagType
Definition: Flag.h:38
@ String
std::string
@ UnsignedInteger
uint32_t
@ Unknown
Unsupported value type.
@ Integer
int32_t
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