Stan  1.0
probability, sampling & optimization
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 <map>
5 #include <ostream>
6 #include <set>
7 #include <string>
8 #include <sstream>
9 #include <vector>
10 
11 namespace stan {
12 
13  namespace io {
14 
27  void pad_help_option(std::ostream* o,
28  const std::string& option = "",
29  unsigned int width = 20) {
30  if (!o) return;
31  *o << " " << option;
32  int padding = width - option.size();
33  if (padding < 2) {
34  *o << std::endl;
35  padding = width + 2; // 2 is
36  }
37  for (int i = 0; i < padding; ++i)
38  *o << ' ';
39  }
40 
48  void print_help_helper(std::ostream* o,
49  const std::string& key_val,
50  const std::string& msg,
51  const std::string& note = "") {
52  if (!o) return;
53  pad_help_option(o,key_val);
54  *o << msg
55  << std::endl;
56  if (note.size() > 0) {
57  pad_help_option(o,"");
58  *o << " (" << note << ")"
59  << std::endl;
60  }
61  *o << std::endl;
62  }
63 
72  void print_help_option(std::ostream* o,
73  const std::string& key,
74  const std::string& value_type,
75  const std::string& msg,
76  const std::string& note = "") {
77  std::stringstream ss;
78  ss << "--" << key;
79  if (value_type.size() > 0)
80  ss << "=<" << value_type << ">";
81  print_help_helper(o,ss.str(),msg,note);
82  }
83 
111  class cmd_line {
112  private:
113  std::string cmd_;
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) {
118  if (s.size() < 2
119  || s[0] != '-'
120  || s[1] != '-') {
121  bare_.push_back(s);
122  return;
123  }
124  for (size_t i = 2; i < s.size(); ++i) {
125  if (s[i] == '=') {
126  key_val_[s.substr(2,i - 2)] = s.substr(i + 1,s.size() - i - 1);
127  return;
128  }
129  }
130  flag_.insert(s.substr(2,s.size()));
131  }
132  public:
140  cmd_line(int argc, const char* argv[])
141  : cmd_(argv[0]) {
142  for (int i = 1; i < argc; ++i)
143  parse_arg(argv[i]);
144  }
145 
153  std::string command() {
154  return cmd_;
155  }
156 
163  bool has_key(const std::string& key) const {
164  return key_val_.find(key) != key_val_.end();
165  }
166 
186  template <typename T>
187  bool val(const std::string& key, T& x) const {
188  if (!has_key(key))
189  return false;
190  std::stringstream s(key_val_.find(key)->second);
191  s >> x;
192  return true;
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
internal::traits< Derived >::Scalar value_type
Parses and stores command-line arguments.
Definition: cmd_line.hpp:111
size_t bare_size() const
Return the number of bare arguments.
Definition: cmd_line.hpp:210
bool has_key(const std::string &key) const
Return true if the specified key is defined.
Definition: cmd_line.hpp:163
void print(std::ostream &out) const
Print a human readable parsed form of the command-line arguments to the specified output stream.
Definition: cmd_line.hpp:242
cmd_line(int argc, const char *argv[])
Construct a command-line argument object from the specified command-line arguments.
Definition: cmd_line.hpp:140
bool has_flag(const std::string &flag) const
Return true if the specified flag is defined.
Definition: cmd_line.hpp:201
bool val(const std::string &key, T &x) const
Returns the value for the key provided.
Definition: cmd_line.hpp:187
std::string command()
Returns the name of the command itself.
Definition: cmd_line.hpp:153
bool bare(size_t n, T &x) const
Returns the bare argument.
Definition: cmd_line.hpp:228
void print_help_option(std::ostream *o, const std::string &key, const std::string &value_type, const std::string &msg, const std::string &note="")
Prints single print option to output ptr if non-null.
Definition: cmd_line.hpp:72
void print_help_helper(std::ostream *o, const std::string &key_val, const std::string &msg, const std::string &note="")
Prints single print option to output ptr if non-null.
Definition: cmd_line.hpp:48
void pad_help_option(std::ostream *o, const std::string &option="", unsigned int width=20)
Print help option with padding.
Definition: cmd_line.hpp:27
Probability, optimization and sampling library.
Definition: agrad.cpp:6

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