BDE 4.39.x 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
157 /// Create a default (empty) command-line option. Optionally specify a
158 /// `basicAllocator` used to supply memory. If `basicAllocator` is 0,
159 /// 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 option.
213 ///
214 /// \pre The behavior is undefined if this option is a non-option
215 /// argument (i.e., has no tag).
216 const char *longTag() const;
217
218 /// Return the name of this option.
219 const bsl::string& name() const;
220
221 /// Return a `const` reference to the occurrence info for this option
222 /// (i.e., whether the option is required, optional, or hidden, and its
223 /// default value, if any).
225
226 /// Return the single character used to identify this option on a command
227 /// line preceded with a single `-`, or 0 if this option only has a long tag.
228 ///
229 /// \pre The behavior is undefined if this option is a non-option argument
230 /// (i.e., has no tag).
231 char shortTag() const;
232
233 /// Return the tag string provided to this object at construction.
234 ///
235 /// \note Note that this string is empty if this is a non-option argument.
236 const bsl::string& tagString() const;
237
238 /// Return a `const` reference to the type info for this option (i.e., the
239 /// type of the option, whether the option is linked to a variable, and
240 /// whether it has a constraint).
241 const TypeInfo& typeInfo() const;
242
243 // Validators
244
245 /// Return `true` if the description is valid for this option, leaving
246 /// the specified `stream` unaffected; otherwise, write a diagnostic
247 /// message to `stream` and return `false`.
248 bool isDescriptionValid(bsl::ostream& stream) const;
249
250 /// Return `true` if the specified `longTag` is valid for this option,
251 /// leaving the specified `stream` unaffected; otherwise, write a
252 /// diagnostic message to `stream` and return `false`.
253 bool isLongTagValid(const char *longTag, bsl::ostream& stream) const;
254
255 /// Return `true` if the name is valid for this option, leaving the
256 /// specified `stream` unaffected; otherwise, write a diagnostic message to
257 /// `stream` and return `false`.
258 bool isNameValid(bsl::ostream& stream) const;
259
260 /// Return `true` if the tag is valid for this option, leaving the
261 /// specified `stream` unaffected; otherwise, write a diagnostic message to
262 /// `stream` and return `false`.
263 bool isTagValid(bsl::ostream& stream) const;
264
265 // Aspects
266
267 /// Return the allocator used by this object to supply memory.
268 ///
269 /// \note Note that if no allocator was supplied at construction the currently installed
270 /// default allocator at construction is used.
272
273 /// Format this object to the specified output `stream` at the (absolute
274 /// value of) the optionally specified indentation `level` and return a
275 /// reference to `stream`. If `level` is specified, optionally specify
276 /// `spacesPerLevel`, the number of spaces per indentation level for this
277 /// object. If `level` is negative, suppress indentation of the first
278 /// line. If `stream` is not valid on entry, this operation has no effect.
279 ///
280 /// \pre The behavior is undefined if `spacesPerLevel` is negative.
281 bsl::ostream& print(bsl::ostream& stream,
282 int level = 0,
283 int spacesPerLevel = 4) const;
284};
285
286// FREE OPERATORS
287
288/// Return `true` if the specified `lhs` and `rhs` have the same value, and
289/// `false` otherwise. Two `Option` objects have the same value if their
290/// underlying `OptionInfo` objects have the same value.
291bool operator==(const Option& lhs, const Option& rhs);
292
293/// Return `true` if the specified `lhs` command-line option has a different
294/// value from the specified `rhs` command-line option, and `false` otherwise.
295/// Two `Option` objects do not have the same value if their underlying
296/// `OptionInfo` objects do not have the same value.
297bool operator!=(const Option& lhs, const Option& rhs);
298
299/// Write the value of the specified `rhs` object to the specified `stream` in
300/// a (multi-line) human readable format and return a reference to `stream`.
301///
302/// \note Note that the last line is *not* terminated by a newline character.
303bsl::ostream& operator<<(bsl::ostream& stream, const Option& rhs);
304
305} // close package namespace
306
307
308#endif
309
310// ----------------------------------------------------------------------------
311// Copyright 2020 Bloomberg Finance L.P.
312//
313// Licensed under the Apache License, Version 2.0 (the "License");
314// you may not use this file except in compliance with the License.
315// You may obtain a copy of the License at
316//
317// http://www.apache.org/licenses/LICENSE-2.0
318//
319// Unless required by applicable law or agreed to in writing, software
320// distributed under the License is distributed on an "AS IS" BASIS,
321// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
322// See the License for the specific language governing permissions and
323// limitations under the License.
324// ----------------------------- END-OF-FILE ----------------------------------
325
326/** @} */
327/** @} */
328/** @} */
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:118
Definition bslstl_string.h:1252
Definition bslma_allocator.h:545
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition balcl_commandline.h:1364
bsl::ostream & operator<<(bsl::ostream &stream, const CommandLine &rhs)
Definition balcl_optioninfo.h:135
ArgType
Enumerate the categories of command-line arguments.
Definition balcl_optioninfo.h:140
Definition bdlb_printmethods.h:306
Definition bslma_usesbslmaallocator.h:344
Definition bsls_objectbuffer.h:277