1 #ifndef __STAN__IO__CMD_LINE_HPP__
2 #define __STAN__IO__CMD_LINE_HPP__
28 const std::string& option =
"",
29 unsigned int width = 20) {
32 int padding = width - option.size();
37 for (
int i = 0; i < padding; ++i)
49 const std::string& key_val,
50 const std::string& msg,
51 const std::string& note =
"") {
56 if (note.size() > 0) {
58 *o <<
" (" << note <<
")"
73 const std::string& key,
75 const std::string& msg,
76 const std::string& note =
"") {
79 if (value_type.size() > 0)
80 ss <<
"=<" << value_type <<
">";
114 std::map<std::string,std::string> key_val_;
115 std::set<std::string> flag_;
116 std::vector<std::string> bare_;
117 void parse_arg(
const std::string& s) {
124 for (
size_t i = 2; i < s.size(); ++i) {
126 key_val_[s.substr(2,i - 2)] = s.substr(i + 1,s.size() - i - 1);
130 flag_.insert(s.substr(2,s.size()));
142 for (
int i = 1; i < argc; ++i)
164 return key_val_.find(key) != key_val_.end();
186 template <
typename T>
187 bool val(
const std::string& key, T& x)
const {
190 std::stringstream s(key_val_.find(key)->second);
202 return flag_.find(flag) != flag_.end();
227 template <
typename T>
228 bool bare(
size_t n, T& x)
const {
229 if (n >= bare_.size())
231 std::stringstream s(bare_[n]);
242 void print(std::ostream& out)
const {
243 out <<
"COMMAND=" << cmd_ <<
'\n';
244 size_t flag_count = 0;
245 for (std::set<std::string>::const_iterator it = flag_.begin();
248 out <<
"FLAG " << flag_count <<
"=" << (*it) <<
'\n';
251 size_t key_val_count = 0;
252 for (std::map<std::string,std::string>::const_iterator it = key_val_.begin();
253 it != key_val_.end();
255 out <<
"KEY " << key_val_count <<
"=" << (*it).first;
256 out <<
" VAL " << key_val_count <<
"=" << (*it).second <<
'\n';
259 size_t bare_count = 0;
260 for (
size_t i = 0; i < bare_.size(); ++i) {
261 out <<
"BARE ARG " << bare_count <<
"=" << bare_[i] <<
'\n';