1 #ifndef __STAN__IO__CMD_LINE_HPP__
2 #define __STAN__IO__CMD_LINE_HPP__
30 const std::string& option =
"",
31 unsigned int width = 20) {
34 int padding = width - option.size();
39 for (
int i = 0; i < padding; ++i)
51 const std::string& key_val,
52 const std::string& msg,
53 const std::string& note =
"") {
58 if (note.size() > 0) {
60 *o <<
" (" << note <<
")"
75 const std::string& key,
77 const std::string& msg,
78 const std::string& note =
"") {
81 if (value_type.size() > 0)
82 ss <<
"=<" << value_type <<
">";
116 std::map<std::string,std::string> key_val_;
117 std::set<std::string> flag_;
118 std::vector<std::string> bare_;
119 void parse_arg(
const std::string& s) {
126 for (
size_t i = 2; i < s.size(); ++i) {
128 key_val_[s.substr(2,i - 2)] = s.substr(i + 1,s.size() - i - 1);
132 flag_.insert(s.substr(2,s.size()));
144 for (
int i = 1; i < argc; ++i)
166 return key_val_.find(key) != key_val_.end();
187 template <
typename T>
188 bool val(
const std::string& key, T& x)
const {
191 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';