Vowpal Wabbit
Public Member Functions | Private Member Functions | Private Attributes | List of all members
VW::config::options_boost_po Struct Reference

#include <options_boost_po.h>

Inheritance diagram for VW::config::options_boost_po:
VW::config::options_i

Public Member Functions

 options_boost_po (int argc, char **argv)
 
 options_boost_po (std::vector< std::string > args)
 
 options_boost_po (options_boost_po &)=delete
 
options_boost_pooperator= (options_boost_po &)=delete
 
virtual void add_and_parse (const option_group_definition &group) override
 
virtual bool was_supplied (const std::string &key) override
 
virtual std::string help () override
 
virtual void check_unregistered () override
 
virtual std::vector< std::shared_ptr< base_option > > get_all_options () override
 
virtual std::shared_ptr< base_optionget_option (const std::string &key) override
 
virtual void insert (const std::string &key, const std::string &value) override
 
virtual void replace (const std::string &key, const std::string &value) override
 
bool try_get_positional_option_token (const std::string &key, std::string &token, int position)
 
template<>
po::typed_value< std::vector< bool > > * convert_to_boost_value (std::shared_ptr< typed_option< bool >> &opt)
 
- Public Member Functions inherited from VW::config::options_i
template<typename T >
typed_option< T > & get_typed_option (const std::string &key)
 
virtual ~options_i ()=default
 

Private Member Functions

template<typename T >
po::typed_value< std::vector< T > > * get_base_boost_value (std::shared_ptr< typed_option< T >> &opt)
 
template<typename T >
po::typed_value< std::vector< T > > * get_base_boost_value (std::shared_ptr< typed_option< std::vector< T >>> &opt)
 
template<typename T >
po::typed_value< std::vector< T > > * convert_to_boost_value (std::shared_ptr< typed_option< T >> &opt)
 
template<typename T >
po::typed_value< std::vector< T > > * convert_to_boost_value (std::shared_ptr< typed_option< std::vector< T >>> &opt)
 
template<typename T >
po::typed_value< std::vector< T > > * add_notifier (std::shared_ptr< typed_option< T >> &opt, po::typed_value< std::vector< T >> *po_value)
 
template<typename T >
po::typed_value< std::vector< T > > * add_notifier (std::shared_ptr< typed_option< std::vector< T >>> &opt, po::typed_value< std::vector< T >> *po_value)
 
template<typename T >
bool add_if_t (std::shared_ptr< base_option > opt, po::options_description &options_description)
 
void add_to_description (std::shared_ptr< base_option > opt, po::options_description &options_description)
 
template<typename TTypes >
void add_to_description_impl (std::shared_ptr< base_option > opt, po::options_description &options_description)
 
template<typename T >
void add_to_description (std::shared_ptr< typed_option< T >> opt, po::options_description &options_description)
 
template<>
void add_to_description_impl (std::shared_ptr< base_option >, po::options_description &)
 
template<>
void add_to_description_impl (std::shared_ptr< base_option > opt, po::options_description &options_description)
 

Private Attributes

std::map< std::string, std::shared_ptr< base_option > > m_options
 
std::vector< std::string > m_command_line
 
std::stringstream m_help_stringstream
 
std::set< std::string > m_supplied_options
 
std::set< std::string > m_ignore_supplied
 
po::options_description master_description
 
std::set< std::string > m_defined_options
 

Detailed Description

Definition at line 40 of file options_boost_po.h.

Constructor & Destructor Documentation

◆ options_boost_po() [1/3]

VW::config::options_boost_po::options_boost_po ( int  argc,
char **  argv 
)
inline

Definition at line 42 of file options_boost_po.h.

42 : options_boost_po(std::vector<std::string>(argv + 1, argv + argc)) {}
options_boost_po(int argc, char **argv)

◆ options_boost_po() [2/3]

VW::config::options_boost_po::options_boost_po ( std::vector< std::string >  args)
inline

Definition at line 44 of file options_boost_po.h.

44 : m_command_line(args) {}
std::vector< std::string > m_command_line

◆ options_boost_po() [3/3]

VW::config::options_boost_po::options_boost_po ( options_boost_po )
delete

Member Function Documentation

◆ add_and_parse()

void options_boost_po::add_and_parse ( const option_group_definition group)
overridevirtual

Implements VW::config::options_i.

Definition at line 48 of file options_boost_po.cc.

References add_to_description(), is_number(), m_command_line, m_defined_options, m_help_stringstream, m_ignore_supplied, VW::config::option_group_definition::m_name, VW::config::option_group_definition::m_options, m_options, m_supplied_options, THROW, THROW_EX, and VW::vw_exception::what().

