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