libUTL++
CmdLineArgs.h
1 #pragma once
2 
4 
5 #include <libutl/Array.h>
6 #include <libutl/RBtree.h>
7 #include <libutl/String.h>
8 
10 
11 UTL_NS_BEGIN;
12 
14 
15 class CmdLineArg;
16 
18 
30 
32 class CmdLineArgs : public Object, protected FlagsMI
33 {
35 
36 public:
42  CmdLineArgs(int argc, char** argv)
43  {
44  init(argc, argv);
45  }
46 
51  CmdLineArgs(const Array& args)
52  {
53  init(args);
54  }
55 
57  void clear();
58 
60  const TArray<String>&
61  getArray() const
62  {
63  return _array;
64  }
65 
67  size_t
68  idx() const
69  {
70  return _idx;
71  }
72 
80  bool isSet(const String& sw, String* val = nullptr, size_t* swIdx = nullptr);
81 
83  bool
84  isOwner() const
85  {
86  return getFlag(flg_owner);
87  }
88 
90  void
91  setOwner(bool owner)
92  {
93  setFlag(flg_owner, owner);
94  }
95 
97  size_t
98  items() const
99  {
100  return _array.items();
101  }
102 
107  bool
108  ok() const
109  {
110  return _args.empty();
111  }
112 
118  void parse(int argc, char** argv);
119 
124  void parse(const Array& args);
125 
132  bool printErrors(Stream& os, const String* prefix = nullptr);
133 
138  const String* operator[](size_t idx) const
139  {
140  return _array.operator[](idx);
141  }
142 
144  const String* operator[](int idx) const
145  {
146  return _array.operator[]((size_t)idx);
147  }
148 
153  const String& operator()(size_t idx) const
154  {
155  return _array.operator()(idx);
156  }
157 
159  const String& operator()(int idx) const
160  {
161  return _array.operator()((size_t)idx);
162  }
163 
164 private:
165  enum flg_t
166  {
167  flg_owner
168  };
169 
170 private:
171  void
172  init()
173  {
174  setOwner(true);
175  clear();
176  }
177 
178  void
179  init(int argc, char** argv)
180  {
181  setOwner(true);
182  parse(argc, argv);
183  }
184 
185  void
186  init(const Array& args)
187  {
188  setOwner(true);
189  parse(args);
190  }
191 
192  void
193  deInit()
194  {
195  }
196 
197 private:
198  size_t _idx;
199  TArray<String> _array;
200  TRBtree<CmdLineArg> _args;
201 };
202 
204 
205 UTL_NS_END;
const String * operator[](size_t idx) const
Return the argument at the given index.
Definition: CmdLineArgs.h:138
bool ok() const
Determine whether any unknown switches were given.
Definition: CmdLineArgs.h:108
void deInit()
De-initialize UTL++.
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
Character string.
Definition: String.h:31
Mix-in to provide 64-bits for space-efficient storage of up to 64 boolean flags.
Definition: FlagsMI.h:25
Parse command-line arguments.
Definition: CmdLineArgs.h:32
CmdLineArgs(const Array &args)
Constructor.
Definition: CmdLineArgs.h:51
SortedCollection that stores objects in an array.
Definition: Array.h:116
const String * operator[](int idx) const
Same as above, but takes a signed argument.
Definition: CmdLineArgs.h:144
const String & operator()(size_t idx) const
Return the argument at the given index.
Definition: CmdLineArgs.h:153
const TArray< String > & getArray() const
Get the argument array.
Definition: CmdLineArgs.h:61
Template version of Array.
Definition: TArray.h:25
void setOwner(bool owner)
Set the ownership flag.
Definition: CmdLineArgs.h:91
size_t idx() const
Return the index of the first non-switch argument.
Definition: CmdLineArgs.h:68
size_t items() const
Return the number of arguments.
Definition: CmdLineArgs.h:98
CmdLineArgs(int argc, char **argv)
Constructor.
Definition: CmdLineArgs.h:42
bool isOwner() const
Get the ownership flag.
Definition: CmdLineArgs.h:84
Stream I/O abstraction.
Definition: Stream.h:68
const String & operator()(int idx) const
Same as above, but takes a signed argument.
Definition: CmdLineArgs.h:159
Root of UTL++ class hierarchy.
Definition: Object.h:52
void init()
Initialize UTL++.