965 |
11 Oct 07 |
peter |
1 |
#ifndef _theplu_yat_utility_option_ |
965 |
11 Oct 07 |
peter |
2 |
#define _theplu_yat_utility_option_ |
965 |
11 Oct 07 |
peter |
3 |
|
965 |
11 Oct 07 |
peter |
// $Id$ |
965 |
11 Oct 07 |
peter |
5 |
|
965 |
11 Oct 07 |
peter |
6 |
/* |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2007 Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2009, 2010 Peter Johansson |
965 |
11 Oct 07 |
peter |
10 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
965 |
11 Oct 07 |
peter |
12 |
|
965 |
11 Oct 07 |
peter |
The yat library is free software; you can redistribute it and/or |
965 |
11 Oct 07 |
peter |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
965 |
11 Oct 07 |
peter |
License, or (at your option) any later version. |
965 |
11 Oct 07 |
peter |
17 |
|
965 |
11 Oct 07 |
peter |
The yat library is distributed in the hope that it will be useful, |
965 |
11 Oct 07 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
965 |
11 Oct 07 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
965 |
11 Oct 07 |
peter |
General Public License for more details. |
965 |
11 Oct 07 |
peter |
22 |
|
965 |
11 Oct 07 |
peter |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
965 |
11 Oct 07 |
peter |
25 |
*/ |
965 |
11 Oct 07 |
peter |
26 |
|
965 |
11 Oct 07 |
peter |
27 |
#include <string> |
965 |
11 Oct 07 |
peter |
28 |
#include <vector> |
965 |
11 Oct 07 |
peter |
29 |
|
965 |
11 Oct 07 |
peter |
30 |
namespace theplu { |
965 |
11 Oct 07 |
peter |
31 |
namespace yat { |
965 |
11 Oct 07 |
peter |
32 |
namespace utility { |
965 |
11 Oct 07 |
peter |
33 |
|
965 |
11 Oct 07 |
peter |
34 |
class CommandLine; |
965 |
11 Oct 07 |
peter |
35 |
/// |
965 |
11 Oct 07 |
peter |
/// @brief Container of variables for an option. @see CommandLine |
965 |
11 Oct 07 |
peter |
37 |
/// |
965 |
11 Oct 07 |
peter |
38 |
class Option |
965 |
11 Oct 07 |
peter |
39 |
{ |
965 |
11 Oct 07 |
peter |
40 |
public: |
965 |
11 Oct 07 |
peter |
41 |
/** |
4200 |
19 Aug 22 |
peter |
@brief Constructor |
4200 |
19 Aug 22 |
peter |
43 |
|
1268 |
09 Apr 08 |
peter |
\param cmd Commandline to be hooked up with. |
965 |
11 Oct 07 |
peter |
@param name string such as "help" for --help, "h" for -h or |
965 |
11 Oct 07 |
peter |
"h,help" for having both short and long option name |
965 |
11 Oct 07 |
peter |
@param desc string used in help display |
965 |
11 Oct 07 |
peter |
48 |
*/ |
4200 |
19 Aug 22 |
peter |
49 |
Option(CommandLine& cmd, std::string name, std::string desc); |
965 |
11 Oct 07 |
peter |
50 |
|
965 |
11 Oct 07 |
peter |
51 |
/** |
965 |
11 Oct 07 |
peter |
@brief destructor |
965 |
11 Oct 07 |
peter |
53 |
*/ |
965 |
11 Oct 07 |
peter |
54 |
virtual ~Option(void); |
4200 |
19 Aug 22 |
peter |
55 |
|
965 |
11 Oct 07 |
peter |
56 |
/** |
965 |
11 Oct 07 |
peter |
@return description |
965 |
11 Oct 07 |
peter |
58 |
*/ |
965 |
11 Oct 07 |
peter |
59 |
std::string description(void) const; |
965 |
11 Oct 07 |
peter |
60 |
|
965 |
11 Oct 07 |
peter |
61 |
/** |
2348 |
13 Nov 10 |
peter |
\brief set description |
2348 |
13 Nov 10 |
peter |
63 |
|
2348 |
13 Nov 10 |
peter |
\since New in yat 0.7 |
2348 |
13 Nov 10 |
peter |
65 |
*/ |
2348 |
13 Nov 10 |
peter |
66 |
void description(const std::string& description); |
2348 |
13 Nov 10 |
peter |
67 |
|
2348 |
13 Nov 10 |
peter |
68 |
/** |
965 |
11 Oct 07 |
peter |
\return long name e.g. 'help' for --help option. |
965 |
11 Oct 07 |
peter |
70 |
*/ |
965 |
11 Oct 07 |
peter |
71 |
std::string long_name(void) const; |
965 |
11 Oct 07 |
peter |
72 |
|
965 |
11 Oct 07 |
peter |
73 |
/** |
965 |
11 Oct 07 |
peter |
\brief parsing the commandline |
965 |
11 Oct 07 |
peter |
75 |
*/ |
4200 |
19 Aug 22 |
peter |
76 |
void parse(std::vector<std::string>::iterator&, |
1465 |
02 Sep 08 |
peter |
77 |
const std::vector<std::string>::iterator&); |
965 |
11 Oct 07 |
peter |
78 |
|
965 |
11 Oct 07 |
peter |
79 |
/** |
965 |
11 Oct 07 |
peter |
@brief Get if option was found in cmd. |
4200 |
19 Aug 22 |
peter |
81 |
|
965 |
11 Oct 07 |
peter |
@return true if option has been detected in parsing |
965 |
11 Oct 07 |
peter |
83 |
*/ |
965 |
11 Oct 07 |
peter |
84 |
bool present(void) const; |
965 |
11 Oct 07 |
peter |
85 |
|
965 |
11 Oct 07 |
peter |
86 |
/** |
965 |
11 Oct 07 |
peter |
\brief print help output |
1437 |
25 Aug 08 |
peter |
88 |
|
1437 |
25 Aug 08 |
peter |
This function calls the four virtual private functions print1, |
1437 |
25 Aug 08 |
peter |
print2, print3, and print4. This allows an inherited class to |
1437 |
25 Aug 08 |
peter |
implement one (or several) of these functions and keep the |
1437 |
25 Aug 08 |
peter |
default output of the others. The default behavior is that: |
1437 |
25 Aug 08 |
peter |
93 |
|
1437 |
25 Aug 08 |
peter |
- print1 prints the short name, '-h', as short_name(void) const |
4200 |
19 Aug 22 |
peter |
- print2 prints the long name, '--help', as long_name(void) const |
4200 |
19 Aug 22 |
peter |
- print3 is empty |
1437 |
25 Aug 08 |
peter |
- print4 prints the description as description(void) const. |
965 |
11 Oct 07 |
peter |
98 |
*/ |
965 |
11 Oct 07 |
peter |
99 |
std::string print(void); |
965 |
11 Oct 07 |
peter |
100 |
|
965 |
11 Oct 07 |
peter |
101 |
/** |
965 |
11 Oct 07 |
peter |
\brief sets present to false |
965 |
11 Oct 07 |
peter |
103 |
*/ |
965 |
11 Oct 07 |
peter |
104 |
void reset(void); |
965 |
11 Oct 07 |
peter |
105 |
|
965 |
11 Oct 07 |
peter |
106 |
/** |
965 |
11 Oct 07 |
peter |
\return short name e.g. 'h' for -h option. |
965 |
11 Oct 07 |
peter |
108 |
*/ |
965 |
11 Oct 07 |
peter |
109 |
char short_name(void) const; |
965 |
11 Oct 07 |
peter |
110 |
|
965 |
11 Oct 07 |
peter |
111 |
/** |
965 |
11 Oct 07 |
peter |
\brief Validate the Option |
965 |
11 Oct 07 |
peter |
113 |
|
2046 |
02 Sep 09 |
peter |
This function is called by CommandLine::parse() after all |
2046 |
02 Sep 09 |
peter |
options have been detected and parsed (see Option::parse()). |
965 |
11 Oct 07 |
peter |
116 |
*/ |
965 |
11 Oct 07 |
peter |
117 |
void validate(void); |
965 |
11 Oct 07 |
peter |
118 |
|
980 |
22 Oct 07 |
peter |
119 |
protected: |
980 |
22 Oct 07 |
peter |
120 |
/** |
4200 |
19 Aug 22 |
peter |
\return const reference to CommandLine Option belongs to. |
980 |
22 Oct 07 |
peter |
122 |
*/ |
980 |
22 Oct 07 |
peter |
123 |
const CommandLine& cmd(void) const; |
980 |
22 Oct 07 |
peter |
124 |
|
965 |
11 Oct 07 |
peter |
125 |
private: |
4200 |
19 Aug 22 |
peter |
126 |
virtual void do_parse(std::vector<std::string>::iterator&, |
1465 |
02 Sep 08 |
peter |
127 |
const std::vector<std::string>::iterator&)=0; |
965 |
11 Oct 07 |
peter |
128 |
|
965 |
11 Oct 07 |
peter |
129 |
/** |
965 |
11 Oct 07 |
peter |
130 |
*/ |
965 |
11 Oct 07 |
peter |
131 |
virtual std::string print1(void) const; |
965 |
11 Oct 07 |
peter |
132 |
|
965 |
11 Oct 07 |
peter |
133 |
/** |
965 |
11 Oct 07 |
peter |
134 |
*/ |
965 |
11 Oct 07 |
peter |
135 |
virtual std::string print2(void) const; |
965 |
11 Oct 07 |
peter |
136 |
|
965 |
11 Oct 07 |
peter |
137 |
/** |
965 |
11 Oct 07 |
peter |
138 |
*/ |
965 |
11 Oct 07 |
peter |
139 |
virtual std::string print3(void) const; |
965 |
11 Oct 07 |
peter |
140 |
|
965 |
11 Oct 07 |
peter |
141 |
/** |
965 |
11 Oct 07 |
peter |
142 |
*/ |
965 |
11 Oct 07 |
peter |
143 |
virtual std::string print4(void) const; |
965 |
11 Oct 07 |
peter |
144 |
|
965 |
11 Oct 07 |
peter |
145 |
/** |
965 |
11 Oct 07 |
peter |
146 |
*/ |
965 |
11 Oct 07 |
peter |
147 |
virtual void do_validate(void) const=0; |
965 |
11 Oct 07 |
peter |
148 |
|
965 |
11 Oct 07 |
peter |
149 |
|
980 |
22 Oct 07 |
peter |
150 |
const CommandLine& cmd_; |
965 |
11 Oct 07 |
peter |
151 |
std::string description_; |
965 |
11 Oct 07 |
peter |
152 |
std::string long_name_; |
965 |
11 Oct 07 |
peter |
153 |
bool present_; |
965 |
11 Oct 07 |
peter |
154 |
char short_name_; |
965 |
11 Oct 07 |
peter |
155 |
|
965 |
11 Oct 07 |
peter |
// copy not allowed |
965 |
11 Oct 07 |
peter |
157 |
Option(const Option&); |
965 |
11 Oct 07 |
peter |
158 |
Option& operator=(const Option&); |
965 |
11 Oct 07 |
peter |
159 |
}; |
965 |
11 Oct 07 |
peter |
160 |
|
965 |
11 Oct 07 |
peter |
161 |
}}} // of namespace utility, yat, and theplu |
965 |
11 Oct 07 |
peter |
162 |
|
965 |
11 Oct 07 |
peter |
163 |
#endif |