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)
50 const std::string& key_val,
51 const std::string& msg,
52 const std::string& note =
"") {
57 if (note.size() > 0) {
59 *o <<
" (" << note <<
")"
75 const std::string& key,
76 const std::string& value_type,
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()));
145 for (
int i = 1; i < argc; ++i)
167 return key_val_.find(key) != key_val_.end();
189 template <
typename T>
190 bool val(
const std::string& key, T& x)
const {
193 std::stringstream s(key_val_.find(key)->second);
205 return flag_.find(flag) != flag_.end();
230 template <
typename T>
231 bool bare(
size_t n, T& x)
const {
232 if (n >= bare_.size())
234 std::stringstream s(bare_[n]);
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();
252 out <<
"FLAG " << flag_count <<
"=" << (*it) <<
'\n';
255 size_t key_val_count = 0;
256 for (std::map<std::string, std::string>::const_iterator it
258 it != key_val_.end();
260 out <<
"KEY " << key_val_count <<
"=" << (*it).first;
261 out <<
" VAL " << key_val_count <<
"=" << (*it).second <<
'\n';
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';
275 bool cmd_line::bare<std::string>(
size_t n, std::string& x)
const {
276 if (n >= bare_.size())
285 bool cmd_line::val<std::string>(
const std::string& key,
286 std::string& x)
const {
289 x = key_val_.find(key)->second;
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.
bool val(const std::string &key, T &x) const
Returns the value for the key provided.
bool has_key(const std::string &key) const
Return true if the specified key is defined.
cmd_line(int argc, const char *argv[])
Construct a command-line argument object from the specified command-line arguments.
Parses and stores command-line arguments.
bool has_flag(const std::string &flag) const
Return true if the specified flag is defined.
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.
size_t bare_size() const
Return the number of bare arguments.
void print(std::ostream &out) const
Print a human readable parsed form of the command-line arguments to the specified output stream...
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.
std::string command()
Returns the name of the command itself.