49 {
50  po::options_description new_options(group.m_name);
51 
52  for (auto opt_ptr : group.m_options)
53  {
54  add_to_description(opt_ptr, new_options);
55  m_defined_options.insert(opt_ptr->m_name);
56  m_defined_options.insert(opt_ptr->m_short_name);
57  m_defined_options.insert("-" + opt_ptr->m_short_name);
58 
59  // The last definition is kept. There was a bug where using .insert at a later pointer changed the command line but
60  // the previously defined option's default value was serialized into the model. This resolves that state info.
61  m_options[opt_ptr->m_name] = opt_ptr;
62  }
63 
64  // Add the help for the given options.
65  new_options.print(m_help_stringstream);
66 
67  try
68  {
69  po::variables_map vm;
70  auto parsed_options = po::command_line_parser(m_command_line)
71  .options(new_options)
72  .style(po::command_line_style::default_style ^ po::command_line_style::allow_guessing)
73  .allow_unregistered()
74  .run();
75 
76  for (auto const& option : parsed_options.options)
77  {
78  // If the supplied option is interpreted as a number, then ignore it. There are no options like this and it is
79  // just a false positive.
80  if (is_number(option.string_key))
81  {
82  m_ignore_supplied.insert(option.string_key);
83  }
84 
85  m_supplied_options.insert(option.string_key);
86 
87  // If a std::string is later determined to be a value the erase it. This happens for negative numbers "-2"
88  for (auto& val : option.value)
89  {
90  m_ignore_supplied.insert(val);
91  }
92 
93  // Parsed options can contain short options in the form -k, we can only check these as the group definitions come
94  // in.
95  if (option.string_key.length() > 0 && option.string_key[0] == '-')
96  {
97  auto short_name = option.string_key.substr(1);
98  for (const auto& opt_ptr : group.m_options)
99  {
100  if (opt_ptr->m_short_name == short_name)
101  {
102  m_supplied_options.insert(short_name);
103  }
104  }
105  }
106  }
107 
108  po::store(parsed_options, vm);
109  po::notify(vm);
110  }
111  catch (boost::exception_detail::clone_impl<
112  boost::exception_detail::error_info_injector<boost::program_options::invalid_option_value>>& ex)
113  {
115  }
116  catch (boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast>>& ex)
117  {
119  }
120  catch (boost::exception_detail::clone_impl<
121  boost::exception_detail::error_info_injector<boost::program_options::ambiguous_option>>& ex)
122  {
123  THROW(ex.what());
124  }
125  catch (boost::program_options::ambiguous_option& ex)
126  {
127  THROW(ex.what());
128  }
129 }
std::vector< std::shared_ptr< base_option > > m_options
Definition: options.h:104
#define THROW_EX(ex, args)
Definition: vw_exception.h:188
std::vector< std::string > m_command_line
const char * what() const noexcept override
Definition: vw_exception.cc:35
void add_to_description(std::shared_ptr< base_option > opt, po::options_description &options_description)
std::set< std::string > m_ignore_supplied
bool is_number(const std::string &s)
std::set< std::string > m_supplied_options
std::map< std::string, std::shared_ptr< base_option > > m_options
std::set< std::string > m_defined_options
std::stringstream m_help_stringstream
#define THROW(args)
Definition: vw_exception.h:181

◆ add_if_t()

template<typename T >
bool VW::config::options_boost_po::add_if_t ( std::shared_ptr< base_option opt,
po::options_description &  options_description 
)
private

Definition at line 247 of file options_boost_po.h.

248 {
249  if (opt->m_type_hash == typeid(T).hash_code())
250  {
251  auto typed = std::dynamic_pointer_cast<typed_option<T>>(opt);
252  add_to_description(typed, options_description);
253  return true;
254  }
255 
256  return false;
257 }
void add_to_description(std::shared_ptr< base_option > opt, po::options_description &options_description)

◆ add_notifier() [1/2]

template<typename T >
po::typed_value< std::vector< T > > * VW::config::options_boost_po::add_notifier ( std::shared_ptr< typed_option< T >> &  opt,
po::typed_value< std::vector< T >> *  po_value 
)
private

Definition at line 214 of file options_boost_po.h.

References THROW_EX.

Referenced by convert_to_boost_value().

