Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3d4f277

Browse files
committedFeb 1, 2024
Add default argument methods
1 parent dde42cf commit 3d4f277

File tree

4 files changed

+10831
-4839
lines changed

4 files changed

+10831
-4839
lines changed
 

‎README.md

+8
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ parser.run_and_exit_if_error();
8686

8787
The only difference is that the `run_and_exit_if_error` method does not provide overloads for passing custom output and error streams. The `parse` method has overloads to support such scenarios. By default `std::cout` is used the regular output, e.g., the integrated help. Also `std::cerr` is used for displaying error messages.
8888

89+
### Default Arguments
90+
To set and get default arguments (that do not need a name), use the `set_default` and `get_default` methods.
91+
92+
```cpp
93+
parser.set_default<std::string>(false, "May be optional or required depending on the first parameter", "Default value");
94+
const auto default_argument = parser.get_default<std::string>();
95+
```
96+
8997
## Integrated help
9098

9199
The parser comes with a pre-defined command that has the shorthand `-h` and the longhand `--help`. This is the integrated help, which appears if only a single command line argument is given, which happens to be either the shorthand or longhand form.

‎cmdparser.Test/catch.hpp

+10,785-4,838
Large diffs are not rendered by default.

‎cmdparser.Test/tests.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,38 @@ TEST_CASE( "Parse required bool provided but inverted", "[required] [bool] [avai
149149

150150
REQUIRE(ret == false);
151151
}
152+
TEST_CASE( "Parse default argument", "[default]") {
153+
std::stringstream output { };
154+
std::stringstream errors { };
155+
156+
const char* args[2] = {
157+
"myapp",
158+
"default_arg"
159+
};
160+
Parser parser(2, args);
161+
parser.set_default<std::string>(true, "default argument");
162+
const auto value = parser.run(output, errors);
163+
REQUIRE(value == true);
164+
REQUIRE(parser.app_name() == "myapp");
165+
const auto ret = parser.get_default<std::string>();
166+
REQUIRE(ret == "default_arg");
167+
}
152168

169+
TEST_CASE( "Parse default argument with default value", "[default`] [missing]") {
170+
std::stringstream output { };
171+
std::stringstream errors { };
172+
173+
const char* args[1] = {
174+
"myapp"
175+
};
176+
Parser parser(1, args);
177+
parser.set_default<std::string>(false, "Optional default argument", "default_value");
178+
const auto value = parser.run(output, errors);
179+
REQUIRE(value == true);
180+
REQUIRE(parser.app_name() == "myapp");
181+
const auto ret = parser.get_default<std::string>();
182+
REQUIRE(ret == "default_value");
183+
}
153184
TEST_CASE( "Parse required bool", "[required] [bool] [missing]" ) {
154185
std::stringstream output { };
155186
std::stringstream errors { };

‎cmdparser.hpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,9 @@ namespace cli {
358358
}
359359

360360
template<typename T>
361-
void set_default(bool is_required, const std::string& description = "") {
361+
void set_default(bool is_required, const std::string& description = "", T defaultValue = T()) {
362362
auto command = new CmdArgument<T> { "", "", description, is_required, false };
363+
command->value = defaultValue;
363364
_commands.push_back(command);
364365
}
365366

@@ -488,6 +489,11 @@ namespace cli {
488489
throw std::runtime_error("The parameter " + name + " could not be found.");
489490
}
490491

492+
template<typename T>
493+
T get_default() {
494+
return get<T>("");
495+
}
496+
491497
template<typename T>
492498
T get_if(const std::string& name, std::function<T(T)> callback) const {
493499
auto value = get<T>(name);

0 commit comments

Comments
 (0)
Please sign in to comment.