Option++  2.0
C++ library for reading command-line options
option.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_HPP
26 #define OPTIONPP_OPTION_HPP
27 
28 #include <string>
29 
30 namespace optionpp {
31 
67  class option {
68  public:
69 
73  enum arg_type { string_arg, //< Indicates a string argument.
74  int_arg, //< Indicates an integer argument.
75  uint_arg, //< Indicates an unsigned int argument.
76  double_arg //< Indicates a floating-point argument.
77  };
78 
84  option() noexcept {}
92  option(char short_name) : m_short_name{short_name} {}
105  option(const std::string& long_name,
106  char short_name = '\0',
107  const std::string& description = "",
108  const std::string& arg_name = "",
109  bool arg_required = false);
110 
117  option& name(const std::string& long_name, char short_name = '\0') {
118  m_long_name = long_name;
119  m_short_name = short_name;
120  return *this;
121  }
122 
131  std::string name() const noexcept {
132  if (!m_long_name.empty())
133  return m_long_name;
134  else if (m_short_name != '\0')
135  return std::string{m_short_name};
136  else
137  return "";
138  }
139 
145  option& long_name(const std::string& name) {
146  m_long_name = name;
147  return *this;
148  }
153  const std::string& long_name() const noexcept { return m_long_name; }
154 
160  option& short_name(char name) noexcept {
161  m_short_name = name;
162  return *this;
163  }
168  char short_name() const noexcept { return m_short_name; }
169 
180  option& argument(const std::string& name,
181  bool required = true);
189  const std::string& argument_name() const noexcept { return m_arg_name; }
194  bool is_argument_required() const noexcept { return m_arg_required; }
199  arg_type argument_type() const noexcept { return m_arg_type; }
200 
211  option& bind_bool(bool* var) noexcept;
218  option& bind_string(std::string* var) noexcept;
229  option& bind_int(int* var) noexcept;
241  option& bind_uint(unsigned int* var) noexcept;
253  option& bind_double(double* var) noexcept;
263  bool has_bound_argument_variable() const noexcept { return m_bound_variable; }
273  void write_bool(bool value) const noexcept;
285  void write_string(const std::string& value) const;
297  void write_int(int value) const;
309  void write_uint(unsigned int value) const;
321  void write_double(double value) const;
322 
331  option& description(const std::string& desc) {
332  m_desc = desc;
333  return *this;
334  }
339  const std::string& description() const noexcept { return m_desc; }
340 
341  private:
342  std::string m_long_name; //< The long name.
343  char m_short_name{'\0'}; //< The short name.
344  std::string m_desc; //< Description of option (for help text).
345 
346  std::string m_arg_name; //< The name of the argument (for help text).
347  bool m_arg_required{false}; //< True if argument is mandatory, false if optional.
348  arg_type m_arg_type{string_arg}; //< Type of argument that is expected.
349  bool* m_is_option_set = nullptr; //< Pointer to value to hold whether the option was set.
350  void* m_bound_variable = nullptr; //< Pointer to hold argument value.
351  };
352 
353 } // End namespace
354 
355 #endif
optionpp::option::write_uint
void write_uint(unsigned int value) const
Writes to the bound unsigned integer variable that was specified in bind_uint.
Definition: option.cpp:111
optionpp::option::is_argument_required
bool is_argument_required() const noexcept
Return true if the argument is mandatory.
Definition: option.hpp:194
optionpp::option::write_bool
void write_bool(bool value) const noexcept
Writes to the bound boolean variable that was specified in bind_bool.
Definition: option.cpp:92
optionpp::option
Describes a valid program command-line option.
Definition: option.hpp:67
optionpp::option::option
option(char short_name)
Construct an option with a given short name.
Definition: option.hpp:92
optionpp::option::short_name
option & short_name(char name) noexcept
Set the option's short name.
Definition: option.hpp:160
optionpp::option::write_double
void write_double(double value) const
Writes to the bound double variable that was specified in bind_double.
Definition: option.cpp:118
optionpp::option::bind_string
option & bind_string(std::string *var) noexcept
Designates that the option should take a string argument which should be stored in *var.
Definition: option.cpp:52
optionpp::option::write_string
void write_string(const std::string &value) const
Writes to the bound string variable that was specified in bind_string.
Definition: option.cpp:97
optionpp::option::option
option() noexcept
Default constructor.
Definition: option.hpp:84
optionpp::option::write_int
void write_int(int value) const
Writes to the bound integer variable that was specified in bind_int.
Definition: option.cpp:104
optionpp::option::bind_uint
option & bind_uint(unsigned int *var) noexcept
Designates that the option should take an unsigned integer argument which should be stored in *var.
Definition: option.cpp:72
optionpp::option::bind_int
option & bind_int(int *var) noexcept
Designates that the option should take an integer argument which should be stored in *var.
Definition: option.cpp:62
optionpp::option::name
std::string name() const noexcept
Returns a name for the option.
Definition: option.hpp:131
optionpp::option::name
option & name(const std::string &long_name, char short_name='\0')
Sets the long and short name for the option.
Definition: option.hpp:117
optionpp::option::argument_name
const std::string & argument_name() const noexcept
Retrieve the option's argument name.
Definition: option.hpp:189
optionpp::option::argument
option & argument(const std::string &name, bool required=true)
Set the option's argument information.
Definition: option.cpp:38
optionpp::option::bind_bool
option & bind_bool(bool *var) noexcept
Designates a location to store whether the option was set.
Definition: option.cpp:45
optionpp::option::argument_type
arg_type argument_type() const noexcept
Return the type of argument.
Definition: option.hpp:199
optionpp::option::description
const std::string & description() const noexcept
Retrieve the option description.
Definition: option.hpp:339
optionpp
Library namespace.
Definition: error.hpp:31
optionpp::option::long_name
const std::string & long_name() const noexcept
Retrieve the option's long name.
Definition: option.hpp:153
optionpp::option::has_bound_argument_variable
bool has_bound_argument_variable() const noexcept
Returns true if a variable has been bound to the option's argument.
Definition: option.hpp:263
optionpp::option::short_name
char short_name() const noexcept
Retrieve the option's short name.
Definition: option.hpp:168
optionpp::option::bind_double
option & bind_double(double *var) noexcept
Designates that the option should take a double-precision floating point argument which should be sto...
Definition: option.cpp:82
optionpp::option::long_name
option & long_name(const std::string &name)
Set the option's long name.
Definition: option.hpp:145
optionpp::option::arg_type
arg_type
Holds the possible argument types.
Definition: option.hpp:73
optionpp::option::description
option & description(const std::string &desc)
Set the option description.
Definition: option.hpp:331