1 | // $Id: CommandLine.cc 1437 2008-08-25 17:55:00Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
5 | Copyright (C) 2008 Peter Johansson |
---|
6 | |
---|
7 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
8 | |
---|
9 | The yat library is free software; you can redistribute it and/or |
---|
10 | modify it under the terms of the GNU General Public License as |
---|
11 | published by the Free Software Foundation; either version 2 of the |
---|
12 | License, or (at your option) any later version. |
---|
13 | |
---|
14 | The yat library is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with this program; if not, write to the Free Software |
---|
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
22 | 02111-1307, USA. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "CommandLine.h" |
---|
26 | |
---|
27 | #include "ColumnStream.h" |
---|
28 | #include "Exception.h" |
---|
29 | #include "Option.h" |
---|
30 | #include "OptionSwitch.h" |
---|
31 | #include "utility.h" |
---|
32 | |
---|
33 | #include <algorithm> |
---|
34 | #include <functional> |
---|
35 | #include <fstream> |
---|
36 | #include <iostream> |
---|
37 | #include <sstream> |
---|
38 | #include <stdexcept> |
---|
39 | #include <string> |
---|
40 | #include <vector> |
---|
41 | |
---|
42 | namespace theplu { |
---|
43 | namespace yat { |
---|
44 | namespace utility { |
---|
45 | |
---|
46 | CommandLine::CommandLine(std::string str) |
---|
47 | : description_(str) |
---|
48 | {} |
---|
49 | |
---|
50 | |
---|
51 | CommandLine::~CommandLine(void) |
---|
52 | { |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | void CommandLine::add(Option& option) |
---|
57 | { |
---|
58 | if (option.long_name().size()) { |
---|
59 | if (long_options_.find(option.long_name())!=long_options_.end()) { |
---|
60 | std::stringstream ss; |
---|
61 | ss << "yat::utility::Commandline: two options with long_name: " |
---|
62 | << option.long_name(); |
---|
63 | throw std::runtime_error(ss.str()); |
---|
64 | } |
---|
65 | long_options_[option.long_name()] = &option; |
---|
66 | } |
---|
67 | if (option.short_name()) { |
---|
68 | if (short_options_.find(option.short_name())!=short_options_.end()) { |
---|
69 | std::stringstream ss; |
---|
70 | ss << "yat::utility::Commandline: two options with short_name: " |
---|
71 | << option.short_name(); |
---|
72 | throw std::runtime_error(ss.str()); |
---|
73 | } |
---|
74 | short_options_[option.short_name()] = &option; |
---|
75 | } |
---|
76 | if (option.long_name().size() || option.short_name()) |
---|
77 | options_.push_back(&option); |
---|
78 | // allow `no-switch' for option `switch' |
---|
79 | OptionSwitch* o = dynamic_cast<OptionSwitch*>(&option); |
---|
80 | std::string no_name = std::string("no-")+option.long_name(); |
---|
81 | if (option.long_name().size() && o && |
---|
82 | !( o->long_name().size()>2 && o->long_name().substr(0,3)=="no-") && |
---|
83 | long_options_.find(no_name)==long_options_.end()) |
---|
84 | long_options_[no_name] = &option; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | bool CommandLine::is_long_option(std::string str) const |
---|
89 | { |
---|
90 | return (str.size()>2 && str[0]=='-' && str[1]=='-'); |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | bool CommandLine::is_short_option(std::string str) const |
---|
95 | { |
---|
96 | return (str.size()>=2 && str[0]=='-' && str[1]!='-'); |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | void CommandLine::parse(int argc, char* argv[]) |
---|
101 | { |
---|
102 | using namespace std; |
---|
103 | // just in case it is not pristine |
---|
104 | for_each(options_.begin(), options_.end(),std::mem_fun(&Option::reset)); |
---|
105 | |
---|
106 | std::vector<std::string> arguments; |
---|
107 | arguments.reserve(argc); |
---|
108 | for (int i=0; i<argc; ++i) |
---|
109 | arguments.push_back(argv[i]); |
---|
110 | std::vector<std::string>::iterator arg(arguments.begin()); |
---|
111 | stringstream ss(*arg++); |
---|
112 | // keeping string after last / |
---|
113 | while (getline(ss, program_name_,'/')) {} |
---|
114 | |
---|
115 | try { |
---|
116 | for (; arg!=arguments.end(); ++arg) { |
---|
117 | if (is_long_option(*arg)) { |
---|
118 | std::string key(arg->substr(2)); |
---|
119 | std::stringstream ss(key); |
---|
120 | getline(ss, key, '='); |
---|
121 | std::string value; |
---|
122 | getline(ss, value, '\0'); |
---|
123 | if (!value.empty()){ |
---|
124 | *arg = value; |
---|
125 | *(--arg) = std::string("--")+key; |
---|
126 | } |
---|
127 | else |
---|
128 | *arg = key; |
---|
129 | std::map<std::string, Option*>::const_iterator |
---|
130 | iter(long_options_.find(key)); |
---|
131 | if (iter==long_options_.end()) { |
---|
132 | std::stringstream ss; |
---|
133 | ss << ": unrecognized option `" << key << "'\n" |
---|
134 | << try_help(); |
---|
135 | throw cmd_error(ss.str()); |
---|
136 | } |
---|
137 | else |
---|
138 | iter->second->parse(arg, arguments.end()); |
---|
139 | } |
---|
140 | |
---|
141 | if (is_short_option(*arg)) { |
---|
142 | for (size_t i=1; i<arg->size(); ++i){ |
---|
143 | std::map<char, Option*>::const_iterator |
---|
144 | iter(short_options_.find((*arg)[i])); |
---|
145 | if (iter==short_options_.end()) { |
---|
146 | std::stringstream ss; |
---|
147 | ss << ": invalid option -- " << (*arg)[i] << "\n" |
---|
148 | << try_help() << "\n"; |
---|
149 | throw cmd_error(ss.str()); |
---|
150 | } |
---|
151 | else |
---|
152 | iter->second->parse(arg, arguments.end()); |
---|
153 | } |
---|
154 | } |
---|
155 | } |
---|
156 | for_each(options_.begin(),options_.end(), |
---|
157 | std::mem_fun(&Option::validate)); |
---|
158 | } |
---|
159 | catch (cmd_error& e){ |
---|
160 | std::stringstream ss; |
---|
161 | ss << program_name_ << ": " << e.what(); |
---|
162 | throw cmd_error(ss.str()); |
---|
163 | } |
---|
164 | |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | std::string CommandLine::program_name(void) const |
---|
169 | { |
---|
170 | return program_name_; |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | std::vector<std::string> CommandLine::split(std::string str, char del) const |
---|
175 | { |
---|
176 | std::vector<std::string> vec; |
---|
177 | std::stringstream ss(str); |
---|
178 | while (std::getline(ss, str, del)){ |
---|
179 | vec.push_back(str); |
---|
180 | } |
---|
181 | return vec; |
---|
182 | } |
---|
183 | |
---|
184 | std::string CommandLine::try_help(void) const |
---|
185 | { |
---|
186 | return std::string("Try `"+program_name()+" --help' for more information."); |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | std::ostream& operator<<(std::ostream& os, const CommandLine& cmd) |
---|
191 | { |
---|
192 | os << cmd.description_ << "\n"; |
---|
193 | ColumnStream cs2(os, 2); |
---|
194 | std::string::size_type width = 0; |
---|
195 | for (std::vector<Option*>::const_iterator i(cmd.options_.begin()); |
---|
196 | i!=cmd.options_.end();++i) { |
---|
197 | std::stringstream ss((*i)->print()); |
---|
198 | std::string str; |
---|
199 | getline(ss, str, '\t'); |
---|
200 | width = std::max(width, str.size()+3); |
---|
201 | } |
---|
202 | cs2.width(0)=width; |
---|
203 | cs2.width(1)=76-width; |
---|
204 | cs2.margin(0)=2; |
---|
205 | |
---|
206 | for (std::vector<Option*>::const_iterator i(cmd.options_.begin()); |
---|
207 | i!=cmd.options_.end();++i) |
---|
208 | cs2 << (*i)->print() << "\n"; |
---|
209 | |
---|
210 | return os; |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | }}} // of namespace utility, yat, and theplu |
---|