Skip to content
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

Closed
ztdwu opened this issue Jun 13, 2016 · 2 comments
Closed

Required positional argument #4

ztdwu opened this issue Jun 13, 2016 · 2 comments

Comments

@ztdwu
Copy link

ztdwu commented Jun 13, 2016

I read through the guide and couldn't find anything on required positional arguments. For example:

./program {OPTIONS} <file> 

Here <file> would be a positional argument that must appear somewhere in the arguments.

@Taywee
Copy link
Owner

Taywee commented Jun 13, 2016

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 (a && b) || (c && d) means that you can only determine that the whole expression failed, not which combination was specified "incorrectly", especially if only group a and c were used, for example. It's even more complex because the group validation allows arbitrary validator functions, throwing useful diagnostics completely out the window. I would love to be able to spit out much more complete diagnostic messages for group validation failure, but it gets progressively more complex and requires much more code the more flexibility is given with groups, so I opted for the most flexibility, and even make the parser itself a group.

@Taywee Taywee closed this as completed Jun 13, 2016
@ztdwu
Copy link
Author

ztdwu commented Jun 13, 2016

I see, thanks!

@rhd rhd mentioned this issue Jul 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants