From f606183442b3548a47649d55fe32ff06d04958e0 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 9 Apr 2025 20:53:45 +0100 Subject: Add boost_program_options for command line parsing --- Jamroot.jam | 3 ++- application/resviewer.cpp | 39 ++++++++++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/Jamroot.jam b/Jamroot.jam index 7585ec7..8589861 100644 --- a/Jamroot.jam +++ b/Jamroot.jam @@ -12,6 +12,7 @@ pkg-config.import mxml : : shared ; pkg-config.import assimp : : shared ; lib pthread : : shared ; lib OpenMeshCore : : shared ; +lib boost_program_options : : shared ; variant coverage : debug ; project i-like-trains : requirements @@ -47,7 +48,7 @@ project i-like-trains : requirements ; exe iliketrains : application/main.cpp : ilt ; -exe resviewer : application/resviewer.cpp : ilt ; +exe resviewer : application/resviewer.cpp : ilt boost_program_options ; explicit main ; always main ; diff --git a/application/resviewer.cpp b/application/resviewer.cpp index e512b41..7656b29 100644 --- a/application/resviewer.cpp +++ b/application/resviewer.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -23,7 +24,8 @@ constexpr GlobalDistance MAX_CAMERA_DIST = 30'000; class ViewerContent : public WindowContent, SceneRenderer, SceneProvider { public: - ViewerContent(ScreenAbsCoord size, std::span files) : SceneRenderer {size, 0}, fileList(files) + ViewerContent(ScreenAbsCoord size, std::span files) : + SceneRenderer {size, 0}, fileList(files) { camera.setPosition(calcCameraPosition()); camera.lookAt({0, 0, TERRAIN_HEIGHT + cameraFocus}); @@ -94,12 +96,12 @@ private: fileSelection() { ImGui::BeginListBox("File"); - for (const auto file : fileList) { - if (ImGui::Selectable(file, file == selectedFile)) { + for (const auto & file : fileList) { + if (ImGui::Selectable(file.c_str(), &file == selectedFile)) { location.reset(); selectedAsset = nullptr; gameState->assets = AssetFactory::loadXML(file)->assets; - selectedFile = file; + selectedFile = &file; } } ImGui::EndListBox(); @@ -149,8 +151,8 @@ private: } } - std::span fileList; - char * selectedFile {}; + std::span fileList; + const std::filesystem::path * selectedFile {}; const Renderable * selectedAsset {}; Location position {.pos = {0, 0, TERRAIN_HEIGHT}, .rot = {}}; std::any location; @@ -167,7 +169,7 @@ main(int argc, char ** argv) class ResViewer : GameState, MainApplication { public: void - run(std::span fileList) + run(std::span fileList) { windows.create(DEFAULT_WINDOW_SIZE, "ILT - Resource Viewer") ->setContent(fileList); @@ -175,7 +177,26 @@ main(int argc, char ** argv) } }; - std::span files {argv, static_cast(argc)}; + namespace po = boost::program_options; + po::options_description opts("ILT - Resource Viewer"); + std::vector resources; + // clang-format off + opts.add_options() + ("resource,r", po::value(&resources)->composing(), "Resource file") + ("help,h", po::value()->default_value(false)->zero_tokens(), "Help") + ; + // clang-format on + po::positional_options_description pod; + pod.add("resource", -1); + po::variables_map varmap; + po::store(po::command_line_parser(argc, argv).options(opts).positional(pod).run(), varmap); + po::notify(varmap); + + if (varmap.at("help").as()) { + std::cout << opts << '\n'; + return EXIT_SUCCESS; + } - ResViewer {}.run(files.subspan(2)); + ResViewer {}.run(resources); + return EXIT_SUCCESS; } -- cgit v1.2.3