BDE 4.14.0 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/// Note that each of the first three fields can be default-constructed, and
125/// thus omitted in such a declaration; however, such an object will be of
126/// limited use because, to avoid undefined behavior, the constructor of
127/// `balcl::CommandLine` requires that each of these fields be acceptable to
128/// the `isDescriptionValid`, `isNameValid`, and `isTagValid` methods of
129/// `balcl::Option`. The default string value is not acceptable to any of
130/// those methods. See the {Usage} section for an example of such
131/// initialization.
133
134 // TYPES
135
136 /// Enumerate the categories of command-line arguments.
137 enum ArgType {
138 e_FLAG = 0, // boolean option (present on command line, or not)
139 e_OPTION = 1, // option having a value
140 e_NON_OPTION = 2 // other command-line argument
141 };
142
143 // PUBLIC DATA
144 bsl::string d_tag; // tags (or "" for non-option argument)
145
146 bsl::string d_name; // accessing name
147
148 bsl::string d_description; // description used in printing usage
149
150 TypeInfo d_typeInfo; // Optional field. Within that,
151 // - (optional) type/variable to be linked,
152 // - (optional) constraint
153
154 OccurrenceInfo d_defaultInfo; // Optional -- two sub-parts:
155 // - whether the option is required,
156 // optional, or hidden (default is
157 // optional)
158 // - optionally, a default value.
159
161 // Optional -- environment variable name
162
163#ifdef BALCL_OPTIONINFO_HAS_CONSTRUCTORS
164 // CREATORS
165
166 /// Create an `OptionInfo` with the specified `tag`, `name`, and
167 /// `description`, with `d_typeInfo`, `d_defaultInfo`, and
168 /// `d_environmentVariableName` default-constructed.
170 bsl::string_view name = "",
171 bsl::string_view description = ""); // IMPLICIT
172
173 /// Create an `OptionInfo` with the specified `tag`, `name`, `description`,
174 /// and `typeInfo`, with `d_defaultInfo`, and `d_environmentVariableName`
175 /// default-constructed.
177 bsl::string_view name,
178 bsl::string_view description,
179 const TypeInfo& typeInfo);
180
181 /// Create an `OptionInfo` with the specified `tag`, `name`, `description`,
182 /// `typeInfo`, `defaultInfo`, and `envVarName`.
184 bsl::string_view name,
185 bsl::string_view description,
186 const TypeInfo& typeInfo,
187 const OccurrenceInfo& defaultInfo,
188 bsl::string_view envVarName = "");
189#endif
190};
191
192#ifdef BALCL_OPTIONINFO_HAS_CONSTRUCTORS
193// CREATORS
194inline
195OptionInfo::OptionInfo(bsl::string_view tag,
196 bsl::string_view name,
197 bsl::string_view description)
198: d_tag(tag)
199, d_name(name)
200, d_description(description)
201, d_typeInfo()
202, d_defaultInfo()
203, d_environmentVariableName()
204{
205}
206
207inline
208OptionInfo::OptionInfo(bsl::string_view tag,
209 bsl::string_view name,
210 bsl::string_view description,
211 const TypeInfo& typeInfo)
212: d_tag(tag)
213, d_name(name)
214, d_description(description)
215, d_typeInfo(typeInfo)
216, d_defaultInfo()
217, d_environmentVariableName()
218{
219}
220
221inline
222OptionInfo::OptionInfo(bsl::string_view tag,
223 bsl::string_view name,
224 bsl::string_view description,
225 const TypeInfo& typeInfo,
226 const OccurrenceInfo& defaultInfo,
227 bsl::string_view envVarName)
228: d_tag(tag)
229, d_name(name)
230, d_description(description)
231, d_typeInfo(typeInfo)
232, d_defaultInfo(defaultInfo)
233, d_environmentVariableName(envVarName)
234{
235}
236#endif
237
238// FREE OPERATORS
239
240/// Return `true` if the specified `lhs` and `rhs` have the same value, and
241/// `false` otherwise. Two `OptionInfo` objects have the same value if they
242/// have the same tag string, the same name, the same description, the same
243/// type info, and the same occurrence info values.
244bool operator==(const OptionInfo& lhs, const OptionInfo& rhs);
245
246/// Return `true` if the specified `lhs` and `rhs` do not have the same
247/// value, and `false` otherwise. Two `OptionInfo` object do not have the
248/// same value if they do not have the same tag strings, or the same names,
249/// or the same descriptions, or the same type information, or the same
250/// occurrence information.
251bool operator!=(const OptionInfo& lhs, const OptionInfo& rhs);
252
253/// Write the value of the specified `rhs` object to the specified `stream`
254/// in a (multi-line) human readable format and return a reference to
255/// `stream`. Note that the last line is *not* terminated by a newline
256/// character.
257bsl::ostream& operator<<(bsl::ostream& stream, const OptionInfo& rhs);
258
259} // close package namespace
260
261// ============================================================================
262// INLINE DEFINITIONS
263// ============================================================================
264
265 // -----------------
266 // struct OptionInfo
267 // -----------------
268
269// FREE OPERATORS
270inline
271bool balcl::operator==(const OptionInfo& lhs, const OptionInfo& rhs)
272{
273 return lhs.d_tag == rhs.d_tag
274 && lhs.d_name == rhs.d_name
275 && lhs.d_description == rhs.d_description
276 && lhs.d_typeInfo == rhs.d_typeInfo
277 && lhs.d_defaultInfo == rhs.d_defaultInfo
278 && lhs.d_environmentVariableName == rhs.d_environmentVariableName;
279}
280
281inline
282bool balcl::operator!=(const OptionInfo& lhs, const OptionInfo& rhs)
283{
284 return lhs.d_tag != rhs.d_tag
285 || lhs.d_name != rhs.d_name
286 || lhs.d_description != rhs.d_description
287 || lhs.d_typeInfo != rhs.d_typeInfo
288 || lhs.d_defaultInfo != rhs.d_defaultInfo
289 || lhs.d_environmentVariableName != rhs.d_environmentVariableName;
290}
291
292
293
294#endif
295
296// ----------------------------------------------------------------------------
297// Copyright 2020 Bloomberg Finance L.P.
298//
299// Licensed under the Apache License, Version 2.0 (the "License");
300// you may not use this file except in compliance with the License.
301// You may obtain a copy of the License at
302//
303// http://www.apache.org/licenses/LICENSE-2.0
304//
305// Unless required by applicable law or agreed to in writing, software
306// distributed under the License is distributed on an "AS IS" BASIS,
307// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
308// See the License for the specific language governing permissions and
309// limitations under the License.
310// ----------------------------- END-OF-FILE ----------------------------------
311
312/** @} */
313/** @} */
314/** @} */
Definition balcl_occurrenceinfo.h:120
Definition balcl_typeinfo.h:117
Definition bslstl_stringview.h:441
Definition bslstl_string.h:1281
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
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)
Definition balcl_optioninfo.h:132
bsl::string d_tag
Definition balcl_optioninfo.h:144
bsl::string d_name
Definition balcl_optioninfo.h:146
TypeInfo d_typeInfo
Definition balcl_optioninfo.h:150
bsl::string d_description
Definition balcl_optioninfo.h:148
OccurrenceInfo d_defaultInfo
Definition balcl_optioninfo.h:154
ArgType
Enumerate the categories of command-line arguments.
Definition balcl_optioninfo.h:137
@ e_FLAG
Definition balcl_optioninfo.h:138
@ e_OPTION
Definition balcl_optioninfo.h:139
@ e_NON_OPTION
Definition balcl_optioninfo.h:140
bsl::string d_environmentVariableName
Definition balcl_optioninfo.h:160