216 {
217  return po_value->notifier([opt](std::vector<T> final_arguments) {
218  T first = final_arguments[0];
219  for (auto const& item : final_arguments)
220  {
221  if (item != first)
222  {
223  std::stringstream ss;
224  ss << "Disagreeing option values for '" << opt->m_name << "': '" << first << "' vs '" << item << "'";
226  }
227  }
228 
229  // Set the value for the listening location.
230  opt->m_location = first;
231  opt->value(first);
232  });
233 }
#define THROW_EX(ex, args)
Definition: vw_exception.h:188

◆ add_notifier() [2/2]

template<typename T >
po::typed_value< std::vector< T > > * VW::config::options_boost_po::add_notifier ( std::shared_ptr< typed_option< std::vector< T >>> &  opt,
po::typed_value< std::vector< T >> *  po_value 
)
private

Definition at line 236 of file options_boost_po.h.

238 {
239  return po_value->notifier([opt](std::vector<T> final_arguments) {
240  // Set the value for the listening location.
241  opt->m_location = final_arguments;
242  opt->value(final_arguments);
243  });
244 }

◆ add_to_description() [1/2]

void options_boost_po::add_to_description ( std::shared_ptr< base_option opt,
po::options_description &  options_description 
)
private

Definition at line 42 of file options_boost_po.cc.

Referenced by add_and_parse().

44 {
45  add_to_description_impl<supported_options_types>(std::move(opt), options_description);
46 }

◆ add_to_description() [2/2]

template<typename T >
void VW::config::options_boost_po::add_to_description ( std::shared_ptr< typed_option< T >>  opt,
po::options_description &  options_description 
)
private

Definition at line 264 of file options_boost_po.h.

266 {
267  std::string boost_option_name = opt->m_name;
268  if (opt->m_short_name != "")
269  {
270  boost_option_name += ",";
271  boost_option_name += opt->m_short_name;
272  }
273  options_description.add_options()(boost_option_name.c_str(), convert_to_boost_value(opt), opt->m_help.c_str());
274 
275  if (m_defined_options.count(opt->m_name) == 0)
276  {
277  // TODO may need to add noop notifier here.
278  master_description.add_options()(boost_option_name.c_str(), convert_to_boost_value(opt), "");
279  }
280 }
po::typed_value< std::vector< T > > * convert_to_boost_value(std::shared_ptr< typed_option< T >> &opt)
po::options_description master_description
std::set< std::string > m_defined_options

◆ add_to_description_impl() [1/3]

template<typename TTypes >
void VW::config::options_boost_po::add_to_description_impl ( std::shared_ptr< base_option opt,
po::options_description &  options_description 
)
inlineprivate

Definition at line 139 of file options_boost_po.h.

140  {
141  if (add_if_t<typename TTypes::head>(opt, options_description))
142  {
143  return;
144  }
145  add_to_description_impl<typename TTypes::tail>(opt, options_description);
146  }

◆ add_to_description_impl() [2/3]

template<>
void VW::config::options_boost_po::add_to_description_impl ( std::shared_ptr< base_option ,
po::options_description &   
)
private

Definition at line 180 of file options_boost_po.cc.

References THROW.

181 {
182  THROW("That is an unsupported option type.");
183 }
#define THROW(args)
Definition: vw_exception.h:181

◆ add_to_description_impl() [3/3]

template<>
void VW::config::options_boost_po::add_to_description_impl ( std::shared_ptr< base_option opt,
po::options_description &  options_description 
)
private

◆ check_unregistered()

void options_boost_po::check_unregistered ( )
overridevirtual

Implements VW::config::options_i.

Definition at line 168 of file options_boost_po.cc.

References m_defined_options, m_ignore_supplied, m_supplied_options, and THROW_EX.

169 {
170  for (auto const& supplied : m_supplied_options)
171  {
172  if (m_defined_options.count(supplied) == 0 && m_ignore_supplied.count(supplied) == 0)
173  {
174  THROW_EX(VW::vw_unrecognised_option_exception, "unrecognised option '--" << supplied << "'");
175  }
176  }
177 }
#define THROW_EX(ex, args)
Definition: vw_exception.h:188
std::set< std::string > m_ignore_supplied
std::set< std::string > m_supplied_options
std::set< std::string > m_defined_options

◆ convert_to_boost_value() [1/3]

po::typed_value< std::vector< bool > > * VW::config::options_boost_po::convert_to_boost_value ( std::shared_ptr< typed_option< bool >> &  opt)

Definition at line 26 of file options_boost_po.cc.

References add_notifier(), get_base_boost_value(), and THROW.

