Option++  2.0
C++ library for reading command-line options
parser_result.hpp
Go to the documentation of this file.
1 /* Option++ -- read command-line program options
2  * Copyright (C) 2017-2020 Greg Kikola.
3  *
4  * This file is part of Option++.
5  *
6  * Option++ is free software: you can redistribute it and/or modify
7  * it under the terms of the Boost Software License version 1.0.
8  *
9  * Option++ is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * Boost Software License for more details.
13  *
14  * You should have received a copy of the Boost Software License
15  * along with Option++. If not, see
16  * <https://www.boost.org/LICENSE_1_0.txt>.
17  */
18 /* Written by Greg Kikola <[email protected]>. */
19 
25 #ifndef OPTIONPP_PARSER_RESULT_HPP
26 #define OPTIONPP_PARSER_RESULT_HPP
27 
28 #include <cstddef>
29 #include <initializer_list>
30 #include <iterator>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 #include <optionpp/error.hpp>
35 #include <optionpp/option.hpp>
36 
37 namespace optionpp {
38 
45  struct parsed_entry {
49  parsed_entry() noexcept {};
50 
62  explicit parsed_entry(const std::string& original_text,
63  bool is_option = false,
64  const std::string& long_name = "",
65  char short_name = '\0',
66  const std::string& argument = "")
69 
82  std::string original_text;
83 
93 
103  bool is_option{false};
104 
111  std::string long_name;
112 
119  char short_name{'\0'};
120 
127  std::string argument;
128 
133  const option* opt_info{nullptr};
134  };
135 
153  public:
154 
162  using container_type = std::vector<value_type>;
167  using size_type = container_type::size_type;
171  using iterator = container_type::iterator;
175  using const_iterator = container_type::const_iterator;
179  using reverse_iterator = container_type::reverse_iterator;
183  using const_reverse_iterator = container_type::const_reverse_iterator;
184 
190  parser_result() noexcept {}
195  parser_result(const std::initializer_list<value_type>& il)
196  : m_entries{il} {}
203  template <typename InputIt>
204  parser_result(InputIt first, InputIt last) : m_entries{first, last} {}
205 
210  void push_back(const value_type& entry) { m_entries.push_back(entry); }
214  void push_back(value_type&& entry) { m_entries.push_back(std::move(entry)); }
215 
219  void clear() noexcept { m_entries.clear(); }
220 
230  size_type size() const noexcept { return m_entries.size(); }
235  bool empty() const noexcept { return m_entries.empty(); }
236 
241  iterator begin() noexcept { return m_entries.begin(); }
245  const_iterator begin() const noexcept { return cbegin(); }
250  iterator end() noexcept { return m_entries.end(); }
254  const_iterator end() const noexcept { return cend(); }
255 
260  const_iterator cbegin() const noexcept { return m_entries.cbegin(); }
265  const_iterator cend() const noexcept { return m_entries.cend(); }
266 
272  reverse_iterator rbegin() noexcept { return m_entries.rbegin(); }
276  const_reverse_iterator rbegin() const noexcept { return crbegin(); }
282  reverse_iterator rend() noexcept { return m_entries.rend(); }
286  const_reverse_iterator rend() const noexcept { return crend(); }
287 
293  const_reverse_iterator crbegin() const noexcept { return m_entries.crbegin(); }
299  const_reverse_iterator crend() const noexcept { return m_entries.crend(); }
300 
308  if (index >= size())
309  throw out_of_range("out of bounds parser_result access",
310  "optionpp::parser_result::at");
311  return (*this)[index];
312  }
316  const value_type& at(size_type index) const {
317  if (index >= size())
318  throw out_of_range("out of bounds parser_result access",
319  "optionpp::parser_result::at");
320  return (*this)[index];
321  }
322 
328  value_type& operator[](size_type index) { return m_entries[index]; }
332  const value_type& operator[](size_type index) const {
333  return m_entries[index];
334  }
335 
342  if (empty())
343  throw out_of_range("out of bounds parser_result access",
344  "optionpp::parser_result::back");
345  return m_entries.back();
346  }
347 
351  const value_type& back() const {
352  if (empty())
353  throw out_of_range("out of bounds parser_result access",
354  "optionpp::parser_result::at");
355  return m_entries.back();
356  }
357 
364  bool is_option_set(const std::string& long_name) const noexcept;
371  bool is_option_set(char short_name) const noexcept;
372 
382  std::string get_argument(std::string long_name) const noexcept;
392  std::string get_argument(char short_name) const noexcept;
393 
394  private:
395  container_type m_entries; //< The internal container of `parsed_entry` instances.
396  };
397 
398 } // End namespace
399 
400 #endif
optionpp::parsed_entry::long_name
std::string long_name
The long name of the option which this parsed_entry represents.
Definition: parser_result.hpp:111
optionpp::parser_result::crbegin
const_reverse_iterator crbegin() const noexcept
Return a const_reverse_iterator to the beginning.
Definition: parser_result.hpp:293
optionpp::parser_result::parser_result
parser_result(InputIt first, InputIt last)
Construct from a sequence.
Definition: parser_result.hpp:204
optionpp::out_of_range
Exception indicating out of range access.
Definition: error.hpp:59
optionpp::parser_result::cbegin
const_iterator cbegin() const noexcept
Return a const_iterator to the beginning of the container.
Definition: parser_result.hpp:260
optionpp::option
Describes a valid program command-line option.
Definition: option.hpp:67
optionpp::parser_result::clear
void clear() noexcept
Erase all data entries currently stored.
Definition: parser_result.hpp:219
optionpp::parsed_entry
Holds data parsed from the command line.
Definition: parser_result.hpp:45
optionpp::parser_result::operator[]
const value_type & operator[](size_type index) const
Subscript operator.
Definition: parser_result.hpp:332
optionpp::parser_result::at
const value_type & at(size_type index) const
Range-checked subscript.
Definition: parser_result.hpp:316
optionpp::parser_result::parser_result
parser_result(const std::initializer_list< value_type > &il)
Construct from an initializer list.
Definition: parser_result.hpp:195
optionpp::parser_result::begin
const_iterator begin() const noexcept
Return a const_iterator to the beginning of the container.
Definition: parser_result.hpp:245
optionpp::parser_result::crend
const_reverse_iterator crend() const noexcept
Return a const_reverse_iterator to the end.
Definition: parser_result.hpp:299
optionpp::parser_result::empty
bool empty() const noexcept
Return whether the container is empty.
Definition: parser_result.hpp:235
optionpp::parser_result::cend
const_iterator cend() const noexcept
Return a const_iterator to the end of the container.
Definition: parser_result.hpp:265
optionpp::parsed_entry::argument
std::string argument
The argument that was passed to the option, if any.
Definition: parser_result.hpp:127
optionpp::parser_result::back
value_type & back()
Access last entry in the container.
Definition: parser_result.hpp:341
optionpp::parser_result::const_iterator
container_type::const_iterator const_iterator
Constant iterator type.
Definition: parser_result.hpp:175
optionpp::parsed_entry::parsed_entry
parsed_entry() noexcept
Default constructor.
Definition: parser_result.hpp:49
optionpp::parser_result::end
const_iterator end() const noexcept
Return a const_iterator to the end of the container.
Definition: parser_result.hpp:254
optionpp::parser_result::is_option_set
bool is_option_set(const std::string &long_name) const noexcept
Returns whether the specified option is set.
Definition: parser_result.cpp:32
optionpp::parser_result::at
value_type & at(size_type index)
Range-checked subscript.
Definition: parser_result.hpp:307
optionpp::parser_result::size_type
container_type::size_type size_type
Unsigned integer type (usually std::size_t) that can hold container size.
Definition: parser_result.hpp:167
optionpp::parsed_entry::parsed_entry
parsed_entry(const std::string &original_text, bool is_option=false, const std::string &long_name="", char short_name='\0', const std::string &argument="")
Constructor.
Definition: parser_result.hpp:62
optionpp::parser_result::get_argument
std::string get_argument(std::string long_name) const noexcept
Get the argument for the specified option.
Definition: parser_result.cpp:52
optionpp::parsed_entry::original_without_argument
std::string original_without_argument
The original text used on the command line but without any option argument.
Definition: parser_result.hpp:92
optionpp::parsed_entry::original_text
std::string original_text
The original text used on the command line.
Definition: parser_result.hpp:82
optionpp::parser_result::push_back
void push_back(value_type &&entry)
Add a parsed_entry to the back of the container.
Definition: parser_result.hpp:214
optionpp::parser_result::push_back
void push_back(const value_type &entry)
Add a parsed_entry to the back of the container.
Definition: parser_result.hpp:210
optionpp
Library namespace.
Definition: error.hpp:31
optionpp::parser_result::back
const value_type & back() const
Access last entry in the container.
Definition: parser_result.hpp:351
optionpp::parser_result::end
iterator end() noexcept
Return an iterator to the end of the container.
Definition: parser_result.hpp:250
optionpp::parser_result
Holds data that was parsed from the program command line.
Definition: parser_result.hpp:152
optionpp::parser_result::begin
iterator begin() noexcept
Return an iterator to the beginning of the container.
Definition: parser_result.hpp:241
optionpp::parser_result::rend
const_reverse_iterator rend() const noexcept
Return a const_reverse_iterator to the end.
Definition: parser_result.hpp:286
optionpp::parser_result::iterator
container_type::iterator iterator
Plain iterator type.
Definition: parser_result.hpp:171
optionpp::parser_result::rbegin
const_reverse_iterator rbegin() const noexcept
Return a const_reverse_iterator to the beginning.
Definition: parser_result.hpp:276
optionpp::parser_result::reverse_iterator
container_type::reverse_iterator reverse_iterator
Reverse iterator type.
Definition: parser_result.hpp:179
optionpp::parser_result::parser_result
parser_result() noexcept
Default constructor.
Definition: parser_result.hpp:190
optionpp::parser_result::size
size_type size() const noexcept
Return the number of data entries.
Definition: parser_result.hpp:230
option.hpp
Header file for option class.
optionpp::parsed_entry::opt_info
const option * opt_info
Pointer to the option instance representing this option, if any.
Definition: parser_result.hpp:133
optionpp::parser_result::rend
reverse_iterator rend() noexcept
Return a reverse_iterator to the end.
Definition: parser_result.hpp:282
optionpp::parsed_entry::is_option
bool is_option
True if this parsed_entry represents a program option, false otherwise.
Definition: parser_result.hpp:103
optionpp::parser_result::container_type
std::vector< value_type > container_type
Type of container used to store the data entries.
Definition: parser_result.hpp:162
optionpp::parsed_entry::short_name
char short_name
The short name of the option which this parsed_entry represents.
Definition: parser_result.hpp:119
optionpp::parser_result::operator[]
value_type & operator[](size_type index)
Subscript operator.
Definition: parser_result.hpp:328
error.hpp
Header file for exception classes.
optionpp::parser_result::const_reverse_iterator
container_type::const_reverse_iterator const_reverse_iterator
Constant reverse iterator type.
Definition: parser_result.hpp:183
optionpp::parser_result::rbegin
reverse_iterator rbegin() noexcept
Return a reverse_iterator to the beginning.
Definition: parser_result.hpp:272