cli parsing
This commit is contained in:
parent
72740547d1
commit
ee15e3ccaa
63
src/main.cpp
63
src/main.cpp
|
@ -1,40 +1,69 @@
|
|||
#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 "graphics.hpp"
|
||||
|
||||
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) {
|
||||
|
||||
printf("argc:\t\t%d\n", argc);
|
||||
for (int i = 0; i < argc; i++) {
|
||||
printf("argv[%d]:\t%s\n", i, argv[i]);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
/* Parsing CLI Arguments */
|
||||
po::options_description desc("Available options");
|
||||
desc.add_options()
|
||||
("help,h", "show help")
|
||||
("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");
|
||||
desc.add_options()("help", "produce help message")(
|
||||
"compression", po::value<int>(), "set compression level");
|
||||
/* unused for now */
|
||||
po::positional_options_description pdesc;
|
||||
// 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::store(po::parse_command_line(ac, av, desc), vm); */
|
||||
po::notify(vm);
|
||||
try {
|
||||
|
||||
if (vm.count("help")) {
|
||||
std::cout << desc << "\n";
|
||||
po::store(po::command_line_parser(argc, argv)
|
||||
.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;
|
||||
}
|
||||
|
||||
if (vm.count("compression")) {
|
||||
std::cout << "Compression level was set to " << vm["compression"].as<int>()
|
||||
<< ".\n";
|
||||
} else {
|
||||
std::cout << "Compression level was not set.\n";
|
||||
if (vm.count("verbose")) {
|
||||
printf("verbose!");
|
||||
}
|
||||
|
||||
/* Run our main program */
|
||||
int result = mainWindow();
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue