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 =
"") {
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';
internal::traits< Derived >::Scalar value_type
Parses and stores command-line arguments.
size_t bare_size() const
Return the number of bare arguments.
bool has_key(const std::string &key) const
Return true if the specified key is defined.
void print(std::ostream &out) const
Print a human readable parsed form of the command-line arguments to the specified output stream.
cmd_line(int argc, const char *argv[])
Construct a command-line argument object from the specified command-line arguments.
bool has_flag(const std::string &flag) const
Return true if the specified flag is defined.
bool val(const std::string &key, T &x) const
Returns the value for the key provided.
std::string command()
Returns the name of the command itself.
bool bare(size_t n, T &x) const
Returns the bare argument.
void print_help_option(std::ostream *o, const std::string &key, const std::string &value_type, const std::string &msg, const std::string ¬e="")
Prints single print option to output ptr if non-null.
void print_help_helper(std::ostream *o, const std::string &key_val, const std::string &msg, const std::string ¬e="")
Prints single print option to output ptr if non-null.
void pad_help_option(std::ostream *o, const std::string &option="", unsigned int width=20)
Print help option with padding.
Probability, optimization and sampling library.