Stan  1.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
cmd_line.hpp
Go to the documentation of this file.
1 #ifndef __STAN__IO__CMD_LINE_HPP__
2 #define __STAN__IO__CMD_LINE_HPP__
3 
4 #include <cstddef>
5 #include <map>
6 #include <iostream>
7 #include <ostream>
8 #include <set>
9 #include <string>
10 #include <sstream>
11 #include <vector>
12 
13 namespace stan {
14 
15  namespace io {
16 
29  void pad_help_option(std::ostream* o,
30  const std::string& option = "",
31  unsigned int width = 20) {
32  if (!o) return;
33  *o << " " << option;
34  int padding = width - option.size();
35  if (padding < 2) {
36  *o << std::endl;
37  padding = width + 2; // 2 is
38  }
39  for (int i = 0; i < padding; ++i)
40  *o << ' ';
41  }
42 
50  void print_help_helper(std::ostream* o,
51  const std::string& key_val,
52  const std::string& msg,
53  const std::string& note = "") {
54  if (!o) return;
55  pad_help_option(o,key_val);
56  *o << msg
57  << std::endl;
58  if (note.size() > 0) {
59  pad_help_option(o,"");
60  *o << " (" << note << ")"
61  << std::endl;
62  }
63  *o << std::endl;
64  }
65 
74  void print_help_option(std::ostream* o,
75  const std::string& key,
76  const std::string& value_type,
77  const std::string& msg,
78  const std::string& note = "") {
79  std::stringstream ss;
80  ss << "--" << key;
81  if (value_type.size() > 0)
82  ss << "=<" << value_type << ">";
83  print_help_helper(o,ss.str(),msg,note);
84  }
85 
113  class cmd_line {
114  private:
115  std::string cmd_;
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) {
120  if (s.size() < 2
121  || s[0] != '-'
122  || s[1] != '-') {
123  bare_.push_back(s);
124  return;
125  }
126  for (size_t i = 2; i < s.size(); ++i) {
127  if (s[i] == '=') {
128  key_val_[s.substr(2,i - 2)] = s.substr(i + 1,s.size() - i - 1);
129  return;
130  }
131  }
132  flag_.insert(s.substr(2,s.size()));
133  }
134  public:
142  cmd_line(int argc, const char* argv[])
143  : cmd_(argv[0]) {
144  for (int i = 1; i < argc; ++i)
145  parse_arg(argv[i]);
146  }
147 
155  std::string command() {
156  return cmd_;
157  }
158 
165  bool has_key(const std::string& key) const {
166  return key_val_.find(key) != key_val_.end();
167  }
168 
187  template <typename T>
188  bool val(const std::string& key, T& x) const {
189  if (!has_key(key))
190  return false;
191  std::stringstream s(key_val_.find(key)->second);
192  return s >> x;
193  }
194 
201  bool has_flag(const std::string& flag) const {
202  return flag_.find(flag) != flag_.end();
203  }
204 
210  size_t bare_size() const {
211  return bare_.size();
212  }
213 
227  template <typename T>
228  bool bare(size_t n, T& x) const {
229  if (n >= bare_.size())
230  return false;
231  std::stringstream s(bare_[n]);
232  s >> x;
233  return true;
234  }
235 
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();
246  it != flag_.end();
247  ++it) {
248  out << "FLAG " << flag_count << "=" << (*it) << '\n';
249  ++flag_count;
250  }
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();
254  ++it) {
255  out << "KEY " << key_val_count << "=" << (*it).first;
256  out << " VAL " << key_val_count << "=" << (*it).second << '\n';
257  ++key_val_count;
258  }
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';
262  ++bare_count;
263  }
264  }
265 
266  };
267 
268 
269  }
270 
271 }
272 
273 #endif

     [ Stan Home Page ] © 2011–2012, Stan Development Team.