1 | #ifndef _theplu_yat_utility_commandline_ |
---|
2 | #define _theplu_yat_utility_commandline_ |
---|
3 | |
---|
4 | //$Id: CommandLine.h 687 2006-10-16 23:51:10Z jari $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2006 Peter Johansson |
---|
8 | |
---|
9 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 2 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | 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 | |
---|
27 | #include "Option.h" |
---|
28 | |
---|
29 | #include <cctype> |
---|
30 | #include <list> |
---|
31 | #include <map> |
---|
32 | #include <string> |
---|
33 | #include <utility> |
---|
34 | #include <vector> |
---|
35 | |
---|
36 | namespace theplu { |
---|
37 | namespace yat { |
---|
38 | namespace utility { |
---|
39 | |
---|
40 | /** |
---|
41 | @brief Class for parsing the command line. |
---|
42 | |
---|
43 | Provides parsing and storage of command line arguments (argc, |
---|
44 | argv). To use this class first add a set of valid parameters |
---|
45 | using the add_parameter() function. Each parameter is associated |
---|
46 | to a one-character flag and/or a longer string flag. The longer |
---|
47 | flag expects to be preceded by '--' as e.g. '--help' for |
---|
48 | help. The shorter flag expects to be preceded by '-' as |
---|
49 | e.g. '-h', and can also be concatenated as e.g. "program -vf" is |
---|
50 | equivalent to "program -v -f". Associated to each parameter is an |
---|
51 | attribute telling what type of argument is expected for the |
---|
52 | parameter. Several types of arguments are supported: no argument, |
---|
53 | string argument, int argument, double argument. The argument |
---|
54 | value for all found parameters are set during parsing, which |
---|
55 | supports both gnu-style "--support-gnu=value", POSIX-like |
---|
56 | "--support-posix value", as well as shorter "-s value". The |
---|
57 | argument of an parameter is retrived by the value() function or |
---|
58 | its sibblings for different types. By using the set_help() |
---|
59 | function help will be displayed when flag (default is '-h' and |
---|
60 | '--help') is found in parsing. |
---|
61 | |
---|
62 | Here is a small code example: |
---|
63 | @code |
---|
64 | #include "yat/utility/CommandLine.h" |
---|
65 | #include <iostream> |
---|
66 | int main(const int argc,const char* argv[]) |
---|
67 | { |
---|
68 | theplu::utility::CommandLine cmd; |
---|
69 | cmd.set_general_description("This is just an example program"); |
---|
70 | cmd.add_parameter('n', utility::Option::int_arg, |
---|
71 | "example of parameter taking an integer"); |
---|
72 | cmd.add_parameter('t', "target", utility::Option::string_arg); |
---|
73 | cmd.add_parameter("version",utility::Option::no_arg, |
---|
74 | "output version infomation and exit"); |
---|
75 | cmd.set_help(); |
---|
76 | cmd.parse(argc, argv); |
---|
77 | if (cmd.present("version")) |
---|
78 | std::cout << "example 1.0" << std::endl; |
---|
79 | if (cmd.present("target")) |
---|
80 | std::cout << "using target: " << target << std::endl; |
---|
81 | if (cmd.present('n')){ |
---|
82 | int n=cmd.value('n'); |
---|
83 | for (size_t i=0; i<n; ++i) |
---|
84 | std::cout << "Hello World\n"; |
---|
85 | std::cout << endl; |
---|
86 | } |
---|
87 | return 0; |
---|
88 | } |
---|
89 | @endcode |
---|
90 | |
---|
91 | @see Option |
---|
92 | |
---|
93 | **/ |
---|
94 | class CommandLine |
---|
95 | { |
---|
96 | public: |
---|
97 | |
---|
98 | /// |
---|
99 | /// @brief deafult constructor |
---|
100 | /// |
---|
101 | CommandLine(void); |
---|
102 | |
---|
103 | /// |
---|
104 | /// @brief Destructor |
---|
105 | /// |
---|
106 | virtual ~CommandLine(void); |
---|
107 | |
---|
108 | /// |
---|
109 | /// @brief Function to add a parameter. |
---|
110 | /// |
---|
111 | /// @param long_name string key such as "help" for --help flag |
---|
112 | /// @param arg telling what kind argument this option expects |
---|
113 | /// @param description string used in help display |
---|
114 | /// |
---|
115 | void add_parameter(const std::string& long_name, |
---|
116 | Option::argument_type arg = Option::no_arg, |
---|
117 | const std::string& description = std::string()); |
---|
118 | |
---|
119 | /// |
---|
120 | /// @brief Function to add a parameter. |
---|
121 | /// |
---|
122 | /// @param short_name one character key such as 'h' for -h flag |
---|
123 | /// @param arg telling what kind argument this option expects |
---|
124 | /// @param description string used in help display |
---|
125 | /// |
---|
126 | void add_parameter(const char short_name, |
---|
127 | Option::argument_type arg = Option::no_arg, |
---|
128 | const std::string& description = std::string()); |
---|
129 | |
---|
130 | /// |
---|
131 | /// @brief Function to add a parameter. |
---|
132 | /// |
---|
133 | /// @param short_name one character key such as 'h' for -h flag |
---|
134 | /// @param long_name string key such as "help" for --help flag |
---|
135 | /// @param arg telling what kind argument this option expects |
---|
136 | /// @param description string used in help display |
---|
137 | /// |
---|
138 | inline void add_parameter(const char short_name, |
---|
139 | const std::string& long_name, |
---|
140 | Option::argument_type arg = Option::no_arg, |
---|
141 | const std::string& description = std::string()) |
---|
142 | { add(short_name, long_name, arg, description); } |
---|
143 | |
---|
144 | /// |
---|
145 | /// @return vector of arguments not associated to a specific parameter |
---|
146 | /// |
---|
147 | const std::vector<std::string>& arguments(void) const; |
---|
148 | |
---|
149 | /// |
---|
150 | /// If more than maximal number of arguments is found during |
---|
151 | /// parsing an error message is displayed followed by exit. |
---|
152 | /// |
---|
153 | /// @return maximal number of arguments allowed. |
---|
154 | /// |
---|
155 | inline u_int& max_argument(void) { return max_argument_; } |
---|
156 | |
---|
157 | /// |
---|
158 | /// If less than minimal number of arguments is found during |
---|
159 | /// parsing an error message is displayed followed by exit. |
---|
160 | /// |
---|
161 | /// @return minimal number of arguments allowed. |
---|
162 | /// |
---|
163 | inline u_int& min_argument(void) { return min_argument_; } |
---|
164 | |
---|
165 | /// |
---|
166 | /// @brief parse the commandline |
---|
167 | /// |
---|
168 | void parse(int argc, const char* argv[]); |
---|
169 | |
---|
170 | /// |
---|
171 | /// @return true if @a parameter has been detected in parsing. |
---|
172 | /// |
---|
173 | bool present(const std::string& parameter) const; |
---|
174 | |
---|
175 | /// |
---|
176 | /// @brief allow help. |
---|
177 | /// |
---|
178 | void set_help(char shortname = 'h', |
---|
179 | const std::string& longname = "help", |
---|
180 | const std::string& descr = "display this help and exit"); |
---|
181 | |
---|
182 | /// |
---|
183 | /// The @a description will be included in help display giving a |
---|
184 | /// general explanation what program is doing. |
---|
185 | /// |
---|
186 | inline void set_general_description(const std::string& description) |
---|
187 | { general_description_=description; } |
---|
188 | |
---|
189 | /// |
---|
190 | /// @note Using function for @a parameter not added previously |
---|
191 | /// will cause an error message and exit. |
---|
192 | /// |
---|
193 | /// @return argument value for @a parameter |
---|
194 | /// |
---|
195 | std::string value(const std::string& parameter) const; |
---|
196 | |
---|
197 | /// |
---|
198 | /// If the value for @a parameter is not a valid double an error |
---|
199 | /// message will be displayed followed by an exit. |
---|
200 | /// |
---|
201 | /// @note Using function for @a parameter not added previously |
---|
202 | /// will cause an error message and exit. |
---|
203 | /// |
---|
204 | /// @return argument value for @a parameter |
---|
205 | /// |
---|
206 | double value_double(const std::string& parameter) const; |
---|
207 | |
---|
208 | /// |
---|
209 | /// If the value for @a parameter is not a valid double an error |
---|
210 | /// message will be displayed followed by an exit. |
---|
211 | /// |
---|
212 | /// @note Using function for @a parameter not added previously |
---|
213 | /// will cause an error message and exit. |
---|
214 | /// |
---|
215 | /// @return argument value for @a parameter |
---|
216 | /// |
---|
217 | int value_int(const std::string& parameter) const; |
---|
218 | |
---|
219 | /// |
---|
220 | /// Function to display the help message. |
---|
221 | /// |
---|
222 | void usage(void) const; |
---|
223 | |
---|
224 | private: |
---|
225 | Option* add(char short_name, |
---|
226 | const std::string& long_name, |
---|
227 | Option::argument_type arg, |
---|
228 | const std::string& describtion); |
---|
229 | |
---|
230 | inline bool is_long_option(const std::string& str) |
---|
231 | { return (str.size()>3 && str[0]=='-' && str[1]=='-'); } |
---|
232 | |
---|
233 | inline bool is_short_option(const std::string& str) |
---|
234 | { return (str.size()==2 && str[0]=='-' && isalpha(str[1])); } |
---|
235 | |
---|
236 | void print_try_help(void) const; |
---|
237 | std::string split(std::string&, char) const; |
---|
238 | bool update(const std::string& key, const std::string& value); |
---|
239 | |
---|
240 | |
---|
241 | typedef std::map<std::string, Option*> key2option; |
---|
242 | |
---|
243 | std::string app_name_; |
---|
244 | const Option* help_option_; |
---|
245 | std::string general_description_; |
---|
246 | u_int max_argument_; |
---|
247 | u_int min_argument_; |
---|
248 | key2option param_; |
---|
249 | std::list<Option*> options_; |
---|
250 | std::vector<std::string> arguments_; |
---|
251 | }; |
---|
252 | |
---|
253 | |
---|
254 | }}} // end of namespace utility, yat, and theplu |
---|
255 | |
---|
256 | #endif |
---|