Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Manager.h
1 /*
2  * $Id$
3  * Manager class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2004-07, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef ELM_OPTION_MANAGER_H
23 #define ELM_OPTION_MANAGER_H
24 
25 #include <elm/ptr.h>
26 #include <elm/data/ListMap.h>
27 #include <elm/data/Vector.h>
28 #include <elm/option/Option.h>
29 #include <elm/option/SwitchOption.h>
30 #include <elm/option/ValueOption.h>
31 #include <elm/util/Version.h>
32 #include <elm/util/MessageException.h>
33 
34 namespace elm { namespace option {
35 
36 // Manager
37 class Manager {
38  friend class Option;
39 public:
40 
41  class Make {
42  friend class Manager;
43  public:
44  inline Make(void): _help(false), _version_opt(false) { }
45  inline Make(cstring program, Version version = Version::ZERO): _program(program), _version(version), _help(false), _version_opt(false) { }
46  inline Make& author(cstring s) { _author = s; return *this; }
47  inline Make& copyright(cstring s) { _copyright = s; return *this; }
48  inline Make& description(cstring s) { _description = s; return *this; }
49  inline Make& free_argument(cstring s) { _free_argument_description = s; return *this; }
50  inline Make& help(void) { _help = true; return *this; }
51  inline Make& version(void) { _version_opt = true; return *this; }
52  private:
53  cstring _program;
54  Version _version;
55  cstring _author;
56  cstring _copyright;
57  cstring _description;
58  cstring _free_argument_description;
59  bool _help;
60  bool _version_opt;
61  };
62 
63  // shortcut to make options
64  inline SwitchOption::Make make_switch(void) { return SwitchOption::Make(*this); }
65  template <class T> typename ValueOption<T>::Make make_value(void) { return typename ValueOption<T>::Make(*this); }
66 
67  typedef const char * const *argv_t;
68  Manager(void);
69  Manager(const Make& maker);
70  virtual ~Manager(void);
71  void addOption(Option *option);
72  void removeOption(Option *option);
73  void parse(int argc, argv_t argv);
74  int manage(int artc, argv_t argv);
75 
76  virtual void displayHelp(void);
77  virtual void displayVersion(void);
78 
79  // accessors
80  inline cstring program(void) const { return info._program; }
81  inline const Version& version(void) const { return info._version; }
82  inline cstring author(void) const { return info._author; }
83  inline cstring copyright(void) const { return info._copyright; }
84  inline cstring description(void) const { return info._description; }
85  inline cstring freeArgumentDescription(void) const { return info._free_argument_description; }
86 
87  // deprecated
88  inline cstring getProgram(void) const { return info._program; }
89  inline const Version& getVersion(void) const { return info._version; }
90  inline cstring getAuthor(void) const { return info._author; }
91  inline cstring getCopyright(void) const { return info._copyright; }
92  inline cstring getDescription(void) const { return info._description; }
93  inline cstring getFreeArgumentDescription(void) const { return info._free_argument_description; }
94 
95 protected:
97  virtual void process(String arg);
98  virtual void run(void);
99  inline const Vector<string>& freeArguments() const { return _frees; }
100 
101 private:
102  Vector<Option *> options;
103  void processOption(Option *option, int& i, int argc, argv_t argv, const char *earg);
104  void addShort(char cmd, Option *option);
105  void addLong(cstring cmd, Option *option);
106  void addCommand(string cmd, Option *option);
109  UniquePtr<SwitchOption> _help_opt, _version_opt;
110  Vector<string> _frees;
111 };
112 
113 } } //elm::option
114 
115 #define ELM_RUN(C) int main(int argc, char **argv) { return C().manage(argc, argv); }
116 
117 #endif // ELM_OPTION_MANAGER_H
elm::option::Manager::description
cstring description(void) const
Definition: Manager.h:84
elm::option::Manager::freeArgumentDescription
cstring freeArgumentDescription(void) const
Definition: Manager.h:85
elm::option::Manager::run
virtual void run(void)
Definition: option_Manager.cpp:343
elm::ListMap
Definition: ListMap.h:29
elm::UniquePtr
Definition: UniquePtr.h:27
elm::option::Manager::Make
Definition: Manager.h:41
elm::option::ValueOption::Make
Definition: ValueOption.h:72
elm::option::Manager::getFreeArgumentDescription
cstring getFreeArgumentDescription(void) const
Definition: Manager.h:93
elm::Version::ZERO
static const Version ZERO
Definition: Version.h:34
elm::option::Manager::removeOption
void removeOption(Option *option)
Definition: option_Manager.cpp:292
elm::option::Manager::getAuthor
cstring getAuthor(void) const
Definition: Manager.h:90
elm::option::Manager::freeArguments
const Vector< string > & freeArguments() const
Definition: Manager.h:99
elm::option::Manager::make_value
ValueOption< T >::Make make_value(void)
Definition: Manager.h:65
elm::option::Manager::Make::copyright
Make & copyright(cstring s)
Definition: Manager.h:47
elm::option::Manager
Definition: Manager.h:37
elm::option::Manager::make_switch
SwitchOption::Make make_switch(void)
Definition: Manager.h:64
elm::option::Manager::displayVersion
virtual void displayVersion(void)
Definition: option_Manager.cpp:433
elm::CString
Definition: CString.h:17
elm::Version
Definition: Version.h:32
elm::option::Option::Make
Definition: Option.h:52
elm::option::Manager::Make::help
Make & help(void)
Definition: Manager.h:50
elm::option::Manager::Make::description
Make & description(cstring s)
Definition: Manager.h:48
elm::option::Manager::version
const Version & version(void) const
Definition: Manager.h:81
elm::option::Manager::Make::version
Make & version(void)
Definition: Manager.h:51
elm::option::Manager::argv_t
const typedef char *const * argv_t
Definition: Manager.h:67
elm::option::Manager::Make::Make
Make(void)
Definition: Manager.h:44
elm::option::Manager::Make::author
Make & author(cstring s)
Definition: Manager.h:46
elm
Definition: adapter.h:26
elm::option::Manager::process
virtual void process(String arg)
Definition: option_Manager.cpp:273
elm::option::Manager::copyright
cstring copyright(void) const
Definition: Manager.h:83
elm::Vector< string >
elm::option::Option
Definition: Option.h:49
elm::option::Manager::info
Make info
Definition: Manager.h:96
elm::option::Manager::~Manager
virtual ~Manager(void)
Definition: option_Manager.cpp:206
elm::option::Manager::getCopyright
cstring getCopyright(void) const
Definition: Manager.h:91
elm::option::Manager::Make::Make
Make(cstring program, Version version=Version::ZERO)
Definition: Manager.h:45
elm::option::Manager::getProgram
cstring getProgram(void) const
Definition: Manager.h:88
elm::String
Definition: String.h:30
elm::option::Manager::Manager
Manager(void)
Definition: option_Manager.cpp:183
elm::option::Manager::program
cstring program(void) const
Definition: Manager.h:80
elm::option::Manager::author
cstring author(void) const
Definition: Manager.h:82
elm::option::Manager::Make::free_argument
Make & free_argument(cstring s)
Definition: Manager.h:49
elm::option::Manager::displayHelp
virtual void displayHelp(void)
Definition: option_Manager.cpp:441
elm::option::Manager::getDescription
cstring getDescription(void) const
Definition: Manager.h:92
elm::option::Manager::manage
int manage(int artc, argv_t argv)
Definition: option_Manager.cpp:364
elm::option::Manager::parse
void parse(int argc, argv_t argv)
Definition: option_Manager.cpp:388
elm::option::Manager::getVersion
const Version & getVersion(void) const
Definition: Manager.h:89
elm::option::Manager::addOption
void addOption(Option *option)
Definition: option_Manager.cpp:282