@@ -31,11 +31,12 @@ SOFTWARE.
31
31
*
32
32
* This header file contians the entire CPP-AP library implementation.
33
33
*
34
- * @version 1.2
34
+ * @version 2.0.0
35
35
*/
36
36
37
37
#pragma once
38
38
39
+ #include " detail/argument_name.hpp"
39
40
#include " detail/concepts.hpp"
40
41
41
42
#include < algorithm>
@@ -75,95 +76,6 @@ class argument_parser;
75
76
// / @brief Internal argument handling utility.
76
77
namespace argument ::detail {
77
78
78
- // / @brief Structure holding the argument name.
79
- struct argument_name {
80
- argument_name () = delete ;
81
-
82
- argument_name (const argument_name&) = default ;
83
- argument_name (argument_name&&) = default ;
84
-
85
- argument_name& operator =(const argument_name&) = delete ;
86
- argument_name& operator =(argument_name&&) = delete ;
87
-
88
- /* *
89
- * @brief Primary name constructor.
90
- * @param primary The primary name of the argument.
91
- */
92
- argument_name (std::string_view primary) : primary(primary) {}
93
-
94
- /* *
95
- * @brief Primary and secondary name constructor.
96
- * @param primary The primary name of the argument.
97
- * @param secondary The secondary (short) name of the argument.
98
- */
99
- argument_name (std::string_view primary, std::string_view secondary)
100
- : primary(primary), secondary(secondary) {}
101
-
102
- // / @brief Class destructor.
103
- ~argument_name () = default ;
104
-
105
- /* *
106
- * @brief Equality comparison operator.
107
- * @param other The argument_name instance to compare with.
108
- * @return Equality of argument names.
109
- */
110
- bool operator ==(const argument_name& other) const noexcept {
111
- if (not (this ->secondary and other.secondary ) and (this ->secondary or other.secondary ))
112
- return false ;
113
-
114
- if (this ->primary != other.primary )
115
- return false ;
116
-
117
- return this ->secondary ? this ->secondary .value () == other.secondary .value () : true ;
118
- }
119
-
120
- /* *
121
- * @brief Matches the given string to the argument_name instance.
122
- * @param arg_name The name string to match.
123
- * @return True if name is equal to either the primary or the secondary name of the argument_name instance.
124
- */
125
- [[nodiscard]] bool match (std::string_view arg_name) const noexcept {
126
- return arg_name == this ->primary
127
- or (this ->secondary and arg_name == this ->secondary .value ());
128
- }
129
-
130
- /* *
131
- * @brief Matches the given argument name to the argument_name instance.
132
- * @param arg_name The argument_name instance to match.
133
- * @return True if arg_name's primary or secondary value matches the argument_name instance.
134
- */
135
- [[nodiscard]] bool match (const argument_name& arg_name) const noexcept {
136
- if (this ->match (arg_name.primary ))
137
- return true ;
138
-
139
- if (arg_name.secondary )
140
- return this ->match (arg_name.secondary .value ());
141
-
142
- return false ;
143
- }
144
-
145
- // / @brief Get a string representation of the argument_name.
146
- [[nodiscard]] std::string str () const noexcept {
147
- return this ->secondary
148
- ? (" [" + this ->primary + " ," + this ->secondary .value () + " ]" )
149
- : (" [" + this ->primary + " ]" );
150
- }
151
-
152
- /* *
153
- * @brief Stream insertion operator for argument names.
154
- * @param os The output stream.
155
- * @param arg_name The argument name to be inserted into the stream.
156
- * @return The modified output stream.
157
- */
158
- friend std::ostream& operator <<(std::ostream& os, const argument_name& arg_name) noexcept {
159
- os << arg_name.str ();
160
- return os;
161
- }
162
-
163
- const std::string primary; // /< The primary name of the argument.
164
- const std::optional<std::string> secondary; // /< The optional (short) name of the argument.
165
- };
166
-
167
79
// / @brief Argument class interface
168
80
class argument_interface {
169
81
public:
@@ -196,7 +108,7 @@ class argument_interface {
196
108
197
109
protected:
198
110
// / @return Reference to the name of the argument.
199
- virtual const argument_name& name () const noexcept = 0;
111
+ virtual const ap::detail:: argument_name& name () const noexcept = 0;
200
112
201
113
// / @return True if the argument is required, false otherwise
202
114
virtual bool is_required () const noexcept = 0;
@@ -266,7 +178,7 @@ class value_already_set_error : public argument_parser_error {
266
178
* @brief Constructor for the value_already_set_error class.
267
179
* @param arg_name The name of the argument that already has a value set.
268
180
*/
269
- explicit value_already_set_error (const argument:: detail::argument_name& arg_name)
181
+ explicit value_already_set_error (const detail::argument_name& arg_name)
270
182
: argument_parser_error(
271
183
std::format (" Value for argument {} has already been set." , arg_name.str())
272
184
) {}
@@ -280,9 +192,7 @@ class invalid_value_error : public argument_parser_error {
280
192
* @param arg_name The name of the argument for which the value parsing failed.
281
193
* @param value The value that failed to parse.
282
194
*/
283
- explicit invalid_value_error (
284
- const argument::detail::argument_name& arg_name, const std::string& value
285
- )
195
+ explicit invalid_value_error (const detail::argument_name& arg_name, const std::string& value)
286
196
: argument_parser_error(
287
197
std::format (" Cannot parse value `{}` for argument {}." , value, arg_name.str())
288
198
) {}
@@ -296,9 +206,7 @@ class invalid_choice_error : public argument_parser_error {
296
206
* @param arg_name The name of the argument for which the value is not in choices.
297
207
* @param value The value that is not in the allowed choices.
298
208
*/
299
- explicit invalid_choice_error (
300
- const argument::detail::argument_name& arg_name, const std::string& value
301
- )
209
+ explicit invalid_choice_error (const detail::argument_name& arg_name, const std::string& value)
302
210
: argument_parser_error(
303
211
std::format (" Value `{}` is not a valid choice for argument {}." , value, arg_name.str())
304
212
) {}
@@ -311,7 +219,7 @@ class argument_name_used_error : public argument_parser_error {
311
219
* @brief Constructor for the argument_name_used_error class.
312
220
* @param arg_name The name of the argument causing the collision.
313
221
*/
314
- explicit argument_name_used_error (const argument:: detail::argument_name& arg_name)
222
+ explicit argument_name_used_error (const detail::argument_name& arg_name)
315
223
: argument_parser_error(std::format(" Given name `{}` already used." , arg_name.str())) {}
316
224
};
317
225
@@ -335,7 +243,7 @@ class invalid_value_type_error : public argument_parser_error {
335
243
* @param value_type The type information that failed to cast.
336
244
*/
337
245
explicit invalid_value_type_error (
338
- const argument:: detail::argument_name& arg_name, const std::type_info& value_type
246
+ const detail::argument_name& arg_name, const std::type_info& value_type
339
247
)
340
248
: argument_parser_error(std::format(
341
249
" Invalid value type specified for argument {} = {}." , arg_name.str(), value_type.name()
@@ -349,7 +257,7 @@ class required_argument_not_parsed_error : public argument_parser_error {
349
257
* @brief Constructor for the required_argument_not_parsed_error class.
350
258
* @param arg_name The name of the required argument that was not parsed.
351
259
*/
352
- explicit required_argument_not_parsed_error (const argument:: detail::argument_name& arg_name)
260
+ explicit required_argument_not_parsed_error (const detail::argument_name& arg_name)
353
261
: argument_parser_error(" No values parsed for a required argument " + arg_name.str()) {}
354
262
};
355
263
@@ -376,7 +284,7 @@ class invalid_nvalues_error : public argument_parser_error {
376
284
* @return The error message.
377
285
*/
378
286
[[nodiscard]] static std::string msg (
379
- const std::weak_ordering ordering, const argument:: detail::argument_name& arg_name
287
+ const std::weak_ordering ordering, const detail::argument_name& arg_name
380
288
) {
381
289
if (std::is_lt (ordering))
382
290
return " Too few values provided for optional argument " + arg_name.str ();
@@ -390,7 +298,7 @@ class invalid_nvalues_error : public argument_parser_error {
390
298
* @param arg_name The name of the argument for which the error occurred.
391
299
*/
392
300
explicit invalid_nvalues_error (
393
- const std::weak_ordering ordering, const argument:: detail::argument_name& arg_name
301
+ const std::weak_ordering ordering, const detail::argument_name& arg_name
394
302
)
395
303
: argument_parser_error(invalid_nvalues_error::msg(ordering, arg_name)) {}
396
304
};
@@ -617,7 +525,7 @@ class positional_argument : public detail::argument_interface {
617
525
* @brief Constructor for positional_argument with the `name` identifier.
618
526
* @param name The `name` identifier of the positional argument.
619
527
*/
620
- positional_argument (const detail::argument_name& name) : _name(name) {}
528
+ positional_argument (const ap:: detail::argument_name& name) : _name(name) {}
621
529
622
530
~positional_argument () = default ;
623
531
@@ -685,7 +593,7 @@ class positional_argument : public detail::argument_interface {
685
593
686
594
private:
687
595
// / @return Reference the name of the positional argument.
688
- [[nodiscard]] const detail::argument_name& name () const noexcept override {
596
+ [[nodiscard]] const ap:: detail::argument_name& name () const noexcept override {
689
597
return this ->_name ;
690
598
}
691
599
@@ -803,7 +711,7 @@ class positional_argument : public detail::argument_interface {
803
711
using action_type = ap::action::detail::action_variant_type<T>;
804
712
805
713
static constexpr bool _optional = false ;
806
- const detail::argument_name _name;
714
+ const ap:: detail::argument_name _name;
807
715
std::optional<std::string> _help_msg;
808
716
809
717
static constexpr bool _required = true ; // /< Positional arguments are required by default.
@@ -834,7 +742,7 @@ class optional_argument : public detail::argument_interface {
834
742
* @brief Constructor for optional_argument with the `name` identifier.
835
743
* @param name The `name` identifier of the optional argument.
836
744
*/
837
- optional_argument (const detail::argument_name& name) : _name(name) {}
745
+ optional_argument (const ap:: detail::argument_name& name) : _name(name) {}
838
746
839
747
~optional_argument () = default ;
840
748
@@ -970,7 +878,7 @@ class optional_argument : public detail::argument_interface {
970
878
971
879
private:
972
880
// / @return Reference to the name of the optional argument.
973
- [[nodiscard]] const detail::argument_name& name () const noexcept override {
881
+ [[nodiscard]] const ap:: detail::argument_name& name () const noexcept override {
974
882
return this ->_name ;
975
883
}
976
884
@@ -1112,7 +1020,7 @@ class optional_argument : public detail::argument_interface {
1112
1020
using action_type = ap::action::detail::action_variant_type<T>;
1113
1021
1114
1022
static constexpr bool _optional = true ;
1115
- const detail::argument_name _name;
1023
+ const ap:: detail::argument_name _name;
1116
1024
std::optional<std::string> _help_msg;
1117
1025
1118
1026
bool _required = false ;
@@ -1215,7 +1123,7 @@ class argument_parser {
1215
1123
argument::positional_argument<T>& add_positional_argument (std::string_view primary_name) {
1216
1124
// TODO: check forbidden characters
1217
1125
1218
- const argument ::detail::argument_name arg_name = {primary_name};
1126
+ const ap ::detail::argument_name arg_name = {primary_name};
1219
1127
if (this ->_is_arg_name_used (arg_name))
1220
1128
throw error::argument_name_used_error (arg_name);
1221
1129
@@ -1238,7 +1146,7 @@ class argument_parser {
1238
1146
) {
1239
1147
// TODO: check forbidden characters
1240
1148
1241
- const argument ::detail::argument_name arg_name = {primary_name, secondary_name};
1149
+ const ap ::detail::argument_name arg_name = {primary_name, secondary_name};
1242
1150
if (this ->_is_arg_name_used (arg_name))
1243
1151
throw error::argument_name_used_error (arg_name);
1244
1152
@@ -1258,7 +1166,7 @@ class argument_parser {
1258
1166
argument::optional_argument<T>& add_optional_argument (std::string_view primary_name) {
1259
1167
// TODO: check forbidden characters
1260
1168
1261
- const argument ::detail::argument_name arg_name = {primary_name};
1169
+ const ap ::detail::argument_name arg_name = {primary_name};
1262
1170
if (this ->_is_arg_name_used (arg_name))
1263
1171
throw error::argument_name_used_error (arg_name);
1264
1172
@@ -1279,7 +1187,7 @@ class argument_parser {
1279
1187
) {
1280
1188
// TODO: check forbidden characters
1281
1189
1282
- const argument ::detail::argument_name arg_name = {primary_name, secondary_name};
1190
+ const ap ::detail::argument_name arg_name = {primary_name, secondary_name};
1283
1191
if (this ->_is_arg_name_used (arg_name))
1284
1192
throw error::argument_name_used_error (arg_name);
1285
1193
@@ -1554,7 +1462,7 @@ class argument_parser {
1554
1462
* @return Argument predicate based on the provided name.
1555
1463
*/
1556
1464
[[nodiscard]] argument_predicate_type _name_match_predicate (
1557
- const argument ::detail::argument_name& arg_name
1465
+ const ap ::detail::argument_name& arg_name
1558
1466
) const noexcept {
1559
1467
return [&arg_name](const argument_ptr_type& arg) { return arg->name ().match (arg_name); };
1560
1468
}
@@ -1564,8 +1472,7 @@ class argument_parser {
1564
1472
* @param arg_name The name of the argument.
1565
1473
* @return True if the argument name is already used, false otherwise.
1566
1474
*/
1567
- [[nodiscard]] bool _is_arg_name_used (const argument::detail::argument_name& arg_name
1568
- ) const noexcept {
1475
+ [[nodiscard]] bool _is_arg_name_used (const ap::detail::argument_name& arg_name) const noexcept {
1569
1476
const auto predicate = this ->_name_match_predicate (arg_name);
1570
1477
1571
1478
if (std::ranges::find_if (this ->_positional_args , predicate) != this ->_positional_args .end ())
0 commit comments