1
- // Copyright (c) 2023-2024 Jakub Musiał
2
- // This file is part of the CPP-AP project (https://github.com/SpectraL519/cpp-ap).
3
- // Licensed under the MIT License. See the LICENSE file in the project root for full license information.
1
+ /*
2
+ CPP-AP: Command-line argument parser for C++20
3
+
4
+ MIT License
5
+
6
+ Copyright (c) 2023-2024 Jakub Musiał and other contributors
7
+ https://github.com/SpectraL519/cpp-ap
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in all
17
+ copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ SOFTWARE.
26
+ */
4
27
5
28
/* !
6
29
* @file argument_parser.hpp
7
- * @brief CPP-AP library header file.
30
+ * @brief CPP-AP library source file.
8
31
*
9
32
* This header file contians the entire CPP-AP library implementation.
10
33
*
11
- * @version 1.1
34
+ * @version 1.2
12
35
*/
13
36
14
37
#pragma once
@@ -55,22 +78,22 @@ namespace utility {
55
78
* @tparam T Type to check.
56
79
*/
57
80
template <typename T>
58
- concept readable = requires (T value, std::istream& input_stream) { input_stream >> value; };
81
+ concept c_readable = requires (T value, std::istream& input_stream) { input_stream >> value; };
59
82
60
83
/* *
61
- * @brief The concept is satisfied when `T` is readable , copy constructible and assignable.
84
+ * @brief The concept is satisfied when `T` is c_readable , copy constructible and assignable.
62
85
* @tparam T Type to check.
63
86
*/
64
87
template <typename T>
65
- concept valid_argument_value_type =
66
- readable <T> and std::copy_constructible<T> and std::assignable_from<T&, const T&>;
88
+ concept c_argument_value_type =
89
+ c_readable <T> and std::copy_constructible<T> and std::assignable_from<T&, const T&>;
67
90
68
91
/* *
69
92
* @brief The concept is satisfied when `T` is comparable using the equality operator `==`.
70
93
* @tparam T Type to check.
71
94
*/
72
95
template <typename T>
73
- concept equality_comparable = requires (T lhs, T rhs) {
96
+ concept c_equality_comparable = requires (T lhs, T rhs) {
74
97
{ lhs == rhs } -> std::convertible_to<bool >;
75
98
};
76
99
@@ -556,13 +579,13 @@ class range {
556
579
557
580
// / @brief Defines valued argument action traits.
558
581
struct valued_action {
559
- template <ap::utility::valid_argument_value_type T>
582
+ template <ap::utility::c_argument_value_type T>
560
583
using type = std::function<T(const T&)>;
561
584
};
562
585
563
586
// / @brief Defines void argument action traits.
564
587
struct void_action {
565
- template <ap::utility::valid_argument_value_type T>
588
+ template <ap::utility::c_argument_value_type T>
566
589
using type = std::function<void (T&)>;
567
590
};
568
591
@@ -581,15 +604,14 @@ namespace detail {
581
604
* @tparam AS The action specifier type.
582
605
*/
583
606
template <typename AS>
584
- concept valid_action_specifier =
585
- ap::utility::is_valid_type_v<AS, ap::valued_action, ap::void_action>;
607
+ concept c_action_specifier = ap::utility::is_valid_type_v<AS, ap::valued_action, ap::void_action>;
586
608
587
609
// / @brief Template argument action callable type alias.
588
- template <valid_action_specifier AS, ap::utility::valid_argument_value_type T>
610
+ template <c_action_specifier AS, ap::utility::c_argument_value_type T>
589
611
using callable_type = typename AS::template type<T>;
590
612
591
613
// / @brief Template argument action callabla variant type alias.
592
- template <ap::utility::valid_argument_value_type T>
614
+ template <ap::utility::c_argument_value_type T>
593
615
using action_variant_type =
594
616
std::variant<callable_type<ap::valued_action, T>, callable_type<ap::void_action, T>>;
595
617
@@ -599,15 +621,15 @@ using action_variant_type =
599
621
* @param action The action variant.
600
622
* @return True if the held action is a void action.
601
623
*/
602
- template <ap::utility::valid_argument_value_type T>
624
+ template <ap::utility::c_argument_value_type T>
603
625
[[nodiscard]] inline bool is_void_action (const action_variant_type<T>& action) noexcept {
604
626
return std::holds_alternative<callable_type<ap::void_action, T>>(action);
605
627
}
606
628
607
629
} // namespace detail
608
630
609
631
// / @brief Returns a default argument action.
610
- template <ap::utility::valid_argument_value_type T>
632
+ template <ap::utility::c_argument_value_type T>
611
633
detail::callable_type<ap::void_action, T> default_action () noexcept {
612
634
return [](T&) {};
613
635
}
@@ -629,7 +651,7 @@ namespace argument {
629
651
* @brief "Positional argument class of type T.
630
652
* @tparam T The type of the argument value.
631
653
*/
632
- template <utility::valid_argument_value_type T = std::string>
654
+ template <utility::c_argument_value_type T = std::string>
633
655
class positional_argument : public detail ::argument_interface {
634
656
public:
635
657
using value_type = T; // /< Type of the argument value.
@@ -672,7 +694,7 @@ class positional_argument : public detail::argument_interface {
672
694
* @note Requires T to be equality comparable.
673
695
*/
674
696
positional_argument& choices (const std::vector<value_type>& choices) noexcept
675
- requires(utility::equality_comparable <value_type>)
697
+ requires(utility::c_equality_comparable <value_type>)
676
698
{
677
699
this ->_choices = choices;
678
700
return *this ;
@@ -685,7 +707,7 @@ class positional_argument : public detail::argument_interface {
685
707
* @param action The action function to set.
686
708
* @return Reference to the positional_argument.
687
709
*/
688
- template <ap::action::detail::valid_action_specifier AS, std::invocable<value_type&> F>
710
+ template <ap::action::detail::c_action_specifier AS, std::invocable<value_type&> F>
689
711
positional_argument& action (F&& action) noexcept {
690
712
using callable_type = ap::action::detail::callable_type<AS, value_type>;
691
713
this ->_action = std::forward<callable_type>(action);
@@ -847,7 +869,7 @@ class positional_argument : public detail::argument_interface {
847
869
* @brief Optional argument class of type T.
848
870
* @tparam T The type of the argument value.
849
871
*/
850
- template <utility::valid_argument_value_type T = std::string>
872
+ template <utility::c_argument_value_type T = std::string>
851
873
class optional_argument : public detail ::argument_interface {
852
874
public:
853
875
using value_type = T;
@@ -940,7 +962,7 @@ class optional_argument : public detail::argument_interface {
940
962
* @param action The action function to set.
941
963
* @return Reference to the optional_argument.
942
964
*/
943
- template <ap::action::detail::valid_action_specifier AS, std::invocable<value_type&> F>
965
+ template <ap::action::detail::c_action_specifier AS, std::invocable<value_type&> F>
944
966
optional_argument& action (F&& action) noexcept {
945
967
using callable_type = ap::action::detail::callable_type<AS, value_type>;
946
968
this ->_action = std::forward<callable_type>(action);
@@ -954,7 +976,7 @@ class optional_argument : public detail::argument_interface {
954
976
* @note Requires T to be equality comparable.
955
977
*/
956
978
optional_argument& choices (const std::vector<value_type>& choices) noexcept
957
- requires(utility::equality_comparable <value_type>)
979
+ requires(utility::c_equality_comparable <value_type>)
958
980
{
959
981
this ->_choices = choices;
960
982
return *this ;
@@ -1239,7 +1261,7 @@ class argument_parser {
1239
1261
* @param primary_name The primary name of the argument.
1240
1262
* @return Reference to the added positional argument.
1241
1263
*/
1242
- template <utility::valid_argument_value_type T = std::string>
1264
+ template <utility::c_argument_value_type T = std::string>
1243
1265
argument::positional_argument<T>& add_positional_argument (std::string_view primary_name) {
1244
1266
// TODO: check forbidden characters
1245
1267
@@ -1259,7 +1281,7 @@ class argument_parser {
1259
1281
* @param secondary_name The secondary name of the argument.
1260
1282
* @return Reference to the added positional argument.
1261
1283
*/
1262
- template <utility::valid_argument_value_type T = std::string>
1284
+ template <utility::c_argument_value_type T = std::string>
1263
1285
argument::positional_argument<T>& add_positional_argument (
1264
1286
std::string_view primary_name, std::string_view secondary_name
1265
1287
) {
@@ -1280,7 +1302,7 @@ class argument_parser {
1280
1302
* @param primary_name The primary name of the argument.
1281
1303
* @return Reference to the added optional argument.
1282
1304
*/
1283
- template <utility::valid_argument_value_type T = std::string>
1305
+ template <utility::c_argument_value_type T = std::string>
1284
1306
argument::optional_argument<T>& add_optional_argument (std::string_view primary_name) {
1285
1307
// TODO: check forbidden characters
1286
1308
@@ -1299,7 +1321,7 @@ class argument_parser {
1299
1321
* @param secondary_name The secondary name of the argument.
1300
1322
* @return Reference to the added optional argument.
1301
1323
*/
1302
- template <utility::valid_argument_value_type T = std::string>
1324
+ template <utility::c_argument_value_type T = std::string>
1303
1325
argument::optional_argument<T>& add_optional_argument (
1304
1326
std::string_view primary_name, std::string_view secondary_name
1305
1327
) {
0 commit comments