Public Member Functions | Private Member Functions | Private Attributes | List of all members
stan::io::cmd_line Class Reference

#include "stan/io/cmd_line.hpp"

Public Member Functions

 cmd_line (int argc, const char *argv[])
 
std::string command ()
 
bool has_key (const std::string &key) const
 
template<typename T >
bool val (const std::string &key, T &x) const
 
bool has_flag (const std::string &flag) const
 
size_t bare_size () const
 
template<typename T >
bool bare (size_t n, T &x) const
 
void print (std::ostream &out) const
 

Private Member Functions

void parse_arg (const std::string &s)
 

Private Attributes

std::string cmd_
 
std::map< std::string, std::string > key_val_
 
std::set< std::string > flag_
 
std::vector< std::string > bare_
 

Detailed Description

Parses and stores command-line arguments.

Command-line arguments are organized into four types.

Command: The first argument (at index 0) is just the command itself. There method command() retrieves the command.

Key/Value: The second type of argument is a key-value pair, which must be in the form –key=val. Two hyphens are used to separate arguments from negated numbers. The method has_key(const std::string&) indicates if there is a key and val(const std::string&,T&) writes its value into a reference (whose type is templated; any type understand by the output operator >> is acceptable.

Flag: Flags are specified as –flag. The method has_flag(const std::string&) tests if a flag is present.

Bare Argument: Bare arguments are any arguments that are not prefixed with two hyphens (). The method bare_size() returns the number of bare arguments and they are retrieved with the generic method bare(const std::string&,T&).

Definition at line 113 of file cmd_line.hpp.

Constructor & Destructor Documentation

stan::io::cmd_line::cmd_line ( int  argc,
const char *  argv[] 
)
inline

Construct a command-line argument object from the specified command-line arguments.

Parameters
argcNumber of arguments.
argvArgument strings.

Definition at line 143 of file cmd_line.hpp.

References MECModelEnuComparisons::i, and parse_arg().

144  : cmd_(argv[0]) {
145  for (int i = 1; i < argc; ++i)
146  parse_arg(argv[i]);
147  }
void parse_arg(const std::string &s)
Definition: cmd_line.hpp:119
std::string cmd_
Definition: cmd_line.hpp:115

Member Function Documentation

template<typename T >
bool stan::io::cmd_line::bare ( size_t  n,
T x 
) const
inline

Returns the bare argument.

If the specified index is valid for bare arguments, write the bare argument at the specified index into the specified reference, and otherwise return false without modifying the reference.

Parameters
[in]nBare argument position.
[out]xReference to result.
Returns
true if there were enough bare arguments.
Template Parameters
TType of value returned.

Definition at line 231 of file cmd_line.hpp.

References submit_syst::x.

Referenced by stanc_helper(), and TEST().

231  {
232  if (n >= bare_.size())
233  return false;
234  std::stringstream s(bare_[n]);
235  s >> x;
236  return true;
237  }
std::vector< std::string > bare_
Definition: cmd_line.hpp:118
const XML_Char * s
Definition: expat.h:262
size_t stan::io::cmd_line::bare_size ( ) const
inline

Return the number of bare arguments.

Returns
Number of bare arguments.

Definition at line 213 of file cmd_line.hpp.

Referenced by stanc_helper(), and TEST().

213  {
214  return bare_.size();
215  }
std::vector< std::string > bare_
Definition: cmd_line.hpp:118
std::string stan::io::cmd_line::command ( )
inline

Returns the name of the command itself. The command is always supplied as the first argument (at index 0).

Returns
Name of command.

Definition at line 156 of file cmd_line.hpp.

References cmd_.

Referenced by TEST().

156  {
157  return cmd_;
158  }
std::string cmd_
Definition: cmd_line.hpp:115
bool stan::io::cmd_line::has_flag ( const std::string &  flag) const
inline

Return true if the specified flag is defined.

Parameters
flagFlag to test.
Returns
true if flag is defined.

Definition at line 204 of file cmd_line.hpp.

Referenced by stanc_helper(), and TEST().

204  {
205  return flag_.find(flag) != flag_.end();
206  }
std::set< std::string > flag_
Definition: cmd_line.hpp:117
bool stan::io::cmd_line::has_key ( const std::string &  key) const
inline

Return true if the specified key is defined.

Parameters
keyKey to test.
Returns
true if it has a value.

Definition at line 166 of file cmd_line.hpp.

Referenced by stan::io::cmd_line::val< std::string >(), stanc_helper(), TEST(), and val().

166  {
167  return key_val_.find(key) != key_val_.end();
168  }
std::map< std::string, std::string > key_val_
Definition: cmd_line.hpp:116
void stan::io::cmd_line::parse_arg ( const std::string &  s)
inlineprivate

Definition at line 119 of file cmd_line.hpp.

References MECModelEnuComparisons::i.

Referenced by cmd_line().

119  {
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  }
std::set< std::string > flag_
Definition: cmd_line.hpp:117
std::vector< std::string > bare_
Definition: cmd_line.hpp:118
const XML_Char * s
Definition: expat.h:262
std::map< std::string, std::string > key_val_
Definition: cmd_line.hpp:116
void stan::io::cmd_line::print ( std::ostream &  out) const
inline

Print a human readable parsed form of the command-line arguments to the specified output stream.

Parameters
[out]outOutput stream.

Definition at line 246 of file cmd_line.hpp.

References MECModelEnuComparisons::i, and it.

246  {
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  }
set< int >::iterator it
std::set< std::string > flag_
Definition: cmd_line.hpp:117
std::vector< std::string > bare_
Definition: cmd_line.hpp:118
std::string cmd_
Definition: cmd_line.hpp:115
std::map< std::string, std::string > key_val_
Definition: cmd_line.hpp:116
template<typename T >
bool stan::io::cmd_line::val ( const std::string &  key,
T x 
) const
inline

Returns the value for the key provided.

If the specified key is defined, write the value of the key into the specified reference and return true, otherwise do not modify the reference and return false.

The conversions defined by std::ostream are used to convert the base string value to the specified type. Thus this method will work as long as operator>>() is defined for the specified type.

Parameters
[in]keyKey whose value is returned.
[out]xReference to value.
Returns
False if the key is not found, and true if it is found.
Template Parameters
Typeof value.

Definition at line 190 of file cmd_line.hpp.

References has_key(), and submit_syst::x.

Referenced by stanc_helper(), and TEST().

190  {
191  if (!has_key(key))
192  return false;
193  std::stringstream s(key_val_.find(key)->second);
194  s >> x;
195  return true;
196  }
bool has_key(const std::string &key) const
Definition: cmd_line.hpp:166
const XML_Char * s
Definition: expat.h:262
std::map< std::string, std::string > key_val_
Definition: cmd_line.hpp:116

Member Data Documentation

std::vector<std::string> stan::io::cmd_line::bare_
private

Definition at line 118 of file cmd_line.hpp.

Referenced by stan::io::cmd_line::bare< std::string >().

std::string stan::io::cmd_line::cmd_
private

Definition at line 115 of file cmd_line.hpp.

Referenced by command().

std::set<std::string> stan::io::cmd_line::flag_
private

Definition at line 117 of file cmd_line.hpp.

std::map<std::string, std::string> stan::io::cmd_line::key_val_
private

Definition at line 116 of file cmd_line.hpp.

Referenced by stan::io::cmd_line::val< std::string >().


The documentation for this class was generated from the following file: