Vowpal Wabbit
opts.cc
Go to the documentation of this file.
1 #include "opts.h"
2 
3 #include <cstdlib>
4 #include <cctype>
5 
6 namespace vw_slim
7 {
8 // && used to avoid constant string copying
9 void find_opt(std::string const& command_line_args, std::string arg_name, std::vector<std::string>& out_values)
10 {
11  // append space to search for '--quadratic '
12  arg_name += ' ';
13 
14  // support -q ab -v -q cd
15  for (size_t start = 0; start < command_line_args.size();)
16  {
17  auto idx = command_line_args.find(arg_name, start);
18  if (idx == std::string::npos)
19  return; // no more occurences found, exit
20 
21  auto idx_after_arg = idx + arg_name.size();
22 
23  // skip all white space
24  for (; idx_after_arg < command_line_args.size() && std::isspace(command_line_args[idx_after_arg]); ++idx_after_arg)
25  ;
26 
27  if (idx_after_arg == command_line_args.size())
28  return;
29 
30  if (command_line_args[idx_after_arg] == '-' &&
31  // make sure we allow -5.2 (negative numbers)
32  !(idx_after_arg + 1 < command_line_args.size() &&
33  (command_line_args[idx_after_arg + 1] >= '0' && command_line_args[idx_after_arg + 1] <= '9')))
34  {
35  start = idx_after_arg;
36  continue; // next option found
37  }
38 
39  // find next non-white space character
40  auto idx_after_value = idx_after_arg;
41  for (; idx_after_value < command_line_args.size() && !std::isspace(command_line_args[idx_after_value]);
42  ++idx_after_value)
43  ;
44 
45  auto value_size = idx_after_value - idx_after_arg;
46  if (value_size > 0)
47  out_values.push_back(command_line_args.substr(idx_after_arg, value_size));
48 
49  start = idx_after_arg + 1;
50  }
51 }
52 
53 std::vector<std::string> find_opt(std::string const& command_line_args, std::string arg_name)
54 {
55  std::vector<std::string> values;
56  find_opt(command_line_args, arg_name, values);
57  return values;
58 }
59 
60 template <typename T, typename S, S (*F)(const char*)>
61 bool find_opt_parse(std::string const& command_line_args, std::string arg_name, T& value)
62 {
63  std::vector<std::string> opts = find_opt(command_line_args, arg_name);
64 
65  if (opts.size() != 1)
66  return false;
67 
68  value = (T)F(opts[0].c_str());
69 
70  return true;
71 }
72 
73 bool find_opt_float(std::string const& command_line_args, std::string arg_name, float& value)
74 {
75  return find_opt_parse<float, double, atof>(command_line_args, arg_name, value);
76 }
77 
78 bool find_opt_int(std::string const& command_line_args, std::string arg_name, int& value)
79 {
80  return find_opt_parse<int, int, atoi>(command_line_args, arg_name, value);
81 }
82 } // namespace vw_slim
bool find_opt_int(std::string const &command_line_args, std::string arg_name, int &value)
Definition: opts.cc:78
void find_opt(std::string const &command_line_args, std::string arg_name, std::vector< std::string > &out_values)
Definition: opts.cc:9
bool find_opt_parse(std::string const &command_line_args, std::string arg_name, T &value)
Definition: opts.cc:61
bool find_opt_float(std::string const &command_line_args, std::string arg_name, float &value)
Definition: opts.cc:73