1 | #ifndef _theplu_utility_option_ |
---|
2 | #define _theplu_utility_option_ |
---|
3 | |
---|
4 | // $Id: Option.h 601 2006-08-29 01:45:59Z peter $ |
---|
5 | /* |
---|
6 | Copyright (C) 2006 Peter Johansson |
---|
7 | |
---|
8 | This file is part of the thep c++ tools library, |
---|
9 | http://lev.thep.lu.se/trac/c++_tools |
---|
10 | |
---|
11 | The c++ tools library is free software; you can redistribute it |
---|
12 | and/or modify it under the terms of the GNU General Public License |
---|
13 | as published by the Free Software Foundation; either version 2 of |
---|
14 | the License, or (at your option) any later version. |
---|
15 | |
---|
16 | The c++ tools library is distributed in the hope that it will be |
---|
17 | useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
---|
18 | of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with this program; if not, write to the Free Software |
---|
23 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
24 | 02111-1307, USA. |
---|
25 | */ |
---|
26 | #include <string> |
---|
27 | |
---|
28 | |
---|
29 | namespace theplu { |
---|
30 | namespace utility { |
---|
31 | |
---|
32 | /// |
---|
33 | /// @brief Container of variables for an option. @see CommandLine |
---|
34 | /// |
---|
35 | class Option |
---|
36 | { |
---|
37 | public: |
---|
38 | |
---|
39 | /// |
---|
40 | /// different types of arguments to an option |
---|
41 | /// |
---|
42 | enum argument_type { |
---|
43 | no_arg, string_arg, int_arg, double_arg |
---|
44 | }; |
---|
45 | |
---|
46 | |
---|
47 | /// |
---|
48 | /// @brief Constructor |
---|
49 | /// |
---|
50 | /// @param short_name one character key such as 'h' for -h flag |
---|
51 | /// @param long_name string key such as "help" for --help flag |
---|
52 | /// @param telling what kind argument this option expects |
---|
53 | /// @param desc string used in help display |
---|
54 | /// |
---|
55 | Option(char short_name, std::string long_name, |
---|
56 | argument_type arg, std::string desc); |
---|
57 | |
---|
58 | |
---|
59 | /// |
---|
60 | /// @return argument type for option |
---|
61 | /// |
---|
62 | inline const argument_type& arg_type(void) const { return arg_type_; } |
---|
63 | |
---|
64 | /// |
---|
65 | /// @return long name unless long name is empty in which case the |
---|
66 | /// short one character name is returned. |
---|
67 | /// |
---|
68 | inline std::string name(void) const |
---|
69 | { return !long_name_.empty() ? long_name_ : std::string(&short_name_) ; } |
---|
70 | |
---|
71 | /// |
---|
72 | /// @return short name |
---|
73 | /// |
---|
74 | inline char short_name(void) const { return short_name_; } |
---|
75 | |
---|
76 | /// |
---|
77 | /// @return long name |
---|
78 | /// |
---|
79 | inline const std::string& long_name(void) const { return long_name_; } |
---|
80 | |
---|
81 | /// |
---|
82 | /// sends output to std::cerr of type |
---|
83 | /// |
---|
84 | /// -v, --verbose explain what is going on |
---|
85 | /// |
---|
86 | void print(void) const; |
---|
87 | |
---|
88 | /// |
---|
89 | /// @return true if option has been detected in parsing |
---|
90 | /// |
---|
91 | inline bool& present(void) { return present_; } |
---|
92 | |
---|
93 | /// |
---|
94 | /// @return argument value |
---|
95 | /// |
---|
96 | inline std::string& value(void) { return value_; } |
---|
97 | |
---|
98 | |
---|
99 | private: |
---|
100 | argument_type arg_type_; |
---|
101 | std::string long_name_; |
---|
102 | std::string mess_; |
---|
103 | char short_name_; |
---|
104 | bool present_; |
---|
105 | std::string value_; |
---|
106 | }; |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | } // end of namespace utility |
---|
112 | } // end of namespace theplu |
---|
113 | |
---|
114 | #endif |
---|