libcommonc++  0.7
Application.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_Application_hxx
24 #define __ccxx_Application_hxx
25 
26 #include <commonc++/Common.h++>
27 #include <commonc++/String.h++>
28 
29 #include <cstddef>
30 
31 namespace ccxx {
32 
43 {
44  private:
45 
46  class CommandOption
47  {
48  friend class Application;
49 
50  protected:
51 
52  char _opt;
53  String _longOpt;
54  bool _hasArg;
55  String _argName;
56  String _argDesc;
57 
58  CommandOption(char opt, const String& longOpt, const String& argName,
59  const String& argDesc);
60 
61  inline bool hasOpt() const
62  { return(_opt != NUL); }
63 
64  inline bool hasLongOpt() const
65  { return(! _longOpt.isNull()); }
66 
67  inline char getOpt() const
68  { return (_opt); }
69 
70  inline String getLongOpt() const
71  { return(_longOpt); }
72 
73  inline bool hasArg() const { return(_hasArg); }
74 
75  inline const String& getArgDesc() const { return(_argDesc); }
76  inline const String& getArgName() const { return(_argName); }
77  };
78 
79  class CommandOptionList; // fwd decl
80 
81  public:
82 
95  Application(int argc, char** argv, const String& version = "0.0",
96  const String& compileDate = "",
97  const String& compileTime = "");
98 
100  virtual ~Application();
101 
102  protected:
103 
115  virtual void shutdown();
116 
143  bool registerOption(char opt, const String& longOpt,
144  const String& argName, const String& desc);
145 
159  bool parseOptions(int argc, char** argv, int& index);
160 
181  virtual bool processOption(char opt, const String& longOpt,
182  const String& arg = String::null);
183 
185  virtual void printUsage();
186 
191  void printError(const char* message, ...);
192 
194  String _version;
195  String _compileDate;
196  String _compileTime;
197  CommandOptionList* _options;
198  int _numLongOpts;
201  private:
202 
203  void buildUsageText();
204  void doShutdown();
205 
206  bool _argsParsed;
207  bool _useStyles;
208  String _helpText;
209  String _optString;
210 
211  static Application* _instance;
212 
213  static void _sighandler(int sig);
214 };
215 
216 } // namespace ccxx
217 
218 #endif // __ccxx_Application_hxx
bool isNull() const
Test if the string is null.
Definition: String.c++:375
#define COMMONCPP_API
Definition: Common.h++:126
A flexible, reference counted, copy-on-write, thread-safe, nullable string.
Definition: String.h++:50
Base functionality for an application, including command-line argument parsing and a shutdown handler...
Definition: Application.h++:42
#define NUL
The NUL character (ASCII value 0).
Definition: Common.h++:240
static const String null
The null string.
Definition: String.h++:1128
Definition: AllocationMap.c++:25