E-MailRelay
goptionvalue.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program 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// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file goptionvalue.h
19///
20
21#ifndef G_OPTION_VALUE_H
22#define G_OPTION_VALUE_H
23
24#include "gdef.h"
25#include "gstr.h"
26#include "gstringview.h"
27#include <string>
28
29namespace G
30{
31 class OptionValue ;
32}
33
34//| \class G::OptionValue
35/// A simple structure encapsulating the value of a command-line option.
36/// Unvalued options (eg. "--debug") can be be explicitly on (eg. "--debug=yes")
37/// or off ("--debug=no"); the latter are typically ignored.
38///
40{
41public:
42 OptionValue() ;
43 ///< Default constructor for a valueless value.
44
45 explicit OptionValue( const std::string & s , std::size_t count = 1U ) ;
46 ///< Constructor for a valued value.
47 ///< Precondition: !s.empty()
48
49 static OptionValue on() ;
50 ///< A factory function for an unvalued option-enabled option.
51
52 static OptionValue off() ;
53 ///< A factory function for an unvalued option-disabled option.
54
55 bool isOn() const noexcept ;
56 ///< Returns true if on().
57
58 bool isOff() const noexcept ;
59 ///< Returns true if off().
60
61 std::string value() const ;
62 ///< Returns the value as a string.
63
64 std::string_view valueref() const noexcept ;
65 ///< Exposes the value as a string view.
66
67 bool operator==( std::string_view ) const noexcept ;
68 ///< Returns true if the given string matches value().
69
70 bool numeric() const noexcept ;
71 ///< Returns true if value() is an unsigned integer.
72
73 unsigned int number( unsigned int default_ = 0U ) const ;
74 ///< Returns value() as an unsigned integer.
75 ///< Returns the default if not numeric().
76
77 size_t count() const noexcept ;
78 ///< Returns an instance count that is one by default.
79
80 void increment() noexcept ;
81 ///< Increments the instance count().
82
83private:
84 bool m_on_off {false} ;
85 std::size_t m_count {1U} ;
86 std::string m_value ;
87} ;
88
89inline
91 m_on_off(true) ,
92 m_value(G::Str::positive())
93{
94}
95
96inline
97G::OptionValue::OptionValue( const std::string & s , std::size_t count ) :
98 m_count(count) ,
99 m_value(s)
100{
101}
102
103inline
105{
106 return {} ;
107}
108
109inline
111{
112 OptionValue v ;
113 v.m_value = G::Str::negative() ;
114 return v ;
115}
116
117inline
118bool G::OptionValue::isOn() const noexcept
119{
120 return m_on_off && G::Str::isPositive(m_value) ;
121}
122
123inline
124bool G::OptionValue::isOff() const noexcept
125{
126 return m_on_off && G::Str::isNegative(m_value) ;
127}
128
129inline
130std::string G::OptionValue::value() const
131{
132 return m_value ;
133}
134
135inline
136std::string_view G::OptionValue::valueref() const noexcept
137{
138 return {m_value} ;
139}
140
141inline
142bool G::OptionValue::operator==( std::string_view s ) const noexcept
143{
144 return s == m_value ;
145}
146
147inline
148bool G::OptionValue::numeric() const noexcept
149{
150 return !m_on_off && G::Str::isUInt(m_value) ;
151}
152
153inline
154unsigned int G::OptionValue::number( unsigned int default_ ) const
155{
156 return numeric() ? G::Str::toUInt(m_value) : default_ ;
157}
158
159inline
160std::size_t G::OptionValue::count() const noexcept
161{
162 return m_count ;
163}
164
165inline
167{
168 m_count++ ;
169}
170
171#endif
A simple structure encapsulating the value of a command-line option.
Definition: goptionvalue.h:40
OptionValue()
Default constructor for a valueless value.
Definition: goptionvalue.h:90
std::string_view valueref() const noexcept
Exposes the value as a string view.
Definition: goptionvalue.h:136
static OptionValue on()
A factory function for an unvalued option-enabled option.
Definition: goptionvalue.h:104
bool isOff() const noexcept
Returns true if off().
Definition: goptionvalue.h:124
size_t count() const noexcept
Returns an instance count that is one by default.
Definition: goptionvalue.h:160
static OptionValue off()
A factory function for an unvalued option-disabled option.
Definition: goptionvalue.h:110
bool numeric() const noexcept
Returns true if value() is an unsigned integer.
Definition: goptionvalue.h:148
bool isOn() const noexcept
Returns true if on().
Definition: goptionvalue.h:118
std::string value() const
Returns the value as a string.
Definition: goptionvalue.h:130
unsigned int number(unsigned int default_=0U) const
Returns value() as an unsigned integer.
Definition: goptionvalue.h:154
void increment() noexcept
Increments the instance count().
Definition: goptionvalue.h:166
bool operator==(std::string_view) const noexcept
Returns true if the given string matches value().
Definition: goptionvalue.h:142
A static class which provides string helper functions.
Definition: gstr.h:48
static bool isUInt(std::string_view s) noexcept
Returns true if the string can be converted into an unsigned integer without throwing an exception.
Definition: gstr.cpp:446
static bool isPositive(std::string_view) noexcept
Returns true if the string has a positive meaning, such as "1", "true", "yes".
Definition: gstr.cpp:1376
static std::string negative()
Returns a default negative string. See isNegative().
Definition: gstr.cpp:1371
static unsigned int toUInt(std::string_view s)
Converts string 's' to an unsigned int.
Definition: gstr.cpp:648
static bool isNegative(std::string_view) noexcept
Returns true if the string has a negative meaning, such as "0", "false", "no".
Definition: gstr.cpp:1384
Low-level classes.
Definition: garg.h:36