Stan  2.10.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 
49  void print_help_helper(std::ostream* o,
50  const std::string& key_val,
51  const std::string& msg,
52  const std::string& note = "") {
53  if (!o) return;
54  pad_help_option(o, key_val);
55  *o << msg
56  << std::endl;
57  if (note.size() > 0) {
58  pad_help_option(o, "");
59  *o << " (" << note << ")"
60  << std::endl;
61  }
62  *o << std::endl;
63  }
64 
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 
135  public:
143  cmd_line(int argc, const char* argv[])
144  : cmd_(argv[0]) {
145  for (int i = 1; i < argc; ++i)
146  parse_arg(argv[i]);
147  }
148 
156  std::string command() {
157  return cmd_;
158  }
159 
166  bool has_key(const std::string& key) const {
167  return key_val_.find(key) != key_val_.end();
168  }
169 
189  template <typename T>
190  bool val(const std::string& key, T& x) const {
191  if (!has_key(key))
192  return false;
193  std::stringstream s(key_val_.find(key)->second);
194  s >> x;
195  return true;
196  }
197 
204  bool has_flag(const std::string& flag) const {
205  return flag_.find(flag) != flag_.end();
206  }
207 
213  size_t bare_size() const {
214  return bare_.size();
215  }
216 
230  template <typename T>
231  bool bare(size_t n, T& x) const {
232  if (n >= bare_.size())
233  return false;
234  std::stringstream s(bare_[n]);
235  s >> x;
236  return true;
237  }
238 
239 
246  void print(std::ostream& out) const {
247  out << "COMMAND=" << cmd_ << '\n';
248  size_t flag_count = 0;
249  for (std::set<std::string>::const_iterator it = flag_.begin();
250  it != flag_.end();
251  ++it) {
252  out << "FLAG " << flag_count << "=" << (*it) << '\n';
253  ++flag_count;
254  }
255  size_t key_val_count = 0;
256  for (std::map<std::string, std::string>::const_iterator it
257  = key_val_.begin();
258  it != key_val_.end();
259  ++it) {
260  out << "KEY " << key_val_count << "=" << (*it).first;
261  out << " VAL " << key_val_count << "=" << (*it).second << '\n';
262  ++key_val_count;
263  }
264  size_t bare_count = 0;
265  for (size_t i = 0; i < bare_.size(); ++i) {
266  out << "BARE ARG " << bare_count << "=" << bare_[i] << '\n';
267  ++bare_count;
268  }
269  }
270  };
271 
272  // explicit instantation for std::string to allow for spaces
273  // in bare_[n]
274  template <>
275  bool cmd_line::bare<std::string>(size_t n, std::string& x) const {
276  if (n >= bare_.size())
277  return false;
278  x = bare_[n];
279  return true;
280  }
281 
282  // explicit instantation for std::string to allow for spaces
283  // in key_val_
284  template <>
285  bool cmd_line::val<std::string>(const std::string& key,
286  std::string& x) const {
287  if (!has_key(key))
288  return false;
289  x = key_val_.find(key)->second;
290  return true;
291  }
292  }
293 }
294 
295 #endif
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.
bool val(const std::string &key, T &x) const
Returns the value for the key provided.
Definition: cmd_line.hpp:190
bool has_key(const std::string &key) const
Return true if the specified key is defined.
Definition: cmd_line.hpp:166
cmd_line(int argc, const char *argv[])
Construct a command-line argument object from the specified command-line arguments.
Definition: cmd_line.hpp:143
Parses and stores command-line arguments.
Definition: cmd_line.hpp:113
bool has_flag(const std::string &flag) const
Return true if the specified flag is defined.
Definition: cmd_line.hpp:204
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:49
size_t bare_size() const
Return the number of bare arguments.
Definition: cmd_line.hpp:213
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:246
bool bare(size_t n, T &x) const
Returns the bare argument.
Definition: cmd_line.hpp:231
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:74
std::string command()
Returns the name of the command itself.
Definition: cmd_line.hpp:156

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