Use ArgumentParser

This commit is contained in:
Matt McWilliams 2024-09-02 17:50:19 -04:00
parent b9ee2180c9
commit c73d9fe698
1 changed files with 47 additions and 15 deletions

View File

@ -1,5 +1,6 @@
#include "image.hpp" #include "image.hpp"
#include "state.hpp" #include "state.hpp"
#include "args/args.hxx"
#ifdef __APPLE__ #ifdef __APPLE__
#include <OpenGL/gl.h> #include <OpenGL/gl.h>
@ -26,6 +27,7 @@ using namespace std::chrono;
uint16_t PORT = 8081; uint16_t PORT = 8081;
const uint16_t BUFFER_SIZE = 2048; const uint16_t BUFFER_SIZE = 2048;
int clientSocket = 0; int clientSocket = 0;
unsigned int refreshMs = 1;
GLuint imageTexture = 0; GLuint imageTexture = 0;
GLuint blankTexture = 0; GLuint blankTexture = 0;
@ -203,7 +205,7 @@ void display () {
void timer(int value) { void timer(int value) {
glutPostRedisplay(); glutPostRedisplay();
glutTimerFunc(1, timer, 0); glutTimerFunc(refreshMs, timer, 0);
} }
/** /**
@ -260,33 +262,63 @@ void runTCPServer (int serverSocket) {
} }
} }
uint8_t handleArgs (int argc, char** argv) { int main (int argc, char** argv) {
if (argc > 1) { args::ArgumentParser parser("filmout_display", "Displays image on a screen");
screenWidth = atoi(argv[1]); args::HelpFlag help(parser, "help", "Display this help menu", {'h', "help"});
args::ValueFlag<int> width(parser, "width", "Screen width (required)", {'w', "width"});
args::ValueFlag<int> height(parser, "height", "Screen height (required)", {'H', "height"});
args::ValueFlag<uint16_t> port(parser, "port", "Socket port (default: 8081)", {'p', "port"});
args::ValueFlag<int> refresh(parser, "refresh", "Refresh timer in ms (default: 1)", {'r', "refresh"});
try
{
parser.ParseCLI(argc, argv);
}
catch (args::Help)
{
std::cout << parser;
return 0;
}
catch (args::ParseError e)
{
std::cerr << e.what() << std::endl;
std::cerr << parser;
return 1;
}
catch (args::ValidationError e)
{
std::cerr << e.what() << std::endl;
std::cerr << parser;
return 1;
}
if (width) {
screenWidth = args::get(width);
cout << "{ \"screen_width\" : " << screenWidth << " }" << endl; cout << "{ \"screen_width\" : " << screenWidth << " }" << endl;
} else { } else {
cerr << "{ \"error\" : \"Please provide screen width as first argument\" }" << endl; cerr << "{ \"error\" : \"Please provide screen width as first argument\" }" << endl;
return 1; return 1;
} }
if (argc > 2) {
screenHeight = atoi(argv[2]); if (height) {
screenHeight = args::get(height);
cout << "{ \"screen_height\" : " << screenHeight << " } " << endl; cout << "{ \"screen_height\" : " << screenHeight << " } " << endl;
} else { } else {
cerr << "{ \"error\" : \"Please provide screen height as second argument\" }" << endl; cerr << "{ \"error\" : \"Please provide screen height as second argument\" }" << endl;
return 2; return 2;
} }
if (argc > 3) {
PORT = (uint16_t) atoi(argv[3]); if (port) {
PORT = args::get(port);
cout << "{ \"port\" : " << PORT << " }" << endl; cout << "{ \"port\" : " << PORT << " }" << endl;
} }
return 0;
}
int main (int argc, char** argv) { if (refresh) {
uint8_t argsRet = handleArgs(argc, argv); refreshMs = args::get(refresh);
if (refreshMs < 1) {
if (argsRet > 0) { refreshMs = 1;
return argsRet; }
cout << "{ \"refresh\" : " << refreshMs << " }" << endl;
} }
img.setDimensions( (uint16_t) screenWidth, (uint16_t) screenHeight); img.setDimensions( (uint16_t) screenWidth, (uint16_t) screenHeight);