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()));
144 for (
int i = 1; i < argc; ++i)
166 return key_val_.find(key) != key_val_.end();
188 template <
typename T>
189 bool val(
const std::string& key, T& x)
const {
192 std::stringstream s(key_val_.find(key)->second);
204 return flag_.find(flag) != flag_.end();
229 template <
typename T>
230 bool bare(
size_t n, T& x)
const {
231 if (n >= bare_.size())
233 std::stringstream s(bare_[n]);
245 void print(std::ostream& out)
const {
246 out <<
"COMMAND=" << cmd_ <<
'\n';
247 size_t flag_count = 0;
248 for (std::set<std::string>::const_iterator it = flag_.begin();
251 out <<
"FLAG " << flag_count <<
"=" << (*it) <<
'\n';
254 size_t key_val_count = 0;
255 for (std::map<std::string,std::string>::const_iterator it = key_val_.begin();
256 it != key_val_.end();
258 out <<
"KEY " << key_val_count <<
"=" << (*it).first;
259 out <<
" VAL " << key_val_count <<
"=" << (*it).second <<
'\n';
262 size_t bare_count = 0;
263 for (
size_t i = 0; i < bare_.size(); ++i) {
264 out <<
"BARE ARG " << bare_count <<
"=" << bare_[i] <<
'\n';
274 bool cmd_line::bare<std::string>(
size_t n, std::string& x)
const {
275 if (n >= bare_.size())
284 bool cmd_line::val<std::string>(
const std::string& key, std::string& x)
const {
287 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.
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.