Stan  2.10.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) {
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 (_subarguments.size() == 0)
96  valid_arg = true;
97  for (std::vector<argument*>::iterator it = _subarguments.begin();
98  it != _subarguments.end(); ++it) {
99  if ((*it)->name() == cat_name) {
100  args.pop_back();
101  valid_arg &= (*it)->parse_args(args, info, err, help_flag);
102  good_arg = true;
103  break;
104  } else if ( (*it)->name() == val_name ) {
105  valid_arg &= (*it)->parse_args(args, info, err, help_flag);
106  good_arg = true;
107  break;
108  } else {
109  good_arg = false;
110  }
111  }
112  }
113  return valid_arg;
114  }
115 
116  virtual void probe_args(argument* base_arg,
118  for (std::vector<argument*>::iterator it = _subarguments.begin();
119  it != _subarguments.end(); ++it) {
120  (*it)->probe_args(base_arg, w);
121  }
122  }
123 
124  void find_arg(const std::string& name,
125  const std::string& prefix,
126  std::vector<std::string>& valid_paths) {
127  argument::find_arg(name, prefix, valid_paths);
128 
129  std::string new_prefix = prefix + _name + " ";
130  for (std::vector<argument*>::iterator it = _subarguments.begin();
131  it != _subarguments.end(); ++it)
132  (*it)->find_arg(name, new_prefix, valid_paths);
133  }
134 
135  std::vector<argument*>& subarguments() {
136  return _subarguments;
137  }
138 
139  argument* arg(const std::string& name) {
140  for (std::vector<argument*>::iterator it = _subarguments.begin();
141  it != _subarguments.end(); ++it)
142  if ( name == (*it)->name() )
143  return (*it);
144  return 0;
145  }
146 
147  protected:
148  std::vector<argument*> _subarguments;
149  };
150 
151  } // services
152 } // stan
153 
154 #endif
Probability, optimization and sampling library.
std::vector< argument * > & subarguments()
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
std::string name() const
Definition: argument.hpp:26
virtual void probe_args(argument *base_arg, interface_callbacks::writer::base_writer &w)

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