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  std::optional<std::string> value_opt(char option) const;
80  std::optional<std::string> value_opt(const char *option) const;
81 
82  const std::list<std::string> &get_values(const std::string &option) const;
83  const std::list<std::string> &get_values(char option) const;
84 
87  std::list<std::string> get_comma_separated_values(const char *option) const;
88 
89  virtual bool isset(char option) const;
90  virtual bool isset(const char *option) const;
92  virtual void set(const std::string &option, bool value = true);
93  virtual void set(const std::string &option, const std::string &value);
94  virtual void set(const std::string &option, const char *value)
95  {
96  set(option, std::string{value});
97  }
98 
99  virtual void clear();
100 
101  bool has_option(const std::string &option) const
102  {
103  return getoptnr(option).has_value();
104  }
105 
107  {
108  explicit option_namest(const cmdlinet &command_line);
110  {
111  // These types are defined such that the class is compatible with being
112  // treated as an STL iterator. For this to work, they must not be renamed.
113  using iterator_category = std::forward_iterator_tag;
114  using value_type = std::string;
115  using difference_type = std::ptrdiff_t;
116  using pointer = const std::string *;
117  using reference = const std::string &;
118 
120  explicit option_names_iteratort(
121  const cmdlinet *command_line,
122  std::size_t index);
126  operator=(const option_names_iteratort &) = default;
128 
131  const std::string &operator*();
132 
133  bool operator==(const option_names_iteratort &other);
134  bool operator!=(const option_names_iteratort &other);
135 
136  private:
137  const cmdlinet *command_line = nullptr;
138  std::size_t index = std::numeric_limits<std::size_t>::max();
139  bool is_valid_index() const;
140  void goto_next_valid_index();
141  };
144 
145  private:
147  };
148 
151  option_namest option_names() const;
152 
153  typedef std::vector<std::string> argst;
155  std::string unknown_arg;
156 
157  cmdlinet();
158  virtual ~cmdlinet();
159 
160  std::vector<std::string>
161  get_argument_suggestions(const std::string &unknown_argument);
162 
163 protected:
164  struct optiont
165  {
166  bool isset;
167  bool hasval;
168  bool islong;
169  char optchar;
170  std::string optstring;
171  std::list<std::string> values;
172  public:
174  isset(false),
175  hasval(false),
176  islong(false),
177  optchar(0)
178  {}
179  };
180 
185  void parse_optstring(const char *optstring);
186 
191  bool parse_arguments(int argc, const char **argv);
192 
193  std::vector<optiont> options;
194 
195  std::optional<std::size_t> getoptnr(char option) const;
196  std::optional<std::size_t> getoptnr(const std::string &option) const;
197 };
198 
199 #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:94
std::string unknown_arg
Definition: cmdline.h:155
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:131
std::optional< std::size_t > getoptnr(char option) const
Definition: cmdline.cpp:145
bool has_option(const std::string &option) const
Definition: cmdline.h:101
cmdlinet()
Definition: cmdline.cpp:16
argst args
Definition: cmdline.h:154
std::optional< std::string > value_opt(char option) const
Definition: cmdline.cpp:53
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:279
std::vector< optiont > options
Definition: cmdline.h:193
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:63
virtual bool parse(int argc, const char **argv, const char *optstring)
Parses a commandline according to a specification given in optstring.
Definition: cmdline.cpp:163
std::vector< std::string > get_argument_suggestions(const std::string &unknown_argument)
Definition: cmdline.cpp:220
void parse_optstring(const char *optstring)
Parses an optstring and writes the result to cmdlinet::options.
Definition: cmdline.cpp:175
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:171
std::vector< std::string > argst
Definition: cmdline.h:153
virtual void clear()
Definition: cmdline.cpp:24
const std::list< std::string > & get_values(const std::string &option) const
Definition: cmdline.cpp:119
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:376
option_names_iteratort(const option_names_iteratort &other)=default
std::forward_iterator_tag iterator_category
Definition: cmdline.h:113
bool operator!=(const option_names_iteratort &other)
Definition: cmdline.cpp:383
option_names_iteratort end()
Definition: cmdline.cpp:399
option_names_iteratort begin()
Definition: cmdline.cpp:394
option_namest(const cmdlinet &command_line)
Definition: cmdline.cpp:389
const cmdlinet & command_line
Definition: cmdline.h:146
std::string optstring
Definition: cmdline.h:170
std::list< std::string > values
Definition: cmdline.h:171