Aim
This is a simple example to show an argument input parser. You can set default value, required or not, short name and full name.
Source
#include <iostream> #include "InputParser.h" // Usage: ./InputParser --input abc.txt // Usage: ./InputParser int main(int argc, char *argv[]) { InputParser parser; parser.AddOption("i", "input", true); // required value example parser.AddOption("n", "number", false, "20"); // default value example parser.AddOption("o", "output"); // optional value example if (!parser.DoParse(argc, argv, true)) { std::cout << "ERR not enough arguments" << std::endl; return 1; } std::cout << "input: " << parser["i"] << std::endl; std::cout << "number: " << std::stoi(parser["n"]) << std::endl; std::cout << "output: " << parser["o"] << std::endl; return 0; }
Output
$ ./src/InputParser --input abc.txt input: abc.txt number: 20 output: $ ./src/InputParser ERR: Option is needed --input ERR: Usage: YourApp [-o|--output] <output> [-i|--input] <input> [-n|--number] <number> ERR not enough arguments
沒有留言:
發佈留言