BDE 4.14.0 Production release
Loading...
Searching...
No Matches
balcl_option.h
Go to the documentation of this file.
1/// @file balcl_option.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// balcl_option.h -*-C++-*-
8#ifndef INCLUDED_BALCL_OPTION
9#define INCLUDED_BALCL_OPTION
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup balcl_option balcl_option
15/// @brief Provide an allocator-aware command-line-option descriptor class.
16/// @addtogroup bal
17/// @{
18/// @addtogroup balcl
19/// @{
20/// @addtogroup balcl_option
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#balcl_option-purpose"> Purpose</a>
25/// * <a href="#balcl_option-classes"> Classes </a>
26/// * <a href="#balcl_option-description"> Description </a>
27/// * <a href="#balcl_option-usage"> Usage </a>
28///
29/// # Purpose {#balcl_option-purpose}
30/// Provide an allocator-aware command-line-option descriptor class.
31///
32/// # Classes {#balcl_option-classes}
33///
34/// - balcl::Option: allocator-aware, in-core VST option descriptor
35///
36/// @see balcl_optioninfo, balcl_commandline
37///
38/// # Description {#balcl_option-description}
39/// This component provides an unconstrained, allocator-aware,
40/// in-core (value-semantic) attribute class, `balcl::Option`, that describes a
41/// command-line option. Class `balcl::Option` is used to specify the
42/// user-defined command-line options accepted by a `balcl::CommandLine` object.
43///
44/// The value of a `balcl::Option` object is the same as that of the
45/// `balcl::OptionInfo` object accessible from the option object via a
46/// conversion operator. That value consists of several (subordinate)
47/// attributes:
48///
49/// * the strings associated with the option:
50/// - `tag`
51/// - `name`, and
52/// - `description`
53/// * the `typeInfo` attribute (see @ref balcl_typeinfo ) that, in turn,
54/// consists of:
55/// - `type` (type of the option's value)
56/// - `linkedVariable` [optional], and
57/// - `constraint` [optional]
58///
59/// * the `occurrenceInfo` attribute (see @ref balcl_occurrenceinfo ) that, in
60/// turn, consists of:
61/// - `occurrenceType` (required, optional, or hidden)
62/// - `defaulValue` [optional]
63///
64/// Since `balcl::TypeInfo` is an in-core VST, so is `balcl::Option`.
65///
66/// When constructing a `balcl::Option` from a `balcl::OptionInfo` object, the
67/// class places no constraints on the latter's value except, of course, for the
68/// constraints required by the types that compose the `balcl::OptionInfo`
69/// class.
70///
71/// Additionally, the `balcl::Option` class provides:
72///
73/// * A set of (`is*Valid`) methods that report whether or not the option's
74/// string attributes (tag, name, description) are valid for use by
75/// `balcl::CommandLine`.
76/// * Allocator awareness that allows `balcl::Option` values to be stored in
77/// allocator-aware containers.
78///
79/// ## Usage {#balcl_option-usage}
80///
81///
82/// The intended use of this component is illustrated in
83/// @ref balcl_commandline-usage .
84/// @}
85/** @} */
86/** @} */
87
88/** @addtogroup bal
89 * @{
90 */
91/** @addtogroup balcl
92 * @{
93 */
94/** @addtogroup balcl_option
95 * @{
96 */
97
98#include <balscm_version.h>
99
100#include <balcl_optioninfo.h>
101
102#include <bdlb_printmethods.h> // 'bdlb::HasPrintMethod'
103
104#include <bslma_allocator.h>
106
108
109#include <bsls_objectbuffer.h>
110
111#include <bsl_iosfwd.h> // 'bsl::ostream'
112#include <bsl_string.h>
113
114
115namespace balcl {
116
117class OccurrenceInfo;
118class TypeInfo;
119
120 // ============
121 // class Option
122 // ============
123
124/// This `class`, constructible from and implicitly convertible to
125/// `OptionInfo`, provides the same attributes, but also uses a
126/// `bslma::Allocator` and thus can be stored in a container. A minor subtlety
127/// arises in the `name` attribute, whereby any suffix starting with an `=`
128/// sign in the `name` attribute of a `OptionInfo` is removed in order to
129/// derive the `name` of the `Option`.
130///
131/// See @ref balcl_option
132class Option {
133
134 // DATA
136 d_optionInfo; // underlying option info
137
138 bslma::Allocator *d_allocator_p; // memory allocator (held, not owned)
139
140 private:
141 // PRIVATE MANIPULATORS
142
143 /// Default initialize the underlying option info.
144 void init();
145
146 /// Initialize the underlying option info from the value of the
147 /// specified `optionInfo` object.
148 void init(const OptionInfo& optionInfo);
149
150 public:
151 // TRAITS
154
155 // CREATORS
156
158 /// Create a default (empty) command-line option. Optionally specify a
159 /// `basicAllocator` used to supply memory. If `basicAllocator` is 0,
160 /// the currently installed default allocator is used.
161 explicit
162 Option(bslma::Allocator *basicAllocator);
163
164 /// Create a command-line option containing the value of the specified
165 /// `optionInfo`. Optionally specify a `basicAllocator` used to supply
166 /// memory. If `basicAllocator` is 0, the currently installed default
167 /// allocator is used.
168 explicit
169 Option(const OptionInfo& optionInfo,
170 bslma::Allocator *basicAllocator = 0);
171
172 /// Create a `Option` object having the same value as the specified
173 /// `original` object. Optionally specify a `basicAllocator` used to
174 /// supply memory. If `basicAllocator` is 0, the currently installed
175 /// default allocator is used.
176 Option(const Option& original,
177 bslma::Allocator *basicAllocator = 0);
178
179 /// Destroy this command-line option object.
181
182 // MANIPULATORS
183
184 /// Assign to this object the value of the specified `rhs` object and
185 /// return a reference providing modifiable access to this object.
186 Option& operator=(const Option& rhs);
188
189 // ACCESSORS
190
191 /// Return a `const` reference to the `OptionInfo` object having the value
192 /// of this option.
193 operator const OptionInfo&() const;
194
195 /// Return the `ArgType` enumerator identifying whether this option is a
196 /// flag (i.e., a tag not followed by a value), or whether it is an option
197 /// (i.e., a tag followed by a value), or a non-option argument (i.e., a
198 /// value not preceded by a short or long tag).
200
201 /// Return the description of this option.
202 const bsl::string& description() const;
203
204 /// Return the environment variable name of this option.
206
207 /// Return `true` if the value of this option is of array type, and `false`
208 /// otherwise.
209 bool isArray() const;
210
211 /// Return the string used to identify this option on a command line
212 /// preceded with `--`, or 0 if there is no long tag associated with this
213 /// option. The behavior is undefined if this option is a non-option
214 /// argument (i.e., has no tag).
215 const char *longTag() const;
216
217 /// Return the name of this option.
218 const bsl::string& name() const;
219
220 /// Return a `const` reference to the occurrence info for this option
221 /// (i.e., whether the option is required, optional, or hidden, and its
222 /// default value, if any).
224
225 /// Return the single character used to identify this option on a command
226 /// line preceded with a single `-`, or 0 if this option only has a long
227 /// tag. The behavior is undefined if this option is a non-option argument
228 /// (i.e., has no tag).
229 char shortTag() const;
230
231 /// Return the tag string provided to this object at construction. Note
232 /// that this string is empty if this is a non-option argument.
233 const bsl::string& tagString() const;
234
235 /// Return a `const` reference to the type info for this option (i.e., the
236 /// type of the option, whether the option is linked to a variable, and
237 /// whether it has a constraint).
238 const TypeInfo& typeInfo() const;
239
240 // Validators
241
242 /// Return `true` if the description is valid for this option, leaving
243 /// the specified `stream` unaffected; otherwise, write a diagnostic
244 /// message to `stream` and return `false`.
245 bool isDescriptionValid(bsl::ostream& stream) const;
246
247 /// Return `true` if the specified `longTag` is valid for this option,
248 /// leaving the specified `stream` unaffected; otherwise, write a
249 /// diagnostic message to `stream` and return `false`.
250 bool isLongTagValid(const char *longTag, bsl::ostream& stream) const;
251
252 /// Return `true` if the name is valid for this option, leaving the
253 /// specified `stream` unaffected; otherwise, write a diagnostic message to
254 /// `stream` and return `false`.
255 bool isNameValid(bsl::ostream& stream) const;
256
257 /// Return `true` if the tag is valid for this option, leaving the
258 /// specified `stream` unaffected; otherwise, write a diagnostic message to
259 /// `stream` and return `false`.
260 bool isTagValid(bsl::ostream& stream) const;
261
262 // Aspects
263
264 /// Return the allocator used by this object to supply memory. Note that
265 /// if no allocator was supplied at construction the currently installed
266 /// default allocator at construction is used.
268
269 /// Format this object to the specified output `stream` at the (absolute
270 /// value of) the optionally specified indentation `level` and return a
271 /// reference to `stream`. If `level` is specified, optionally specify
272 /// `spacesPerLevel`, the number of spaces per indentation level for this
273 /// object. If `level` is negative, suppress indentation of the first
274 /// line. If `stream` is not valid on entry, this operation has no effect.
275 /// The behavior is undefined if `spacesPerLevel` is negative.
276 bsl::ostream& print(bsl::ostream& stream,
277 int level = 0,
278 int spacesPerLevel = 4) const;
279};
280
281// FREE OPERATORS
282
283/// Return `true` if the specified `lhs` and `rhs` have the same value, and
284/// `false` otherwise. Two `Option` objects have the same value if their
285/// underlying `OptionInfo` objects have the same value.
286bool operator==(const Option& lhs, const Option& rhs);
287
288/// Return `true` if the specified `lhs` command-line option has a different
289/// value from the specified `rhs` command-line option, and `false` otherwise.
290/// Two `Option` objects do not have the same value if their underlying
291/// `OptionInfo` objects do not have the same value.
292bool operator!=(const Option& lhs, const Option& rhs);
293
294/// Write the value of the specified `rhs` object to the specified `stream` in
295/// a (multi-line) human readable format and return a reference to `stream`.
296/// Note that the last line is *not* terminated by a newline character.
297bsl::ostream& operator<<(bsl::ostream& stream, const Option& rhs);
298
299} // close package namespace
300
301
302#endif
303
304// ----------------------------------------------------------------------------
305// Copyright 2020 Bloomberg Finance L.P.
306//
307// Licensed under the Apache License, Version 2.0 (the "License");
308// you may not use this file except in compliance with the License.
309// You may obtain a copy of the License at
310//
311// http://www.apache.org/licenses/LICENSE-2.0
312//
313// Unless required by applicable law or agreed to in writing, software
314// distributed under the License is distributed on an "AS IS" BASIS,
315// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
316// See the License for the specific language governing permissions and
317// limitations under the License.
318// ----------------------------- END-OF-FILE ----------------------------------
319
320/** @} */
321/** @} */
322/** @} */
Definition balcl_occurrenceinfo.h:120
Definition balcl_option.h:132
const bsl::string & name() const
Return the name of this option.
BSLMF_NESTED_TRAIT_DECLARATION(Option, bslma::UsesBslmaAllocator)
Option(const OptionInfo &optionInfo, bslma::Allocator *basicAllocator=0)
const bsl::string & description() const
Return the description of this option.
bool isTagValid(bsl::ostream &stream) const
~Option()
Destroy this command-line option object.
Option(const Option &original, bslma::Allocator *basicAllocator=0)
Option & operator=(const OptionInfo &rhs)
bslma::Allocator * allocator() const
bool isNameValid(bsl::ostream &stream) const
OptionInfo::ArgType argType() const
bool isDescriptionValid(bsl::ostream &stream) const
bool isArray() const
bool isLongTagValid(const char *longTag, bsl::ostream &stream) const
const char * longTag() const
const bsl::string & tagString() const
Option & operator=(const Option &rhs)
const OccurrenceInfo & occurrenceInfo() const
Option(bslma::Allocator *basicAllocator)
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
const TypeInfo & typeInfo() const
const bsl::string & environmentVariableName() const
Return the environment variable name of this option.
char shortTag() const
BSLMF_NESTED_TRAIT_DECLARATION(Option, bdlb::HasPrintMethod)
Definition balcl_typeinfo.h:117
Definition bslstl_string.h:1281
Definition bslma_allocator.h:457
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition balcl_commandline.h:1364
bsl::ostream & operator<<(bsl::ostream &stream, const CommandLine &rhs)
Definition balcl_optioninfo.h:132
ArgType
Enumerate the categories of command-line arguments.
Definition balcl_optioninfo.h:137
Definition bdlb_printmethods.h:306
Definition bslma_usesbslmaallocator.h:343
Definition bsls_objectbuffer.h:276