1 | #ifndef _theplu_yat_utility_commandline_ |
---|
2 | #define _theplu_yat_utility_commandline_ |
---|
3 | |
---|
4 | //$Id: CommandLine.h 981 2007-10-22 04:18:40Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2007 Peter Johansson |
---|
8 | |
---|
9 | This file is part of the yat library, http://trac.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 "TypeInfo.h" |
---|
28 | |
---|
29 | #include <cctype> |
---|
30 | #include <map> |
---|
31 | #include <iostream> |
---|
32 | #include <sstream> |
---|
33 | #include <stdexcept> |
---|
34 | #include <string> |
---|
35 | #include <typeinfo> |
---|
36 | #include <utility> |
---|
37 | #include <vector> |
---|
38 | |
---|
39 | namespace theplu { |
---|
40 | namespace yat { |
---|
41 | namespace utility { |
---|
42 | |
---|
43 | class Option; |
---|
44 | |
---|
45 | /** |
---|
46 | @brief Class for parsing the command line. |
---|
47 | |
---|
48 | Provides parsing and storage of command line arguments. The class |
---|
49 | is typically used by hooking a number of Option objects to |
---|
50 | CommandLine, and then call the parse() function. Here is a short |
---|
51 | example how the class may be used: |
---|
52 | |
---|
53 | \code |
---|
54 | |
---|
55 | CommandLine cmd; |
---|
56 | OptionArg<std::string> dir(cmd, "d,dir", "output directory"); |
---|
57 | OptionSwitch help(cmd, "h,help", "display this help and exit"); |
---|
58 | OptionSwitch target(cmd, "T,target", "treat DEST as a normal file", true); |
---|
59 | OptionSwitch verbose(cmd, "v,verbose", "explain what is being done"); |
---|
60 | OptionSwitch version(cmd, "version", "output version and exit"); |
---|
61 | try { |
---|
62 | cmd.parse(argc, argv); |
---|
63 | } |
---|
64 | catch (std::runtime_error e){ |
---|
65 | std::cout << e.what() << std::endl; |
---|
66 | return 1; |
---|
67 | } |
---|
68 | if (help.present()){ |
---|
69 | std::cout << "Usage: " << cmd.program_name() << " [OPTION]...\n\n" |
---|
70 | << "This is a test\n\n" << cmd << "\n" |
---|
71 | << "Report bugs to peter@example.net\n"; |
---|
72 | exit(0); |
---|
73 | } |
---|
74 | if (version.present()){ |
---|
75 | std::cout << cmd.program_name() << " 1.0\n" |
---|
76 | << "Copyright (C) 2007 Peter Johansson\n\n" |
---|
77 | << "This is free softwarel see the source for copying " |
---|
78 | << "conditions. There is NO\nwarranty; not even for " |
---|
79 | << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"; |
---|
80 | exit(0); |
---|
81 | } |
---|
82 | ... |
---|
83 | \endcode |
---|
84 | |
---|
85 | After creation a number of Option classes are hooked up to the |
---|
86 | CommandLine object in their constructors. |
---|
87 | |
---|
88 | Each parameter is associated to a one-character flag and/or a |
---|
89 | longer string flag. The longer flag expects to be preceded by |
---|
90 | '--' as e.g. '--help' for help. The shorter flag expects to be |
---|
91 | preceded by '-' as e.g. '-h', and can also be concatenated like |
---|
92 | "program -vf", which is equivalent to "program -v -f". or |
---|
93 | its sibblings for different types. By using the allow_help() |
---|
94 | function help will be displayed when flag (default is '-h' and |
---|
95 | '--help') is found in parsing. |
---|
96 | */ |
---|
97 | class CommandLine |
---|
98 | { |
---|
99 | public: |
---|
100 | /** |
---|
101 | \brief deafult constructor |
---|
102 | */ |
---|
103 | CommandLine(void); |
---|
104 | |
---|
105 | /** |
---|
106 | \brief Destructor |
---|
107 | */ |
---|
108 | virtual ~CommandLine(void); |
---|
109 | |
---|
110 | /** |
---|
111 | \brief Function to add an option. |
---|
112 | */ |
---|
113 | void add(Option&); |
---|
114 | |
---|
115 | /** |
---|
116 | \brief parse the commandline |
---|
117 | |
---|
118 | throw std::runtime_error if an error is detected. |
---|
119 | */ |
---|
120 | void parse(int argc, char* argv[]); |
---|
121 | |
---|
122 | /** |
---|
123 | @return Name of more; more specifically argv[0] is |
---|
124 | stripped so only string after the last '/' remains. |
---|
125 | */ |
---|
126 | std::string program_name(void) const; |
---|
127 | |
---|
128 | /** |
---|
129 | \return something like "Try `<program_name()> --help` for |
---|
130 | more information." |
---|
131 | */ |
---|
132 | std::string try_help(void) const; |
---|
133 | |
---|
134 | private: |
---|
135 | friend std::ostream& operator<<(std::ostream& os, const CommandLine& cl); |
---|
136 | void add_private(std::string, Option&); |
---|
137 | bool is_long_option(std::string str) const; |
---|
138 | bool is_short_option(std::string str) const; |
---|
139 | std::vector<std::string> split(std::string str, char del) const; |
---|
140 | |
---|
141 | std::vector<Option*> options_; |
---|
142 | std::map<char, Option*> short_options_; |
---|
143 | std::map<std::string, Option*> long_options_; |
---|
144 | std::string program_name_; |
---|
145 | }; |
---|
146 | |
---|
147 | /** |
---|
148 | \brief CommandLine output operator |
---|
149 | */ |
---|
150 | std::ostream& operator<<(std::ostream&, const CommandLine&); |
---|
151 | |
---|
152 | }}} // end of namespace utility, yat, and theplu |
---|
153 | |
---|
154 | #endif |
---|