scsl  0.1.1
Shimmering Clarity Standard Library
Flag.h
Go to the documentation of this file.
1 
25 
26 #include <cstdint>
27 #include <functional>
28 #include <map>
29 #include <string>
30 #include <variant>
31 #include <vector>
32 
33 
34 namespace scsl {
35 
36 
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 
48 enum class ParseStatus : uint8_t {
49  Unknown = 0,
50  OK = 1,
51  EndOfFlags = 2,
52  NotRegistered = 3,
53  NotEnoughArgs = 4,
54 };
55 
56 std::string
57 ParseStatusToString(ParseStatus status);
58 
59 
60 typedef union {
61  unsigned int u;
62  int i;
63  std::size_t size;
64  std::string *s;
65  bool b;
66 } FlagValue;
67 
68 
69 typedef struct {
70  FlagType Type;
71  bool WasSet;
72  std::string Name;
73  std::string Description;
74  FlagValue Value;
75 } Flag;
76 
77 Flag *
78 NewFlag(FlagType fType, std::string fName, std::string fDescription);
79 
80 
81 class Flags {
82 public:
83  Flags(std::string fName);
84  Flags(std::string fName, std::string fDescription);
85 
86  bool Register(std::string fName,
87  FlagType fType,
88  std::string fDescription);
89  bool Register(std::string fName,
90  bool defaultValue,
91  std::string fDescription);
92  bool Register(std::string fName,
93  int defaultValue,
94  std::string fDescription);
95  bool Register(std::string fName,
96  unsigned int defaultValue,
97  std::string fDescription);
98  bool Register(std::string fName,
99  size_t defaultValue,
100  std::string fDescription);
101  bool Register(std::string fName,
102  std::string defaultValue,
103  std::string fDescription);
104  size_t Size();
105  Flag *Lookup(std::string fName);
106  bool ValueOf(std::string fName, FlagValue &value);
107 
108  ParseStatus Parse(int argc, char **argv);
109 
110  void Usage(std::ostream &os, int exitCode);
111 
112  size_t NumArgs();
113  std::vector<std::string> Args();
114  std::string Arg(int index);
115 
116  bool GetBool(std::string fName, bool &flagValue);
117  bool GetUnsignedInteger(std::string fName, unsigned int &flagValue);
118  bool GetInteger(std::string fName, int &flagValue);
119  bool GetString(std::string fName, std::string &flagValue);
120  bool GetSizeT(std::string fName, std::size_t &flagValue);
121 
122 
123 private:
124  ParseStatus parseArg(int argc, char **argv, int &index);
125  Flag *checkGetArg(std::string fName, FlagType eType);
126 
127  std::string name;
128  std::string description;
129  std::vector<std::string> args;
130  std::map<std::string, Flag *> flags;
131 };
132 
133 
134 } // namespace scsl
Definition: Flag.h:81
scsl is the top-level namespace containing all the code in this library.
Definition: scsl.h:37
FlagType
FlagType indicates the value held in a FlagValue.
Definition: Flag.h:38
@ UnsignedInteger
uint32_t
@ Integer
int32_t
Definition: Flag.h:69
Definition: Flag.h:60