Stan  2.14.0
probability, sampling & optimization
categorical_argument.hpp
Go to the documentation of this file.
1 #ifndef STAN_SERVICES_ARGUMENTS_CATEGORICAL_ARGUMENT_HPP
2 #define STAN_SERVICES_ARGUMENTS_CATEGORICAL_ARGUMENT_HPP
3 
5 #include <string>
6 #include <vector>
7 
8 namespace stan {
9  namespace services {
10 
12  public:
14  for (std::vector<argument*>::iterator it = _subarguments.begin();
15  it != _subarguments.end(); ++it) {
16  delete *it;
17  }
18 
19  _subarguments.clear();
20  }
21 
23  const int depth,
24  const std::string& prefix) {
25  std::string indent(compute_indent(depth), ' ');
26  w(prefix + indent + _name);
27 
28  for (std::vector<argument*>::iterator it = _subarguments.begin();
29  it != _subarguments.end(); ++it)
30  (*it)->print(w, depth + 1, prefix);
31  }
32 
34  const int depth,
35  const bool recurse) {
36  std::string indent(indent_width * depth, ' ');
37  std::string subindent(indent_width, ' ');
38 
39  w(indent + _name);
40  w(indent + subindent + _description);
41  if (_subarguments.size() > 0) {
42  std::stringstream ss;
43  ss << indent << subindent << "Valid subarguments:";
44 
45  std::vector<argument*>::iterator it = _subarguments.begin();
46  ss << " " << (*it)->name();
47  ++it;
48 
49  for (; it != _subarguments.end(); ++it)
50  ss << ", " << (*it)->name();
51  w(ss.str());
52  w();
53 
54  if (recurse) {
55  for (std::vector<argument*>::iterator it = _subarguments.begin();
56  it != _subarguments.end(); ++it)
57  (*it)->print_help(w, depth + 1, true);
58  }
59  } else {
60  w();
61  }
62  }
63 
64  bool parse_args(std::vector<std::string>& args,
67  bool& help_flag) {
68  bool good_arg = true;
69  bool valid_arg = true;
70 
71  while (good_arg && valid_arg) {
72  if (args.size() == 0)
73  return valid_arg;
74 
75  good_arg = false;
76 
77  std::string cat_name = args.back();
78 
79  if (cat_name == "help") {
80  print_help(info, 0, false);
81  help_flag |= true;
82  args.clear();
83  return true;
84  } else if (cat_name == "help-all") {
85  print_help(info, 0, true);
86  help_flag |= true;
87  args.clear();
88  return true;
89  }
90 
91  std::string val_name;
92  std::string val;
93  split_arg(cat_name, val_name, val);
94 
95  if (val_name == this->name())
96  return false;
97 
98  if (_subarguments.size() == 0)
99  valid_arg = true;
100  for (std::vector<argument*>::iterator it = _subarguments.begin();
101  it != _subarguments.end(); ++it) {
102  if ((*it)->name() == cat_name) {
103  args.pop_back();
104  valid_arg &= (*it)->parse_args(args, info, err, help_flag);
105  good_arg = true;
106  break;
107  } else if ( (*it)->name() == val_name ) {
108  valid_arg &= (*it)->parse_args(args, info, err, help_flag);
109  good_arg = true;
110  break;
111  } else {
112  good_arg = false;
113  }
114  }
115  }
116  return valid_arg;
117  }
118 
119  virtual void probe_args(argument* base_arg,
121  for (std::vector<argument*>::iterator it = _subarguments.begin();
122  it != _subarguments.end(); ++it) {
123  (*it)->probe_args(base_arg, w);
124  }
125  }
126 
127  void find_arg(const std::string& name,
128  const std::string& prefix,
129  std::vector<std::string>& valid_paths) {
130  argument::find_arg(name, prefix, valid_paths);
131 
132  std::string new_prefix = prefix + _name + " ";
133  for (std::vector<argument*>::iterator it = _subarguments.begin();
134  it != _subarguments.end(); ++it)
135  (*it)->find_arg(name, new_prefix, valid_paths);
136  }
137 
138  std::vector<argument*>& subarguments() {
139  return _subarguments;
140  }
141 
142  argument* arg(const std::string& name) {
143  for (std::vector<argument*>::iterator it = _subarguments.begin();
144  it != _subarguments.end(); ++it)
145  if ( name == (*it)->name() )
146  return (*it);
147  return 0;
148  }
149 
150  protected:
151  std::vector<argument*> _subarguments;
152  };
153 
154  } // services
155 } // stan
156 
157 #endif
Probability, optimization and sampling library.
std::vector< argument * > & subarguments()
std::string name() const
Definition: argument.hpp:26
static void split_arg(const std::string &arg, std::string &name, std::string &value)
Definition: argument.hpp:60
std::string _description
Definition: argument.hpp:84
argument * arg(const std::string &name)
void find_arg(const std::string &name, const std::string &prefix, std::vector< std::string > &valid_paths)
int compute_indent(const int depth)
Definition: argument.hpp:78
base_writer is an abstract base class defining the interface for Stan writer callbacks.
Definition: base_writer.hpp:20
void print_help(interface_callbacks::writer::base_writer &w, const int depth, const bool recurse)
bool parse_args(std::vector< std::string > &args, interface_callbacks::writer::base_writer &info, interface_callbacks::writer::base_writer &err, bool &help_flag)
void print(interface_callbacks::writer::base_writer &w, const int depth, const std::string &prefix)
virtual void find_arg(const std::string &name, const std::string &prefix, std::vector< std::string > &valid_paths)
Definition: argument.hpp:52
virtual void probe_args(argument *base_arg, interface_callbacks::writer::base_writer &w)

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