libDwm-0.9.45
DwmOptArgs.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2007, 2016
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// 3. The names of the authors and copyright holders may not be used to
17// endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
21// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
22// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
23// EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
24// DAMAGE.
25//
26// THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
27// DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
28// UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
29// REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
30// IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31// WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
32// OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
33// TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
39//---------------------------------------------------------------------------
40
41#ifndef _DWMOPTARGS_HH_
42#define _DWMOPTARGS_HH_
43
44#include <iostream>
45#include <map>
46#include <sstream>
47#include <string>
48#include <vector>
49
50#include "DwmStringUtils.hh"
51
52namespace Dwm {
53
54 //--------------------------------------------------------------------------
56 //--------------------------------------------------------------------------
57 template <typename T> class Argument;
58
59 //--------------------------------------------------------------------------
61 //--------------------------------------------------------------------------
62 template <typename T>
63 std::ostream & operator << (std::ostream & os, const Argument<T> & arg)
64 {
65 os << arg._value;
66 return(os);
67 }
68
69 //--------------------------------------------------------------------------
72 //--------------------------------------------------------------------------
73 template <typename T>
75 {
76 public:
77 //------------------------------------------------------------------------
79 //------------------------------------------------------------------------
80 Argument()
81 : _value()
82 {}
83
84 //------------------------------------------------------------------------
86 //------------------------------------------------------------------------
87 Argument & operator = (const std::string & s)
88 {
89 StringUtils::StringTo(s, _value);
90 return(*this);
91 }
92
93 //------------------------------------------------------------------------
95 //------------------------------------------------------------------------
96 operator T () const
97 {
98 return(_value);
99 }
100
101 //------------------------------------------------------------------------
103 //------------------------------------------------------------------------
104 friend std::ostream &
105 operator << <> (std::ostream & os, const Argument<T> & arg);
106
107 protected:
108 T _value;
109 };
110
111 //--------------------------------------------------------------------------
116 //--------------------------------------------------------------------------
117 class OptArg
118 {
119 public:
120 //------------------------------------------------------------------------
122 //------------------------------------------------------------------------
123 OptArg();
124
125 OptArg(const std::string & c, const std::string & longName = "",
126 bool isRequired = false, const std::string & value = "",
127 const std::string & help = "");
128
129 //------------------------------------------------------------------------
131 //------------------------------------------------------------------------
132 const std::string & Name() const;
133
134 //------------------------------------------------------------------------
136 //------------------------------------------------------------------------
137 const std::string & LongName() const;
138
139 //------------------------------------------------------------------------
141 //------------------------------------------------------------------------
142 bool IsRequired() const;
143
144 //------------------------------------------------------------------------
146 //------------------------------------------------------------------------
147 bool IsRequired(bool isRequired);
148
149 //------------------------------------------------------------------------
151 //------------------------------------------------------------------------
152 bool NeedsArg() const;
153
154 //------------------------------------------------------------------------
156 //------------------------------------------------------------------------
157 const std::string & Value() const;
158
159 //------------------------------------------------------------------------
161 //------------------------------------------------------------------------
162 const std::string & Value(const std::string & value);
163
164 //------------------------------------------------------------------------
166 //------------------------------------------------------------------------
167 const std::string & Help() const;
168
169 //------------------------------------------------------------------------
171 //------------------------------------------------------------------------
172 const std::string & Help(const std::string & help);
173
174 protected:
175 std::string _name;
176 std::string _longName;
177 bool _isRequired;
178 bool _needsArg;
179 std::string _value;
180 std::string _help;
181 };
182
183 //--------------------------------------------------------------------------
185 //--------------------------------------------------------------------------
187 {
188 public:
189 //------------------------------------------------------------------------
191 //------------------------------------------------------------------------
192 OptArgs();
193
194 //------------------------------------------------------------------------
196 //------------------------------------------------------------------------
197 void AddOptArg(const OptArg & optarg);
198
199 //------------------------------------------------------------------------
201 //------------------------------------------------------------------------
202 void AddOptArg(const std::string & name, const std::string & longName,
203 bool isRequired = false, const std::string & def = "",
204 const std::string & help = "");
205
206 void AddNormalArg(const std::string & name, bool required);
207
208 //------------------------------------------------------------------------
210 //------------------------------------------------------------------------
211 template <typename T>
212 T Get(int c)
213 {
214 Argument<T> rc;
215 std::map<int,OptArg>::const_iterator i = _args.find(c);
216 if (i != _args.end()) {
217 rc = i->second.Value();
218 }
219 return(rc);
220 }
221
222 //------------------------------------------------------------------------
224 //------------------------------------------------------------------------
225 template <typename T>
226 T Get(const std::string & s)
227 {
228 Argument<T> rc;
229 std::map<std::string,OptArg *>::const_iterator i = _longArgs.find(s);
230 if (i != _longArgs.end()) {
231 rc = i->second->Value();
232 }
233 return(rc);
234 }
235
236 //------------------------------------------------------------------------
240 //------------------------------------------------------------------------
241 int Parse(int argc, const std::string argv[]);
242
243 //------------------------------------------------------------------------
247 //------------------------------------------------------------------------
248 int Parse(int argc, char *argv[]);
249
250 //------------------------------------------------------------------------
252 //------------------------------------------------------------------------
253 void Usage(const std::string & argv0) const;
254
255 protected:
256 std::map<int,OptArg> _args;
257 std::map<std::string,OptArg *> _longArgs;
258
259 std::vector<std::pair<std::string,bool> > _normalArgs;
260 };
261
262
263} // namespace Dwm
264
265#endif // _DWMOPTARGS_HH_
266
267//---------------------------- emacs settings -----------------------------
268// Local Variables:
269// mode: C++
270// tab-width: 2
271// indent-tabs-mode: nil
272// c-basic-offset: 2
273// End:
274//-------------------------------------------------------------------------
std::ostream & operator<<(std::ostream &os, const Dwm::ProcessInfo &pi)
ostream output operator
Miscellaneous string utilities.
Encapsulate a single argument.
Definition DwmOptArgs.hh:75
Encapsulates a command line option and its argument (if it takes an argument).
Definition DwmOptArgs.hh:118
const std::string & Value(const std::string &value)
Sets and returns the value of the argument.
const std::string & Help(const std::string &help)
Sets and returns the help text for the argument.
bool IsRequired(bool isRequired)
Sets whether or not the option is required.
bool NeedsArg() const
Returns true if an argument is required for the option.
const std::string & Name() const
Returns the name of the option.
const std::string & LongName() const
Returns the long name for the option.
const std::string & Value() const
Returns the value of the argument.
bool IsRequired() const
Returns whether or not the option is required.
const std::string & Help() const
Returns the help text for the argument.
Class to encapsulate command line options and parse them.
Definition DwmOptArgs.hh:187
T Get(int c)
Fetches the value of an optional argument by its short name.
Definition DwmOptArgs.hh:212
int Parse(int argc, const std::string argv[])
Parses all optional arguments.
void AddOptArg(const OptArg &optarg)
Adds an optional argument.
int Parse(int argc, char *argv[])
Parses all optional arguments.
void AddOptArg(const std::string &name, const std::string &longName, bool isRequired=false, const std::string &def="", const std::string &help="")
Adds an optional argument.
void Usage(const std::string &argv0) const
Prints the usage statement.
T Get(const std::string &s)
Fetches the value of an optional argument by its long name.
Definition DwmOptArgs.hh:226