Option++  2.0
C++ library for reading command-line options
option_group.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_OPTION_GROUP_HPP
26 #define OPTIONPP_OPTION_GROUP_HPP
27 
28 #include <string>
29 #include <utility>
30 #include <vector>
31 #include <optionpp/option.hpp>
32 
33 namespace optionpp {
34 
41  class option_group {
42  public:
43 
47  using value_type = option;
51  using container_type = std::vector<option>;
55  using size_type = container_type::size_type;
59  using iterator = container_type::iterator;
63  using const_iterator = container_type::const_iterator;
67  using reverse_iterator = container_type::reverse_iterator;
71  using const_reverse_iterator = container_type::const_reverse_iterator;
72 
76  option_group() noexcept {}
81  option_group(const std::string& name) : m_name{name} {}
95  template <typename InputIt>
96  option_group(const std::string& name,
97  InputIt first, InputIt last)
98  : m_name{name}, m_options{first, last} {}
99 
104  const std::string& name() const noexcept { return m_name; }
105 
121  option& add_option(const option& opt = option{}) {
122  m_options.push_back(opt);
123  return m_options.back();
124  }
135  option& add_option(const std::string& long_name,
136  char short_name = '\0',
137  const std::string& description = "",
138  const std::string& arg_name = "",
139  bool arg_required = false);
140 
145  size_type size() const noexcept { return m_options.size(); }
150  bool empty() const noexcept { return m_options.empty(); }
151 
156  iterator begin() noexcept { return m_options.begin(); }
160  const_iterator begin() const noexcept { return cbegin(); }
166  iterator end() noexcept { return m_options.end(); }
170  const_iterator end() const noexcept { return cend(); }
171 
176  const_iterator cbegin() const noexcept { return m_options.cbegin(); }
182  const_iterator cend() const noexcept { return m_options.cend(); }
183 
189  reverse_iterator rbegin() noexcept { return m_options.rbegin(); }
193  const_reverse_iterator rbegin() const noexcept { return crbegin(); }
199  reverse_iterator rend() noexcept { return m_options.rend(); }
203  const_reverse_iterator rend() const noexcept { return crend(); }
204 
210  const_reverse_iterator crbegin() const noexcept { return m_options.crbegin(); }
216  const_reverse_iterator crend() const noexcept { return m_options.crend(); }
217 
227  iterator find(const std::string& long_name);
228 
238  const_iterator find(const std::string& long_name) const;
239 
249  iterator find(char short_name);
250 
260  const_iterator find(char short_name) const;
261 
270  void sort();
271 
281  option& operator[](const std::string long_name);
291  option& operator[](char short_name);
292 
293  private:
294  std::string m_name; //< Group name.
295  container_type m_options; //< Collection of program options.
296  };
297 
298 } // End namespace
299 
300 #endif
optionpp::option_group
Holds a group of program options.
Definition: option_group.hpp:41
optionpp::option_group::rend
const_reverse_iterator rend() const noexcept
Return a const_reverse_iterator to the end.
Definition: option_group.hpp:203
optionpp::option_group::name
const std::string & name() const noexcept
Returns the name of the group.
Definition: option_group.hpp:104
optionpp::option_group::rbegin
reverse_iterator rbegin() noexcept
Return a reverse_iterator to the beginning.
Definition: option_group.hpp:189
optionpp::option
Describes a valid program command-line option.
Definition: option.hpp:67
optionpp::option_group::rend
reverse_iterator rend() noexcept
Return a reverse_iterator to the end.
Definition: option_group.hpp:199
optionpp::option_group::add_option
option & add_option(const option &opt=option{})
Add a program option to the group.
Definition: option_group.hpp:121
optionpp::option_group::operator[]
option & operator[](const std::string long_name)
Subscript operator.
Definition: option_group.cpp:41
optionpp::option_group::option_group
option_group(const std::string &name, InputIt first, InputIt last)
Construct from a sequence.
Definition: option_group.hpp:96
optionpp::option_group::begin
iterator begin() noexcept
Return an iterator to the first option in the group.
Definition: option_group.hpp:156
optionpp::option_group::reverse_iterator
container_type::reverse_iterator reverse_iterator
Reverse iterator type.
Definition: option_group.hpp:67
optionpp::option_group::begin
const_iterator begin() const noexcept
Return a const_iterator to the first option.
Definition: option_group.hpp:160
optionpp::option_group::empty
bool empty() const noexcept
Return whether the container is empty.
Definition: option_group.hpp:150
optionpp::option_group::rbegin
const_reverse_iterator rbegin() const noexcept
Return a const_reverse_iterator to the beginning.
Definition: option_group.hpp:193
optionpp::option_group::option_group
option_group(const std::string &name)
Construct empty named group.
Definition: option_group.hpp:81
optionpp::option_group::end
const_iterator end() const noexcept
Return a const_iterator to the end of the container.
Definition: option_group.hpp:170
optionpp::option_group::iterator
container_type::iterator iterator
Plain iterator type.
Definition: option_group.hpp:59
optionpp::option_group::cend
const_iterator cend() const noexcept
Return a const_iterator to the end of the container.
Definition: option_group.hpp:182
optionpp::option_group::container_type
std::vector< option > container_type
Type of container used to hold the options.
Definition: option_group.hpp:51
optionpp::option_group::const_reverse_iterator
container_type::const_reverse_iterator const_reverse_iterator
Constant reverse iterator type.
Definition: option_group.hpp:71
optionpp::option_group::size_type
container_type::size_type size_type
Type used to represent the size of the container.
Definition: option_group.hpp:55
optionpp
Library namespace.
Definition: error.hpp:31
optionpp::option_group::sort
void sort()
Sorts the options by name.
Definition: option_group.cpp:77
optionpp::option_group::crend
const_reverse_iterator crend() const noexcept
Return a const_reverse_iterator to the end.
Definition: option_group.hpp:216
optionpp::option_group::end
iterator end() noexcept
Return an iterator to one beyond the last option in the group.
Definition: option_group.hpp:166
optionpp::option_group::option_group
option_group() noexcept
Default constructor.
Definition: option_group.hpp:76
optionpp::option_group::cbegin
const_iterator cbegin() const noexcept
Return a const_iterator to the first option.
Definition: option_group.hpp:176
optionpp::option_group::crbegin
const_reverse_iterator crbegin() const noexcept
Return a const_reverse_iterator to the beginning.
Definition: option_group.hpp:210
optionpp::option_group::find
iterator find(const std::string &long_name)
Search for an option in the group.
Definition: option_group.cpp:57
optionpp::option_group::const_iterator
container_type::const_iterator const_iterator
Constant iterator type.
Definition: option_group.hpp:63
optionpp::option_group::size
size_type size() const noexcept
Return the number of options in the group.
Definition: option_group.hpp:145
option.hpp
Header file for option class.