check(), capture_default_str() and default_str() not working? #989
Replies: 3 comments 9 replies
-
here is my main int main(int argc, char *argv[])
{
CLI::App app{"MyApp"};
std::filesystem::path binpath1 = "../example/myfile.bin";
std::filesystem::path binpath2 = "../example/myfile.bin";
std::filesystem::path binpath3 = "../example/myfile.bin";
app.add_option("-f,--file1", binpath1, "Path to a .bin file"); // this works but the path "../example/myfile.bin" won't show up in --help. I need it to show somewhere that this is a required argument but it's populated with a default value - running the app.exe without the arg will work and use the default value, but the user can overwrite it.
app.add_option("-g,--file2", binpath2, "Path to a .bin file")->capture_default_str(); // this doesn't seem to change anything, and I don't understand the documentation, it's not clear to me what it says.
app.add_option("-i,--file3", binpath3, "Path to a .bin file")->default_str("defFile.bin"); // this doesn't seem to change anything either, and I also don't understand the documentation, it's not clear to me what it should do or change.
try
{
app.parse(argc, argv);
} catch (const CLI::ParseError& e)
{
return app.exit(e);
}
} Here is the output for help
so I think the capture_default_str which pulls the default string from the variable, and default_str which allows you to set it directly is working as expected. |
Beta Was this translation helpful? Give feedback.
-
If I set the third option to app.add_option("-i,--file3", binpath3, "Path to a .bin file")->default_str("defFile.bin")->check(CLI::ExistingFile); now when I run with -i not_a_file.bin I get --file3: File does not exist: not_a_file.bin
Run with --help for more information. the check only checks when an option is given, it does not check the variable exists unless |
Beta Was this translation helpful? Give feedback.
-
If those are now showing up, that sounds like a bug. Does it work with just plain std::string? We do have a few tests for std::filesystem, but not a lot, and possibly it behaves differently on Windows. |
Beta Was this translation helpful? Give feedback.
-
Hi,
I'm a new (potential) CLI11 user, and I struggle quite a bit with the readme / docs.
I have an app with a couple of command-line arguments that are paths to files. These paths should have default values (e.g.
"../example/myfile.bin"
), but be overrideable with a command-line argument likemyapp.exe --file c:/folder/another_file.bin
. I'd also likemyapp.exe --help
to show the default value.I've tried a few things:
I've also been trying to use
app.add_option("-f,--file", binpath, "Path to a .bin file")->check(CLI::ExistingFile);
but if I give it a non-existing file, it just happily continues - no error or warning, like I would expect. What am I missing?I've also been trying to use
app.add_option("-f,--file", binpath, "Path to a .bin file")->option_text("<file>")
. This works and changes "TEXT" in the--help
screen to "". However, when I start my app with the argument missing, e.g.myapp.exe -f
, then it outputs:--file: 1 required TEXT missing Run with --help for more information.
, and it hasn't replaced "TEXT" with "". Is this a bug or am I doing something wrong?Thanks a lot!
(If it's relevant, I'm on Windows 11, latest VS / MSVC, CLI11 from vcpkg, using CMake, and /std:c++23.)
Beta Was this translation helpful? Give feedback.
All reactions