Skip to content

Commit beed365

Browse files
authored
YT-CPPAP-44: Type-flexible argument paremeter setters
Aligned the setter functions of the `default_value` and `implicit_value` argument parameters to accept as arguments instances of any type convertible to the argument's value type.
1 parent eb80332 commit beed365

File tree

5 files changed

+15
-10
lines changed

5 files changed

+15
-10
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ else()
77
endif()
88

99
project(cpp-ap
10-
VERSION 2.3.0
10+
VERSION 2.3.1
1111
DESCRIPTION "Command-line argument parser for C++20"
1212
HOMEPAGE_URL "https://github.com/SpectraL519/cpp-ap"
1313
LANGUAGES CXX

Doxyfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ PROJECT_NAME = CPP-AP
4848
# could be handy for archiving the generated documentation or if some version
4949
# control system is used.
5050

51-
PROJECT_NUMBER = 2.3.0
51+
PROJECT_NUMBER = 2.3.1
5252

5353
# Using the PROJECT_BRIEF tag one can provide an optional one line description
5454
# for a project that appears at the top of each page and should give viewer a

docs/tutorial.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ Command Result
313313
./program -i input.txt -o myfile.txt Parsing success; Printing data to the `myfile.txt` file
314314
```
315315
316+
> [!TIP]
317+
>
318+
> The setter of the `default_value` parameter accepts any type that is convertible to the argument's value type.
319+
316320
#### 5. `choices` - A list of valid argument values.
317321
318322
The `choices` parameter takes as an argument an instance of `std::initializer_list` or any `std::ranges::range` type such that its value type is convertible to the argument's `value_type`.
@@ -367,7 +371,7 @@ Actions are represented as functions, which take the argument's value as an argu
367371
368372
> [!TIP]
369373
>
370-
> A single argument can have multiple value actions. Instead of writing complex logic in one action, consider composing several simple, focused actions for better readability and reuse.
374+
> A single argument can have multiple value actions. Instead of writing complex logic in one action, consider composing several simple, focused actions for better readability and reusability.
371375
372376
<br/>
373377
@@ -434,7 +438,8 @@ Command Result
434438
435439
> [!TIP]
436440
>
437-
> The `implicit_value` parameter is extremely useful when combined with default value (e.g. in case of boolean flags - see [Adding Arguments](#adding-arguments)).
441+
> - The `implicit_value` parameter is extremely useful when combined with default value (e.g. in case of boolean flags - see [Adding Arguments](#adding-arguments)).
442+
> - The setter of the `implicit_value` parameter accepts any type that is convertible to the argument's value type.
438443
439444
#### 4. On-flag actions - For optional arguments, apart from value actions, you can specify on-flag actions which are executed immediately after parsing an argument's flag.
440445

include/ap/argument/optional.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ class optional : public detail::argument_base {
174174
* @return Reference to the optional argument.
175175
* @attention Setting the default value disables the `required` flag.
176176
*/
177-
optional& default_value(const value_type& default_value) noexcept {
178-
this->_default_value = default_value;
177+
optional& default_value(const std::convertible_to<value_type> auto& default_value) noexcept {
178+
this->_default_value = std::make_any<value_type>(default_value);
179179
this->_required = false;
180180
return *this;
181181
}
@@ -185,8 +185,8 @@ class optional : public detail::argument_base {
185185
* @param implicit_value The implicit value to set.
186186
* @return Reference to the optional argument.
187187
*/
188-
optional& implicit_value(const value_type& implicit_value) noexcept {
189-
this->_implicit_value = implicit_value;
188+
optional& implicit_value(const std::convertible_to<value_type> auto& implicit_value) noexcept {
189+
this->_implicit_value = std::make_any<value_type>(implicit_value);
190190
return *this;
191191
}
192192

include/ap/argument/positional.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ class positional : public detail::argument_base {
120120
* @return Reference to the positional argument.
121121
* @attention Setting the default value disables the `required` flag.
122122
*/
123-
positional& default_value(const value_type& default_value) noexcept {
124-
this->_default_value = default_value;
123+
positional& default_value(const std::convertible_to<value_type> auto& default_value) noexcept {
124+
this->_default_value = std::make_any<value_type>(default_value);
125125
this->_required = false;
126126
return *this;
127127
}

0 commit comments

Comments
 (0)