ROSE  0.9.9.139
CommandLineBoost.h
1 // WARNING: Changes to this file must be contributed back to Sawyer or else they will
2 // be clobbered by the next update from Sawyer. The Sawyer repository is at
3 // https://github.com/matzke1/sawyer.
4 
5 
6 
7 
8 #ifndef Sawyer_CommandLine_Boost_H
9 #define Sawyer_CommandLine_Boost_H
10 
11 #include <Sawyer/CommandLine.h>
12 
13 namespace Sawyer {
14 namespace CommandLine {
15 
26 namespace Boost {
27 
35 template<class T>
36 struct value {};
37 
45 
48 
50  explicit options_description(const std::string &title): sg(title) {}
51 
60  options_description& operator()(const std::string &switchName, const value<std::string>&, const std::string &doc);
61  options_description& operator()(const std::string &switchName, const value<int>&, const std::string &doc);
62  options_description& operator()(const std::string &switchName, const value<long int>&, const std::string &doc);
63  options_description& operator()(const std::string &switchName, const value<std::vector<int> >&, const std::string &doc);
64  options_description& operator()(const std::string &switchName, const value< std::vector<std::string> >&,
65  const std::string &doc);
66  options_description& operator()(const std::string &switchName, const std::string &doc);
74  return *this;
75  }
76 
84 
90  void print() const;
91 };
92 
94 std::ostream& operator<<(std::ostream &out, const options_description &x);
95 
98  int argc;
99  char **argv;
105  command_line_parser(int argc, char *argv[]): argc(argc), argv(argv) {
106  parser.errorStream(Sawyer::Message::mlog[Sawyer::Message::FATAL]); // use error messages instead of exceptions
107  }
108 
123 
128  return parser.parse(argc, argv).apply();
129  }
130 };
131 
136 struct parsed_values { // not a boost::program_options type
141 
143  explicit parsed_values(const Sawyer::CommandLine::ParsedValues &pv): pv(pv) {}
144 
148  template<class T>
149  T as() {
150  return as_helper(T());
151  }
152 
153 private:
154  template<class T>
155  T as_helper(const T&) {
156  ASSERT_forbid(pv.empty());
157  return pv.back().as<T>();
158  }
159 
160  template<class T>
161  std::vector<T> as_helper(const std::vector<T>&) {
162  std::vector<T> retval;
163  BOOST_FOREACH (const Sawyer::CommandLine::ParsedValue &v, pv) {
165  BOOST_FOREACH (const Sawyer::CommandLine::ParsedValue &elmt, elmts)
166  retval.push_back(elmt.as<T>());
167  }
168  return retval;
169  }
172 };
173 
179  size_t count(const std::string &swName) const;
180 
182  parsed_values operator[](const std::string &swName) const;
183 };
184 
186 void store(const Sawyer::CommandLine::ParserResult &results, variables_map &output);
187 
193 void notify(variables_map&);
194 
195 } // namespace
196 
197 } // namespace
198 } // namespace
199 
200 #endif
std::vector< ParsedValue > ParsedValues
A vector of parsed values.
Definition: CommandLine.h:616
Sawyer::CommandLine::ParsedValues pv
Wrapped ParsedValues.
T as() const
Convenient any_cast.
Definition: CommandLine.h:567
options_description()
Construct an empty switch group without documentation.
The result from parsing a command line.
Definition: CommandLine.h:3177
Replacement for basic use of boost::program_options::options_description.
command_line_parser & options(const options_description &)
Insert specified switch declarations.
A collection of related switch declarations.
Definition: CommandLine.h:2488
command_line_parser & allow_unregistered()
Add predefined switches.
std::ostream & operator<<(std::ostream &out, const options_description &x)
Print documentation for a few switches.
Facility mlog
Facility used by Sawyer components.
T as()
Convert parsed value to another type.
Messages that indicate an abnormal situation from which the program was unable to recover...
Definition: Message.h:329
void store(const Sawyer::CommandLine::ParserResult &results, variables_map &output)
Transfer parser results to map.
Name space for the entire library.
Definition: Access.h:11
options_description & operator()(const std::string &switchName, const value< std::string > &, const std::string &doc)
Declare a switch of specified type.
command_line_parser(int argc, char *argv[])
Construct a parser.
int argc
Argument count saved by c'tor to be available during parsing.
Information about a parsed switch value.
Definition: CommandLine.h:483
The parser for a program command line.
Definition: CommandLine.h:2674
options_description & add(const options_description &other)
Insert other switches into this group.
Sawyer::CommandLine::ParserResult run()
Parse command-line.
void notify(variables_map &)
Transfer map to C++ variables.
char ** argv
Arguments saved by c'tor to be available during parsing.
Sawyer::CommandLine::Parser parser
Wrapped parser.
options_description & add_options()
Boost intermediate type for adding more switches.
Sawyer::CommandLine::ParserResult pr
Wrapped ParserResult.
const ParserResult & apply() const
Saves parsed values in switch-specified locations.
std::list< ParsedValue > ValueList
Value type for list ParsedValue.
Definition: CommandLine.h:1293
ParserResult parse(int argc, char *argv[])
Parse program arguments.
Sawyer::CommandLine::SwitchGroup sg
The underlying Sawyer mechanism.
Replacement for basic use of boost::program_options::value.
options_description(const std::string &title)
Construct an empty switch group having a title.
void print() const
Print switch documentation.
Wrapper around Sawyer's CommandLine class.
Parser & errorStream(const Message::SProxy &stream)
Specifies a message stream to which errors are sent.
Definition: CommandLine.h:2878
Wrapper around parsed values.
parsed_values(const Sawyer::CommandLine::ParsedValues &pv)
Wrap ParsedValues.