ROSE  0.11.96.11
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 #include <Sawyer/Sawyer.h>
13 
14 namespace Sawyer {
15 namespace CommandLine {
16 
27 namespace Boost {
28 
36 template<class T>
37 struct value {};
38 
46 
49 
51  explicit options_description(const std::string &title): sg(title) {}
52 
61  options_description& operator()(const std::string &switchName, const value<std::string>&, const std::string &doc);
62  options_description& operator()(const std::string &switchName, const value<int>&, const std::string &doc);
63  options_description& operator()(const std::string &switchName, const value<long int>&, const std::string &doc);
64  options_description& operator()(const std::string &switchName, const value<std::vector<int> >&, const std::string &doc);
65  options_description& operator()(const std::string &switchName, const value< std::vector<std::string> >&,
66  const std::string &doc);
67  options_description& operator()(const std::string &switchName, const std::string &doc);
75  return *this;
76  }
77 
85 
91  void print() const;
92 };
93 
95 std::ostream& operator<<(std::ostream &out, const options_description &x);
96 
99  int argc;
100  char **argv;
107  parser.errorStream(Sawyer::Message::mlog[Sawyer::Message::FATAL]); // use error messages instead of exceptions
108  }
109 
124 
129  return parser.parse(argc, argv).apply();
130  }
131 };
132 
137 struct parsed_values { // not a boost::program_options type
142 
145 
149  template<class T>
150  T as() {
151  return as_helper(T());
152  }
153 
154 private:
155  template<class T>
156  T as_helper(const T&) {
157  ASSERT_forbid(pv.empty());
158  return pv.back().as<T>();
159  }
160 
161  template<class T>
162  std::vector<T> as_helper(const std::vector<T>&) {
163  std::vector<T> retval;
164  BOOST_FOREACH (const Sawyer::CommandLine::ParsedValue &v, pv) {
166  BOOST_FOREACH (const Sawyer::CommandLine::ParsedValue &elmt, elmts)
167  retval.push_back(elmt.as<T>());
168  }
169  return retval;
170  }
173 };
174 
180  size_t count(const std::string &swName) const;
181 
183  parsed_values operator[](const std::string &swName) const;
184 };
185 
187 void store(const Sawyer::CommandLine::ParserResult &results, variables_map &output);
188 
194 void notify(variables_map&);
195 
196 } // namespace
197 
198 } // namespace
199 } // namespace
200 
201 #endif
Sawyer::CommandLine::Boost::command_line_parser::argv
char ** argv
Arguments saved by c'tor to be available during parsing.
Definition: CommandLineBoost.h:100
Sawyer::CommandLine::ParsedValue
Information about a parsed switch value.
Definition: util/Sawyer/CommandLine.h:505
Sawyer::CommandLine::Boost::command_line_parser::allow_unregistered
command_line_parser & allow_unregistered()
Add predefined switches.
Definition: CommandLineBoost.h:123
Sawyer::Message::mlog
Facility mlog
Facility used by Sawyer components.
Sawyer::CommandLine::Boost::variables_map::count
size_t count(const std::string &swName) const
Number of times a switch appeared.
Sawyer::CommandLine::Boost::command_line_parser::options
command_line_parser & options(const options_description &)
Insert specified switch declarations.
Sawyer::CommandLine::Parser
The parser for a program command line.
Definition: util/Sawyer/CommandLine.h:2756
Sawyer::CommandLine::ParserResult::apply
const ParserResult & apply() const
Saves parsed values in switch-specified locations.
Sawyer::CommandLine::Boost::notify
void notify(variables_map &)
Transfer map to C++ variables.
Sawyer::CommandLine::Boost::parsed_values::pv
Sawyer::CommandLine::ParsedValues pv
Wrapped ParsedValues.
Definition: CommandLineBoost.h:138
Sawyer::CommandLine::ParsedValues
std::vector< ParsedValue > ParsedValues
A vector of parsed values.
Definition: util/Sawyer/CommandLine.h:638
Sawyer::CommandLine::Boost::command_line_parser::run
Sawyer::CommandLine::ParserResult run()
Parse command-line.
Definition: CommandLineBoost.h:128
Sawyer::CommandLine::Boost::variables_map::operator[]
parsed_values operator[](const std::string &swName) const
Saved values for a switch.
Sawyer::CommandLine::ParserResult
The result from parsing a command line.
Definition: util/Sawyer/CommandLine.h:3333
Sawyer::CommandLine::Boost::command_line_parser::command_line_parser
command_line_parser(int argc, char *argv[])
Construct a parser.
Definition: CommandLineBoost.h:106
Sawyer::CommandLine::Boost::command_line_parser::argc
int argc
Argument count saved by c'tor to be available during parsing.
Definition: CommandLineBoost.h:99
Sawyer::Message::FATAL
@ FATAL
Messages that indicate an abnormal situation from which the program was unable to recover.
Definition: Message.h:332
Sawyer::CommandLine::Boost::store
void store(const Sawyer::CommandLine::ParserResult &results, variables_map &output)
Transfer parser results to map.
Sawyer::CommandLine::Boost::options_description::sg
Sawyer::CommandLine::SwitchGroup sg
The underlying Sawyer mechanism.
Definition: CommandLineBoost.h:45
Sawyer::CommandLine::ListParser::ValueList
std::list< ParsedValue > ValueList
Value type for list ParsedValue.
Definition: util/Sawyer/CommandLine.h:1336
Sawyer::CommandLine::Boost::parsed_values::as
T as()
Convert parsed value to another type.
Definition: CommandLineBoost.h:150
Sawyer::CommandLine::Parser::parse
ParserResult parse(int argc, char *argv[])
Parse program arguments.
Sawyer::CommandLine::Boost::parsed_values::parsed_values
parsed_values()
Wrap nothing.
Definition: CommandLineBoost.h:141
Sawyer::CommandLine::Boost::operator<<
std::ostream & operator<<(std::ostream &out, const options_description &x)
Print documentation for a few switches.
Sawyer::CommandLine::Boost::options_description::options_description
options_description(const std::string &title)
Construct an empty switch group having a title.
Definition: CommandLineBoost.h:51
Sawyer::CommandLine::Boost::options_description::operator()
options_description & operator()(const std::string &switchName, const value< std::string > &, const std::string &doc)
Declare a switch of specified type.
Sawyer::CommandLine::Boost::command_line_parser::parser
Sawyer::CommandLine::Parser parser
Wrapped parser.
Definition: CommandLineBoost.h:101
Sawyer
Name space for the entire library.
Definition: Access.h:13
Sawyer::CommandLine::Boost::options_description::add
options_description & add(const options_description &other)
Insert other switches into this group.
Sawyer::CommandLine::Boost::value
Replacement for basic use of boost::program_options::value.
Definition: CommandLineBoost.h:37
Sawyer::CommandLine::Boost::variables_map
Wrapper around ParserResult.
Definition: CommandLineBoost.h:176
Sawyer::CommandLine::Boost::options_description::options_description
options_description()
Construct an empty switch group without documentation.
Definition: CommandLineBoost.h:48
sg
This namespace contains template functions that operate on the ROSE AST.
Definition: sageFunctors.h:15
Sawyer::CommandLine::Boost::options_description
Replacement for basic use of boost::program_options::options_description.
Definition: CommandLineBoost.h:43
Sawyer::CommandLine::Boost::parsed_values
Wrapper around parsed values.
Definition: CommandLineBoost.h:137
Sawyer::CommandLine::Boost::command_line_parser
Wrapper around Sawyer's CommandLine class.
Definition: CommandLineBoost.h:98
Sawyer::CommandLine::SwitchGroup
A collection of related switch declarations.
Definition: util/Sawyer/CommandLine.h:2569
Sawyer::CommandLine::Boost::variables_map::pr
Sawyer::CommandLine::ParserResult pr
Wrapped ParserResult.
Definition: CommandLineBoost.h:177
Sawyer::CommandLine::Boost::options_description::print
void print() const
Print switch documentation.
Sawyer::CommandLine::Parser::errorStream
Parser & errorStream(const Message::SProxy &stream)
Specifies a message stream to which errors are sent.
Definition: util/Sawyer/CommandLine.h:2998
Sawyer::CommandLine::ParsedValue::as
T as() const
Convenient any_cast.
Definition: util/Sawyer/CommandLine.h:589
Sawyer::CommandLine::Boost::options_description::add_options
options_description & add_options()
Boost intermediate type for adding more switches.
Definition: CommandLineBoost.h:74
Sawyer::CommandLine::Boost::parsed_values::parsed_values
parsed_values(const Sawyer::CommandLine::ParsedValues &pv)
Wrap ParsedValues.
Definition: CommandLineBoost.h:144