Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
list_argument.hpp
Go to the documentation of this file.
1 #ifndef STAN__GM__ARGUMENTS__LIST__ARGUMENT__BETA
2 #define STAN__GM__ARGUMENTS__LIST__ARGUMENT__BETA
3 
4 #include <iostream>
7 
8 namespace stan {
9 
10  namespace gm {
11 
13 
14  public:
15 
17  _value_type = "list element";
18  }
19 
21 
22  for (std::vector<argument*>::iterator it = _values.begin();
23  it != _values.end(); ++it) {
24  delete *it;
25  }
26 
27  _values.clear();
28 
29  }
30 
31  void print(std::ostream* s, int depth, const std::string prefix) {
32  valued_argument::print(s, depth, prefix);
33  _values.at(_cursor)->print(s, depth + 1, prefix);
34  }
35 
36  void print_help(std::ostream* s, int depth, bool recurse) {
37  _default = _values.at(_default_cursor)->name();
38 
40 
41  if (recurse) {
42  for (std::vector<argument*>::iterator it = _values.begin();
43  it != _values.end(); ++it)
44  (*it)->print_help(s, depth + 1, true);
45  }
46  }
47 
48  bool parse_args(std::vector<std::string>& args, std::ostream* out,
49  std::ostream* err, bool& help_flag) {
50 
51  if(args.size() == 0) return true;
52 
53  std::string name;
54  std::string value;
55  split_arg(args.back(), name, value);
56 
57  if(_name == "help") {
58  print_help(out, 0, false);
59  help_flag |= true;
60  args.clear();
61  return false;
62  }
63  else if(_name == "help-all") {
64  print_help(out, 0, true);
65  help_flag |= true;
66  args.clear();
67  return false;
68  }
69  else if(_name == name) {
70 
71  args.pop_back();
72 
73  bool good_arg = false;
74  bool valid_arg = true;
75 
76  for (size_t i = 0; i < _values.size(); ++i) {
77  if( _values.at(i)->name() != value) continue;
78 
79  _cursor = i;
80  valid_arg &= _values.at(_cursor)->parse_args(args, out, err, help_flag);
81  good_arg = true;
82  break;
83  }
84 
85  if(!good_arg) {
86 
87  if(err) {
88  *err << value << " is not a valid value for \"" << _name << "\"" << std::endl;
89  *err << std::string(indent_width, ' ') << "Valid values:" << print_valid() << std::endl;
90  }
91 
92  args.clear();
93  }
94 
95  return valid_arg && good_arg;
96 
97  }
98 
99  return true;
100 
101  };
102 
103  virtual void probe_args(argument* base_arg, std::stringstream& s) {
104 
105  for (size_t i = 0; i < _values.size(); ++i) {
106  _cursor = i;
107 
108  s << "good" << std::endl;
109  base_arg->print(&s, 0, "");
110  s << std::endl;
111 
112  _values.at(i)->probe_args(base_arg, s);
113  }
114 
115  _values.push_back(new arg_fail);
116  _cursor = _values.size() - 1;
117  s << "bad" << std::endl;
118  base_arg->print(&s, 0, "");
119  s << std::endl;
120 
121  _values.pop_back();
123 
124  }
125 
126  void find_arg(std::string name,
127  std::string prefix,
128  std::vector<std::string>& valid_paths) {
129 
130  if (name == _name) {
131  valid_paths.push_back(prefix + _name + "=<list_element>");
132  }
133 
134  prefix += _name + "=";
135  for (std::vector<argument*>::iterator it = _values.begin();
136  it != _values.end(); ++it) {
137  std::string value_prefix = prefix + (*it)->name() + " ";
138  (*it)->find_arg(name, prefix, valid_paths);
139  }
140 
141  }
142 
143  bool valid_value(std::string name) {
144  for (std::vector<argument*>::iterator it = _values.begin();
145  it != _values.end(); ++it)
146  if (name == (*it)->name())
147  return true;
148  return false;
149  }
150 
151  argument* arg(std::string name) {
152  if(name == _values.at(_cursor)->name())
153  return _values.at(_cursor);
154  else
155  return 0;
156  }
157 
158  std::vector<argument*>& values() { return _values; }
159 
160  std::string value() { return _values.at(_cursor)->name(); }
161 
162  std::string print_value() { return _values.at(_cursor)->name(); }
163 
164  std::string print_valid() {
165  std::string valid_values;
166 
167  std::vector<argument*>::iterator it = _values.begin();
168  valid_values += " " + (*it)->name();
169  ++it;
170 
171  for (; it != _values.end(); ++it)
172  valid_values += ", " + (*it)->name();
173 
174  return valid_values;
175 
176  }
177 
178  bool is_default() { return _cursor == _default_cursor; }
179 
180  protected:
181 
182  int _cursor;
184 
185  std::vector<argument*> _values;
186 
187  };
188 
189  } // gm
190 
191 } // stan
192 
193 #endif
virtual void print_help(std::ostream *s, const int depth, const bool recurse=false)
argument * arg(std::string name)
void find_arg(std::string name, std::string prefix, std::vector< std::string > &valid_paths)
std::vector< argument * > _values
static void split_arg(const std::string &arg, std::string &name, std::string &value)
Definition: argument.hpp:54
virtual void probe_args(argument *base_arg, std::stringstream &s)
void print_help(std::ostream *s, int depth, bool recurse)
std::string _name
Definition: argument.hpp:76
virtual void print(std::ostream *s, const int depth, const std::string prefix)
bool parse_args(std::vector< std::string > &args, std::ostream *out, std::ostream *err, bool &help_flag)
bool valid_value(std::string name)
std::string name() const
Definition: argument.hpp:26
virtual void print(std::ostream *s, const int depth, const std::string prefix)=0
std::vector< argument * > & values()
void print(std::ostream *s, int depth, const std::string prefix)

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