BDE 4.39.x Production Release
Loading...
Searching...
No Matches
balcl_optioninfo.h
Go to the documentation of this file.
1/// @file balcl_optioninfo.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// balcl_optioninfo.h -*-C++-*-
8#ifndef INCLUDED_BALCL_OPTIONINFO
9#define INCLUDED_BALCL_OPTIONINFO
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup balcl_optioninfo balcl_optioninfo
15/// @brief Provide a POD command-line-option descriptor `struct`.
16/// @addtogroup bal
17/// @{
18/// @addtogroup balcl
19/// @{
20/// @addtogroup balcl_optioninfo
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#balcl_optioninfo-purpose"> Purpose</a>
25/// * <a href="#balcl_optioninfo-classes"> Classes </a>
26/// * <a href="#balcl_optioninfo-description"> Description </a>
27/// * <a href="#balcl_optioninfo-usage"> Usage </a>
28///
29/// # Purpose {#balcl_optioninfo-purpose}
30/// Provide a POD command-line-option descriptor `struct`.
31///
32/// # Classes {#balcl_optioninfo-classes}
33///
34/// - balcl::OptionInfo: POD `struct` that describes a command-line option
35///
36/// @see balcl_option, balcl_commandline
37///
38/// # Description {#balcl_optioninfo-description}
39/// This component provides a `struct`, `balcl::OptionInfo`, that
40/// describes a command-line option. The `balcl::OptionInfo` `struct` is used
41/// to specify the user-defined command-line options accepted by a
42/// `balcl::CommandLine` object. This type is typically used when one wants to
43/// statically initialize an array of option specifications. When an
44/// allocator-aware, full-featured value-semantic class is needed to describe
45/// command-line options, use `balcl::Option`.
46///
47/// For further details see
48/// @ref balcl_commandline-specifying-command-line-arguments .
49///
50/// ## Usage {#balcl_optioninfo-usage}
51///
52///
53/// The intended use of this component is illustrated in
54/// @ref balcl_commandline-usage .
55/// @}
56/** @} */
57/** @} */
58
59/** @addtogroup bal
60 * @{
61 */
62/** @addtogroup balcl
63 * @{
64 */
65/** @addtogroup balcl_optioninfo
66 * @{
67 */
68
69#include <balscm_version.h>
70
71#include <balcl_typeinfo.h>
73
75
76#include <bsl_iosfwd.h>
77#include <bsl_string.h>
78
79// Optioninfo is intended to be aggregate-initialized. This means that in
80// C++03, it must not have constructors declared. Declaring the constructors
81// is useful in C++11 and beyond because it avoids warnings when later fields
82// are not initialized by the aggregate.
83
84#undef BALCL_OPTIONINFO_HAS_CONSTRUCTORS
85#if 201103L <= BSLS_COMPILERFEATURES_CPLUSPLUS
86# define BALCL_OPTIONINFO_HAS_CONSTRUCTORS
87#endif
88
89
90namespace balcl {
91
92 // =================
93 // struct OptionInfo
94 // =================
95
96/// This `struct` is a simple attribute class that describes the information
97/// associated with an option, namely the associated tag (as a string, from
98/// which the short and long tags are extracted), the option name, the
99/// description used in printing usage, and optional associated `TypeInfo`
100/// and `OccurrenceInfo` objects.
101///
102/// By design, this `struct` does not have any user-defined constructors, so
103/// there is no provision for passing an allocator to its data members (all
104/// of which take an allocator). Consequently, all instances of this class
105/// use the default allocator. If proper allocator propagation is desired
106/// (e.g., for storage within an allocator-aware container for which the use
107/// of the default allocator is counter-indicated), one may use `Option`,
108/// which is both allocator-aware and constructible from `OptionInfo`.
109///
110/// The main purpose of this `struct` is to provide a type whose values can
111/// be statically-initialized. For example:
112/// @code
113/// const balcl::OptionInfo OPTIONS[] = {
114/// {
115/// "s|longTag", // s(hortTag)
116/// "optionName",
117/// "option description",
118/// balcl::TypeInfo(/* . . . */), // optional
119/// balcl::OccurrenceInfo(/* . . . */) // optional
120/// },
121/// // ...
122/// };
123/// @endcode
124///
125/// \note Note that each of the first three fields can be default-constructed, and
126/// thus omitted in such a declaration; however, such an object will be of
127/// limited use because, to avoid undefined behavior, the constructor of
128/// `balcl::CommandLine` requires that each of these fields be acceptable to
129/// the `isDescriptionValid`, `isNameValid`, and `isTagValid` methods of
130/// `balcl::Option`. The default string value is not acceptable to any of
131/// those methods. See the {Usage} section for an example of such
132/// initialization.
133///
134/// See @ref balcl_optioninfo
136
137 // TYPES
138
139 /// Enumerate the categories of command-line arguments.
140 enum ArgType {
141 e_FLAG = 0, // boolean option (present on command line, or not)
142 e_OPTION = 1, // option having a value
143 e_NON_OPTION = 2 // other command-line argument
144 };
145
146 // PUBLIC DATA
147 bsl::string d_tag; // tags (or "" for non-option argument)
148
149 bsl::string d_name; // accessing name
150
151 bsl::string d_description; // description used in printing usage
152
153 TypeInfo d_typeInfo; // Optional field. Within that,
154 // - (optional) type/variable to be linked,
155 // - (optional) constraint
156
157 OccurrenceInfo d_defaultInfo; // Optional -- two sub-parts:
158 // - whether the option is required,
159 // optional, or hidden (default is
160 // optional)
161 // - optionally, a default value.
162
164 // Optional -- environment variable name
165
166#ifdef BALCL_OPTIONINFO_HAS_CONSTRUCTORS
167 // CREATORS
168
169 /// Create an `OptionInfo` with the specified `tag`, `name`, and
170 /// `description`, with `d_typeInfo`, `d_defaultInfo`, and
171 /// `d_environmentVariableName` default-constructed.
173 bsl::string_view name = "",
174 bsl::string_view description = ""); // IMPLICIT
175
176 /// Create an `OptionInfo` with the specified `tag`, `name`, `description`,
177 /// and `typeInfo`, with `d_defaultInfo`, and `d_environmentVariableName`
178 /// default-constructed.
180 bsl::string_view name,
181 bsl::string_view description,
182 const TypeInfo& typeInfo);
183
184 /// Create an `OptionInfo` with the specified `tag`, `name`, `description`,
185 /// `typeInfo`, `defaultInfo`, and `envVarName`.
187 bsl::string_view name,
188 bsl::string_view description,
189 const TypeInfo& typeInfo,
190 const OccurrenceInfo& defaultInfo,
191 bsl::string_view envVarName = "");
192#endif
193};
194
195#ifdef BALCL_OPTIONINFO_HAS_CONSTRUCTORS
196// CREATORS
197inline
198OptionInfo::OptionInfo(bsl::string_view tag,
199 bsl::string_view name,
200 bsl::string_view description)
201: d_tag(tag)
202, d_name(name)
203, d_description(description)
204, d_typeInfo()
205, d_defaultInfo()
206, d_environmentVariableName()
207{
208}
209
210inline
211OptionInfo::OptionInfo(bsl::string_view tag,
212 bsl::string_view name,
213 bsl::string_view description,
214 const TypeInfo& typeInfo)
215: d_tag(tag)
216, d_name(name)
217, d_description(description)
218, d_typeInfo(typeInfo)
219, d_defaultInfo()
220, d_environmentVariableName()
221{
222}
223
224inline
225OptionInfo::OptionInfo(bsl::string_view tag,
226 bsl::string_view name,
227 bsl::string_view description,
228 const TypeInfo& typeInfo,
229 const OccurrenceInfo& defaultInfo,
230 bsl::string_view envVarName)
231: d_tag(tag)
232, d_name(name)
233, d_description(description)
234, d_typeInfo(typeInfo)
235, d_defaultInfo(defaultInfo)
236, d_environmentVariableName(envVarName)
237{
238}
239#endif
240
241// FREE OPERATORS
242
243/// Return `true` if the specified `lhs` and `rhs` have the same value, and
244/// `false` otherwise. Two `OptionInfo` objects have the same value if they
245/// have the same tag string, the same name, the same description, the same
246/// type info, and the same occurrence info values.
247bool operator==(const OptionInfo& lhs, const OptionInfo& rhs);
248
249/// Return `true` if the specified `lhs` and `rhs` do not have the same
250/// value, and `false` otherwise. Two `OptionInfo` object do not have the
251/// same value if they do not have the same tag strings, or the same names,
252/// or the same descriptions, or the same type information, or the same
253/// occurrence information.
254bool operator!=(const OptionInfo& lhs, const OptionInfo& rhs);
255
256/// Write the value of the specified `rhs` object to the specified `stream`
257/// in a (multi-line) human readable format and return a reference to `stream`.
258///
259/// \note Note that the last line is *not* terminated by a newline
260/// character.
261bsl::ostream& operator<<(bsl::ostream& stream, const OptionInfo& rhs);
262
263} // close package namespace
264
265// ============================================================================
266// INLINE DEFINITIONS
267// ============================================================================
268
269 // -----------------
270 // struct OptionInfo
271 // -----------------
272
273// FREE OPERATORS
274inline
275bool balcl::operator==(const OptionInfo& lhs, const OptionInfo& rhs)
276{
277 return lhs.d_tag == rhs.d_tag
278 && lhs.d_name == rhs.d_name
279 && lhs.d_description == rhs.d_description
280 && lhs.d_typeInfo == rhs.d_typeInfo
281 && lhs.d_defaultInfo == rhs.d_defaultInfo
282 && lhs.d_environmentVariableName == rhs.d_environmentVariableName;
283}
284
285inline
286bool balcl::operator!=(const OptionInfo& lhs, const OptionInfo& rhs)
287{
288 return lhs.d_tag != rhs.d_tag
289 || lhs.d_name != rhs.d_name
290 || lhs.d_description != rhs.d_description
291 || lhs.d_typeInfo != rhs.d_typeInfo
292 || lhs.d_defaultInfo != rhs.d_defaultInfo
293 || lhs.d_environmentVariableName != rhs.d_environmentVariableName;
294}
295
296
297
298#endif
299
300// ----------------------------------------------------------------------------
301// Copyright 2020 Bloomberg Finance L.P.
302//
303// Licensed under the Apache License, Version 2.0 (the "License");
304// you may not use this file except in compliance with the License.
305// You may obtain a copy of the License at
306//
307// http://www.apache.org/licenses/LICENSE-2.0
308//
309// Unless required by applicable law or agreed to in writing, software
310// distributed under the License is distributed on an "AS IS" BASIS,
311// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
312// See the License for the specific language governing permissions and
313// limitations under the License.
314// ----------------------------- END-OF-FILE ----------------------------------
315
316/** @} */
317/** @} */
318/** @} */
Definition balcl_occurrenceinfo.h:120
Definition balcl_typeinfo.h:118
Definition bslstl_stringview.h:471
Definition bslstl_string.h:1252
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition balcl_commandline.h:1364
bool operator==(const CommandLine_SchemaData &lhs, const CommandLine_SchemaData &rhs)
bsl::ostream & operator<<(bsl::ostream &stream, const CommandLine &rhs)
bool operator!=(const CommandLine_SchemaData &lhs, const CommandLine_SchemaData &rhs)
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
ALLOCATOR & lhs
Definition bslstl_string.h:3917
Definition balcl_optioninfo.h:135
bsl::string d_tag
Definition balcl_optioninfo.h:147
bsl::string d_name
Definition balcl_optioninfo.h:149
TypeInfo d_typeInfo
Definition balcl_optioninfo.h:153
bsl::string d_description
Definition balcl_optioninfo.h:151
OccurrenceInfo d_defaultInfo
Definition balcl_optioninfo.h:157
ArgType
Enumerate the categories of command-line arguments.
Definition balcl_optioninfo.h:140
@ e_FLAG
Definition balcl_optioninfo.h:141
@ e_OPTION
Definition balcl_optioninfo.h:142
@ e_NON_OPTION
Definition balcl_optioninfo.h:143
bsl::string d_environmentVariableName
Definition balcl_optioninfo.h:163