CBMC
cmdline.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 
10 #ifndef CPROVER_UTIL_CMDLINE_H
11 #define CPROVER_UTIL_CMDLINE_H
12 
13 #include <limits>
14 #include <list>
15 #include <optional>
16 #include <string>
17 #include <vector>
18 
19 class cmdlinet
20 {
21 public:
74  virtual bool parse(int argc, const char **argv, const char *optstring);
75 
76  std::string get_value(char option) const;
77  std::string get_value(const char *option) const;
78 
79  const std::list<std::string> &get_values(const std::string &option) const;
80  const std::list<std::string> &get_values(char option) const;
81 
84  std::list<std::string> get_comma_separated_values(const char *option) const;
85 
86  virtual bool isset(char option) const;
87  virtual bool isset(const char *option) const;
89  virtual void set(const std::string &option, bool value = true);
90  virtual void set(const std::string &option, const std::string &value);
91  virtual void set(const std::string &option, const char *value)
92  {
93  set(option, std::string{value});
94  }
95 
96  virtual void clear();
97 
98  bool has_option(const std::string &option) const
99  {
100  return getoptnr(option).has_value();
101  }
102 
104  {
105  explicit option_namest(const cmdlinet &command_line);
107  {
108  // These types are defined such that the class is compatible with being
109  // treated as an STL iterator. For this to work, they must not be renamed.
110  using iterator_category = std::forward_iterator_tag;
111  using value_type = std::string;
112  using difference_type = std::ptrdiff_t;
113  using pointer = const std::string *;
114  using reference = const std::string &;
115 
117  explicit option_names_iteratort(
118  const cmdlinet *command_line,
119  std::size_t index);
123  operator=(const option_names_iteratort &) = default;
125 
128  const std::string &operator*();
129 
130  bool operator==(const option_names_iteratort &other);
131  bool operator!=(const option_names_iteratort &other);
132 
133  private:
134  const cmdlinet *command_line = nullptr;
135  std::size_t index = std::numeric_limits<std::size_t>::max();
136  bool is_valid_index() const;
137  void goto_next_valid_index();
138  };
141 
142  private:
144  };
145 
148  option_namest option_names() const;
149 
150  typedef std::vector<std::string> argst;
152  std::string unknown_arg;
153 
154  cmdlinet();
155  virtual ~cmdlinet();
156 
157  std::vector<std::string>
158  get_argument_suggestions(const std::string &unknown_argument);
159 
160 protected:
161  struct optiont
162  {
163  bool isset;
164  bool hasval;
165  bool islong;
166  char optchar;
167  std::string optstring;
168  std::list<std::string> values;
169  public:
171  isset(false),
172  hasval(false),
173  islong(false),
174  optchar(0)
175  {}
176  };
177 
182  void parse_optstring(const char *optstring);
183 
188  bool parse_arguments(int argc, const char **argv);
189 
190  std::vector<optiont> options;
191 
192  std::optional<std::size_t> getoptnr(char option) const;
193  std::optional<std::size_t> getoptnr(const std::string &option) const;
194 };
195 
196 #endif // CPROVER_UTIL_CMDLINE_H
std::string get_value(char option) const
Definition: cmdline.cpp:48
virtual void set(const std::string &option, const char *value)
Definition: cmdline.h:91
std::string unknown_arg
Definition: cmdline.h:152
virtual bool isset(char option) const
Definition: cmdline.cpp:30
std::list< std::string > get_comma_separated_values(const char *option) const
Collect all occurrences of option option and split their values on each comma, merging them into a si...
Definition: cmdline.cpp:121
std::optional< std::size_t > getoptnr(char option) const
Definition: cmdline.cpp:135
bool has_option(const std::string &option) const
Definition: cmdline.h:98
cmdlinet()
Definition: cmdline.cpp:16
argst args
Definition: cmdline.h:151
bool parse_arguments(int argc, const char **argv)
Parses a commandline according to a previously parsed optstring and writes the result to cmdlinet::op...
Definition: cmdline.cpp:269
std::vector< optiont > options
Definition: cmdline.h:190
virtual ~cmdlinet()
Definition: cmdline.cpp:20
virtual void set(const std::string &option, bool value=true)
Set option option to value, or true if the value is omitted.
Definition: cmdline.cpp:58
virtual bool parse(int argc, const char **argv, const char *optstring)
Parses a commandline according to a specification given in optstring.
Definition: cmdline.cpp:153
std::vector< std::string > get_argument_suggestions(const std::string &unknown_argument)
Definition: cmdline.cpp:210
void parse_optstring(const char *optstring)
Parses an optstring and writes the result to cmdlinet::options.
Definition: cmdline.cpp:165
option_namest option_names() const
Pseudo-object that can be used to iterate over options in this cmdlinet (should not outlive this)
Definition: cmdline.cpp:161
std::vector< std::string > argst
Definition: cmdline.h:150
virtual void clear()
Definition: cmdline.cpp:24
const std::list< std::string > & get_values(const std::string &option) const
Definition: cmdline.cpp:109
option_names_iteratort(option_names_iteratort &&other)=default
option_names_iteratort & operator=(option_names_iteratort &&)=default
option_names_iteratort & operator=(const option_names_iteratort &)=default
bool operator==(const option_names_iteratort &other)
Definition: cmdline.cpp:366
option_names_iteratort(const option_names_iteratort &other)=default
std::forward_iterator_tag iterator_category
Definition: cmdline.h:110
bool operator!=(const option_names_iteratort &other)
Definition: cmdline.cpp:373
option_names_iteratort end()
Definition: cmdline.cpp:389
option_names_iteratort begin()
Definition: cmdline.cpp:384
option_namest(const cmdlinet &command_line)
Definition: cmdline.cpp:379
const cmdlinet & command_line
Definition: cmdline.h:143
std::string optstring
Definition: cmdline.h:167
std::list< std::string > values
Definition: cmdline.h:168