cli parsing

This commit is contained in:
Christoph J. Scherr 2023-10-18 15:29:56 +02:00
parent 72740547d1
commit ee15e3ccaa
2 changed files with 46 additions and 17 deletions

View File

View File

@ -1,40 +1,69 @@
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/positional_options.hpp>
#include <boost/program_options/variables_map.hpp>
#include <exception>
#include <iostream> #include <iostream>
#include "graphics.hpp" #include "graphics.hpp"
namespace po = boost::program_options; namespace po = boost::program_options;
using namespace std;
int main(int argc, char **argv);
void help(char *prog, po::options_description desc);
int main(int argc, char **argv) { int main(int argc, char **argv) {
printf("argc:\t\t%d\n", argc); /* Parsing CLI Arguments */
for (int i = 0; i < argc; i++) { po::options_description desc("Available options");
printf("argv[%d]:\t%s\n", i, argv[i]); desc.add_options()
} ("help,h", "show help")
std::cout << std::endl; ("verbose,v", "more verbose output")
("vert", po::value<string>()->required(), "vertex shader file")
("frag", po::value<string>()->required(), "fragment shader file");
po::options_description desc("Allowed options"); /* unused for now */
desc.add_options()("help", "produce help message")( po::positional_options_description pdesc;
"compression", po::value<int>(), "set compression level"); // NOTE: if you want to add a positional argument,
// you need to create and option for it as it seems.
pdesc.add("vert", 1);
pdesc.add("frag", 1);
po::variables_map vm; po::variables_map vm;
/* po::store(po::parse_command_line(ac, av, desc), vm); */ try {
po::notify(vm);
if (vm.count("help")) { po::store(po::command_line_parser(argc, argv)
std::cout << desc << "\n"; .options(desc) // load options
.positional(pdesc) // load positionals
.run(),
vm);
po::notify(vm);
if (vm.count("help") || argc < 3) {
help(argv[0], desc);
return 1;
}
} catch (po::error &e) {
cout << e.what() << endl << endl;
help(argv[0], desc);
return 1; return 1;
} }
if (vm.count("compression")) { if (vm.count("verbose")) {
std::cout << "Compression level was set to " << vm["compression"].as<int>() printf("verbose!");
<< ".\n";
} else {
std::cout << "Compression level was not set.\n";
} }
/* Run our main program */
int result = mainWindow(); int result = mainWindow();
return result; return result;
} }
void help(char *prog, po::options_description desc) {
cout << "Usage:" << endl
<< prog << " [-vh] [--vert] shaders/vertex.glsl [--frag] shaders/fragment.glsl"
<< endl << endl
<< desc << endl;
}