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

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