27 {
28  auto value = get_base_boost_value(opt);
29 
30  if (opt->default_value_supplied())
31  {
32  THROW("Using a bool option type acts as a switch, no explicit default value is allowed.")
33  }
34 
35  value->default_value({false});
36  value->zero_tokens();
37  value->implicit_value({true});
38 
39  return add_notifier(opt, value);
40 }
po::typed_value< std::vector< T > > * add_notifier(std::shared_ptr< typed_option< T >> &opt, po::typed_value< std::vector< T >> *po_value)
po::typed_value< std::vector< T > > * get_base_boost_value(std::shared_ptr< typed_option< T >> &opt)
#define THROW(args)
Definition: vw_exception.h:181

◆ convert_to_boost_value() [2/3]

template<typename T >
po::typed_value< std::vector< T > > * VW::config::options_boost_po::convert_to_boost_value ( std::shared_ptr< typed_option< T >> &  opt)
private

Definition at line 198 of file options_boost_po.h.

199 {
200  return get_base_boost_value(opt);
201 }
po::typed_value< std::vector< T > > * get_base_boost_value(std::shared_ptr< typed_option< T >> &opt)

◆ convert_to_boost_value() [3/3]

template<typename T >
po::typed_value< std::vector< T > > * VW::config::options_boost_po::convert_to_boost_value ( std::shared_ptr< typed_option< std::vector< T >>> &  opt)
private

Definition at line 204 of file options_boost_po.h.

206 {
207  return get_base_boost_value(opt)->multitoken();
208 }
po::typed_value< std::vector< T > > * get_base_boost_value(std::shared_ptr< typed_option< T >> &opt)

◆ get_all_options()

std::vector< std::shared_ptr< base_option > > options_boost_po::get_all_options ( )
overridevirtual

Implements VW::config::options_i.

Definition at line 146 of file options_boost_po.cc.

References m_options.

147 {
148  std::vector<std::shared_ptr<base_option>> output_values;
149 
150  std::transform(m_options.begin(), m_options.end(), std::back_inserter(output_values),
151  [](std::pair<const std::string, std::shared_ptr<base_option>>& kv) { return kv.second; });
152 
153  return output_values;
154 }
std::map< std::string, std::shared_ptr< base_option > > m_options

◆ get_base_boost_value() [1/2]

template<typename T >
po::typed_value< std::vector< T > > * VW::config::options_boost_po::get_base_boost_value ( std::shared_ptr< typed_option< T >> &  opt)
private

Definition at line 171 of file options_boost_po.h.

Referenced by convert_to_boost_value().

172 {
173  auto value = po::value<std::vector<T>>();
174 
175  if (opt->default_value_supplied())
176  {
177  value->default_value({opt->default_value()});
178  }
179 
180  return add_notifier(opt, value)->composing();
181 }
po::typed_value< std::vector< T > > * add_notifier(std::shared_ptr< typed_option< T >> &opt, po::typed_value< std::vector< T >> *po_value)

◆ get_base_boost_value() [2/2]

template<typename T >
po::typed_value< std::vector< T > > * VW::config::options_boost_po::get_base_boost_value ( std::shared_ptr< typed_option< std::vector< T >>> &  opt)
private

Definition at line 184 of file options_boost_po.h.

186 {
187  auto value = po::value<std::vector<T>>();
188 
189  if (opt->default_value_supplied())
190  {
191  value->default_value(opt->default_value());
192  }
193 
194  return add_notifier(opt, value)->composing();
195 }
po::typed_value< std::vector< T > > * add_notifier(std::shared_ptr< typed_option< T >> &opt, po::typed_value< std::vector< T >> *po_value)

◆ get_option()

std::shared_ptr< base_option > VW::config::options_boost_po::get_option ( const std::string &  key)
overridevirtual

Implements VW::config::options_i.

Definition at line 156 of file options_boost_po.cc.

References m_options.

157 {
158  auto it = m_options.find(key);
159  if (it != m_options.end())
160  {
161  return it->second;
162  }
163 
164  throw std::out_of_range(key + " was not found.");
165 }
std::map< std::string, std::shared_ptr< base_option > > m_options

◆ help()

std::string options_boost_po::help ( )
overridevirtual

Implements VW::config::options_i.

Definition at line 144 of file options_boost_po.cc.

References m_help_stringstream.

144 { return m_help_stringstream.str(); }
std::stringstream m_help_stringstream

◆ insert()

virtual void VW::config::options_boost_po::insert ( const std::string &  key,
const std::string &  value 
)
inlineoverridevirtual

Implements VW::config::options_i.

Definition at line 56 of file options_boost_po.h.

