-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Required positional argument #4
Comments
In order to make the positional required, you would put it in a group that requires all of its arguments: $ cat test.cxx
#include <iostream>
#include <args.hxx>
int main(int argc, char **argv)
{
args::ArgumentParser parser("This is a test program.");
args::HelpFlag help(parser, "help", "Display this help menu", {'h', "help"});
args::Group required(parser, "", args::Group::Validators::All);
args::Positional<std::string> file(required, "file", "This file is required");
try
{
parser.ParseCLI(argc, argv);
}
catch (args::Help)
{
std::cout << parser;
return 0;
}
catch (args::Error e)
{
std::cerr << e.what() << std::endl;
std::cerr << parser;
return 1;
}
std::cout << "File input: " << args::get(file) << std::endl;
return 0;
}
$ g++ -otest test.cxx -std=c++11
$ ./test
Group validation failed somewhere!
./test {OPTIONS} [file]
This is a test program.
OPTIONS:
-h, --help Display this help menu
file This file is required
"--" can be used to terminate flag options and force all following
arguments to be treated as positional options
$ ./test -h
./test {OPTIONS} [file]
This is a test program.
OPTIONS:
-h, --help Display this help menu
file This file is required
"--" can be used to terminate flag options and force all following
arguments to be treated as positional options
$ ./test testfile.txt
File input: testfile.txt
$ I do apologize that the group validation exception isn't easier to decode, but with the incredible complexity allowed in group nesting, it's impossible to print out exactly what went wrong with the validation, because situations like |
I see, thanks! |
I read through the guide and couldn't find anything on required positional arguments. For example:
Here
<file>
would be a positional argument that must appear somewhere in the arguments.The text was updated successfully, but these errors were encountered: