Argument Parser for Modern C++ | Fix Xmake and Modules
Go to file
Pranav Srinivas Kumar 0ceeeb353f
Update README.md
2019-03-30 18:45:54 -04:00
src More unit tests. Added overloaded parse_args method for testing purposes 2019-03-30 16:45:44 -04:00
tests Unit test to parse and construct a vector of objects of some class 2019-03-30 17:26:25 -04:00
.gitignore Added visual studio gitignore 2019-03-30 16:59:57 -04:00
LICENSE Added LICENSE 2019-03-29 18:41:37 -04:00
README.md Update README.md 2019-03-30 18:45:54 -04:00

Argument Parser

Simple Arguments

#include <argparse.hpp>

int main(int argc, char *argv[]) {
  argparse::ArgumentParser program("main");
  
  program.add_argument("config")
    .help("configuration file")
    .default_value([]() { return "config.yml" });
    
  program.add_argument("n", "-n", "--num_iterations")
    .help("The list of input files")
    .action([](const std::string& value) { return std::stoi(value); });
    
  program.parse(argc, argv);
  std::string config = program.get("config");
  int num_iterations = program.get<int>("n");  
  
  return 0;
}

Parsing a list of arguments

#include <argparse.hpp>

int main(int argc, char *argv[]) {
  argparse::ArgumentParser program("main");
  
  program.add_argument("--input_files")
    .help("The list of input files")
    .nargs(3);
    
  program.parse(argc, argv);
  std::vector<std::string> files = program.get<std::vector<std::string>>("--input_files");  
  
  return 0;
}