57  {
58  m_command_line.push_back("--" + key);
59  if (value != "")
60  {
61  m_command_line.push_back(value);
62  }
63  }
std::vector< std::string > m_command_line

◆ operator=()

options_boost_po& VW::config::options_boost_po::operator= ( options_boost_po )
delete

◆ replace()

virtual void VW::config::options_boost_po::replace ( const std::string &  key,
const std::string &  value 
)
inlineoverridevirtual

Implements VW::config::options_i.

Definition at line 66 of file options_boost_po.h.

References recall_tree_ns::find(), and THROW.

67  {
68  auto full_key = "--" + key;
69  auto it = std::find(m_command_line.begin(), m_command_line.end(), full_key);
70 
71  // Not found, insert instead.
72  if (it == m_command_line.end())
73  {
74  insert(key, value);
75  return;
76  }
77 
78  // Check if it is the final option or the next option is not a value.
79  if (it + 1 == m_command_line.end() || (*(it + 1)).find("--") != std::string::npos)
80  {
81  THROW(key + " option does not have a value.");
82  }
83 
84  // Actually replace the value.
85  *(it + 1) = value;
86  }
std::vector< std::string > m_command_line
virtual void insert(const std::string &key, const std::string &value) override
node_pred * find(recall_tree &b, uint32_t cn, example &ec)
Definition: recall_tree.cc:126
#define THROW(args)
Definition: vw_exception.h:181

◆ try_get_positional_option_token()

bool VW::config::options_boost_po::try_get_positional_option_token ( const std::string &  key,
std::string &  token,
int  position 
)
inline

Definition at line 89 of file options_boost_po.h.

90  {
91  po::positional_options_description p;
92  p.add(key.c_str(), position);
93  po::parsed_options pos = po::command_line_parser(m_command_line)
94  .style(po::command_line_style::default_style ^ po::command_line_style::allow_guessing)
95  .options(master_description)
96  .allow_unregistered()
97  .positional(p)
98  .run();
99 
100  auto it = std::find_if(pos.options.begin(), pos.options.end(),
101  [&key](boost::program_options::option& option) { return option.string_key == key; });
102 
103  if (it != pos.options.end() && (*it).value.size() > 0)
104  {
105  token = (*it).value.at(0);
106  return true;
107  }
108 
109  return false;
110  }
std::vector< std::string > m_command_line
po::options_description master_description

◆ was_supplied()

bool options_boost_po::was_supplied ( const std::string &  key)
overridevirtual

Implements VW::config::options_i.

Definition at line 131 of file options_boost_po.cc.

References recall_tree_ns::find(), m_command_line, and m_supplied_options.

132 {
133  // Best check, only valid after options parsed.
134  if (m_supplied_options.count(key) > 0)
135  {
136  return true;
137  }
138 
139  // Basic check, std::string match against command line.
140  auto it = std::find(m_command_line.begin(), m_command_line.end(), std::string("--" + key));
141  return it != m_command_line.end();
142 }
std::vector< std::string > m_command_line
std::set< std::string > m_supplied_options
node_pred * find(recall_tree &b, uint32_t cn, example &ec)
Definition: recall_tree.cc:126

Member Data Documentation

◆ m_command_line

std::vector<std::string> VW::config::options_boost_po::m_command_line
private

Definition at line 154 of file options_boost_po.h.

Referenced by add_and_parse(), and was_supplied().

◆ m_defined_options

std::set<std::string> VW::config::options_boost_po::m_defined_options
private

Definition at line 167 of file options_boost_po.h.

Referenced by add_and_parse(), and check_unregistered().

◆ m_help_stringstream

std::stringstream VW::config::options_boost_po::m_help_stringstream
private

Definition at line 156 of file options_boost_po.h.

Referenced by add_and_parse(), and help().

◆ m_ignore_supplied

std::set<std::string> VW::config::options_boost_po::m_ignore_supplied
private

Definition at line 162 of file options_boost_po.h.

Referenced by add_and_parse(), and check_unregistered().

◆ m_options

std::map<std::string, std::shared_ptr<base_option> > VW::config::options_boost_po::m_options
private

Definition at line 152 of file options_boost_po.h.

Referenced by add_and_parse(), get_all_options(), and get_option().

◆ m_supplied_options

std::set<std::string> VW::config::options_boost_po::m_supplied_options
private

Definition at line 159 of file options_boost_po.h.

Referenced by add_and_parse(), check_unregistered(), and was_supplied().

◆ master_description

po::options_description VW::config::options_boost_po::master_description
private

Definition at line 164 of file options_boost_po.h.


The documentation for this struct was generated from the following files: