From f8e0bc6b35fbc04d35b829a2ba8906e4a5357904 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 18 Dec 2017 12:42:12 +0200 Subject: [build] Replace boost_libprogram_options with args --- bin/offline.cpp | 75 ++++++++++++++++------------- bin/render.cpp | 113 ++++++++++++++++++++++---------------------- cmake/offline.cmake | 2 +- cmake/render.cmake | 2 +- platform/linux/config.cmake | 2 +- platform/macos/config.cmake | 2 +- 6 files changed, 103 insertions(+), 93 deletions(-) diff --git a/bin/offline.cpp b/bin/offline.cpp index ec2fb09096..ecd46e4220 100644 --- a/bin/offline.cpp +++ b/bin/offline.cpp @@ -4,53 +4,62 @@ #include +#include + #include #include #include #include -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunknown-pragmas" -#pragma GCC diagnostic ignored "-Wunused-local-typedefs" -#pragma GCC diagnostic ignored "-Wshadow" -#include -#pragma GCC diagnostic pop - -namespace po = boost::program_options; using namespace std::literals::chrono_literals; int main(int argc, char *argv[]) { - std::string style = mbgl::util::default_styles::streets.url; - double north = 37.2, west = -122.8, south = 38.1, east = -121.7; // Bay area - double minZoom = 0.0, maxZoom = 15.0, pixelRatio = 1.0; - std::string output = "offline.db"; + args::ArgumentParser argumentParser("Mapbox GL offline tool"); + args::HelpFlag helpFlag(argumentParser, "help", "Display this help menu", {'h', "help"}); - const char* tokenEnv = getenv("MAPBOX_ACCESS_TOKEN"); - std::string token = tokenEnv ? tokenEnv : std::string(); - - po::options_description desc("Allowed options"); - desc.add_options() - ("style,s", po::value(&style)->value_name("URL"), "Map stylesheet") - ("north", po::value(&north)->value_name("degrees")->default_value(north), "North latitude") - ("west", po::value(&west)->value_name("degrees")->default_value(west), "West longitude") - ("south", po::value(&south)->value_name("degrees")->default_value(south), "South latitude") - ("east", po::value(&east)->value_name("degrees")->default_value(east), "East longitude") - ("minZoom", po::value(&minZoom)->value_name("number")->default_value(minZoom), "Min zoom level") - ("maxZoom", po::value(&maxZoom)->value_name("number")->default_value(maxZoom), "Max zoom level") - ("pixelRatio", po::value(&pixelRatio)->value_name("number")->default_value(pixelRatio), "Pixel ratio") - ("token,t", po::value(&token)->value_name("key")->default_value(token), "Mapbox access token") - ("output,o", po::value(&output)->value_name("file")->default_value(output), "Output database file name") - ; + args::ValueFlag tokenValue(argumentParser, "key", "Mapbox access token", {'t', "token"}); + args::ValueFlag styleValue(argumentParser, "URL", "Map stylesheet", {'s', "style"}); + args::ValueFlag outputValue(argumentParser, "file", "Output database file name", {'o', "output"}); + + args::ValueFlag northValue(argumentParser, "degrees", "North latitude", {"north"}); + args::ValueFlag westValue(argumentParser, "degrees", "West longitude", {"west"}); + args::ValueFlag southValue(argumentParser, "degrees", "South latitude", {"south"}); + args::ValueFlag eastValue(argumentParser, "degrees", "East longitude", {"east"}); + args::ValueFlag minZoomValue(argumentParser, "number", "Min zoom level", {"minZoom"}); + args::ValueFlag maxZoomValue(argumentParser, "number", "Max zoom level", {"maxZoom"}); + args::ValueFlag pixelRatioValue(argumentParser, "number", "Pixel ratio", {"pixelRatio"}); try { - po::variables_map vm; - po::store(po::parse_command_line(argc, argv, desc), vm); - po::notify(vm); - } catch(std::exception& e) { - std::cout << "Error: " << e.what() << std::endl << desc; + argumentParser.ParseCLI(argc, argv); + } catch (args::Help) { + std::cout << argumentParser; + exit(0); + } catch (args::ParseError e) { + std::cerr << e.what() << std::endl; + std::cerr << argumentParser; exit(1); + } catch (args::ValidationError e) { + std::cerr << e.what() << std::endl; + std::cerr << argumentParser; + exit(2); } + std::string style = styleValue ? args::get(styleValue) : mbgl::util::default_styles::streets.url; + + // Bay area + double north = northValue ? args::get(northValue) : 37.2; + double west = westValue ? args::get(westValue) : -122.8; + double south = southValue ? args::get(southValue) : 38.1; + double east = eastValue ? args::get(eastValue) : -121.7; + + double minZoom = minZoomValue ? args::get(minZoomValue) : 0.0; + double maxZoom = maxZoomValue ? args::get(maxZoomValue) : 15.0; + double pixelRatio = pixelRatioValue ? args::get(pixelRatioValue) : 1.0; + std::string output = outputValue ? args::get(outputValue) : "offline.db"; + + const char* tokenEnv = getenv("MAPBOX_ACCESS_TOKEN"); + std::string token = tokenValue ? args::get(tokenValue) : (tokenEnv ? tokenEnv : std::string()); + using namespace mbgl; util::RunLoop loop; diff --git a/bin/render.cpp b/bin/render.cpp index a13b981d77..3d472e1aa4 100644 --- a/bin/render.cpp +++ b/bin/render.cpp @@ -1,81 +1,82 @@ #include #include #include +#include #include #include #include #include -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunknown-pragmas" -#pragma GCC diagnostic ignored "-Wunused-local-typedefs" -#pragma GCC diagnostic ignored "-Wshadow" -#include -#pragma GCC diagnostic pop - -namespace po = boost::program_options; +#include #include #include #include int main(int argc, char *argv[]) { - std::string style_path; - double lat = 0, lon = 0; - double zoom = 0; - double bearing = 0; - double pitch = 0; - double pixelRatio = 1; - - uint32_t width = 512; - uint32_t height = 512; - static std::string output = "out.png"; - std::string cache_file = "cache.sqlite"; - std::string asset_root = "."; - std::string token; - bool debug = false; - - po::options_description desc("Allowed options"); - desc.add_options() - ("style,s", po::value(&style_path)->required()->value_name("json"), "Map stylesheet") - ("lon,x", po::value(&lon)->value_name("degrees")->default_value(lon), "Longitude") - ("lat,y", po::value(&lat)->value_name("degrees")->default_value(lat), "Latitude in degrees") - ("zoom,z", po::value(&zoom)->value_name("number")->default_value(zoom), "Zoom level") - ("bearing,b", po::value(&bearing)->value_name("degrees")->default_value(bearing), "Bearing") - ("pitch,p", po::value(&pitch)->value_name("degrees")->default_value(pitch), "Pitch") - ("width,w", po::value(&width)->value_name("pixels")->default_value(width), "Image width") - ("height,h", po::value(&height)->value_name("pixels")->default_value(height), "Image height") - ("ratio,r", po::value(&pixelRatio)->value_name("number")->default_value(pixelRatio), "Image scale factor") - ("token,t", po::value(&token)->value_name("key")->default_value(token), "Mapbox access token") - ("debug", po::bool_switch(&debug)->default_value(debug), "Debug mode") - ("output,o", po::value(&output)->value_name("file")->default_value(output), "Output file name") - ("cache,d", po::value(&cache_file)->value_name("file")->default_value(cache_file), "Cache database file name") - ("assets,d", po::value(&asset_root)->value_name("file")->default_value(asset_root), "Directory to which asset:// URLs will resolve") - ; + args::ArgumentParser argumentParser("Mapbox GL render tool"); + args::HelpFlag helpFlag(argumentParser, "help", "Display this help menu", {"help"}); + + args::ValueFlag tokenValue(argumentParser, "key", "Mapbox access token", {'t', "token"}); + args::ValueFlag styleValue(argumentParser, "URL", "Map stylesheet", {'s', "style"}); + args::ValueFlag outputValue(argumentParser, "file", "Output file name", {'o', "output"}); + args::ValueFlag cacheValue(argumentParser, "file", "Cache database file name", {'c', "cache"}); + args::ValueFlag assetsValue(argumentParser, "file", "Directory to which asset:// URLs will resolve", {'a', "assets"}); + + args::Flag debugFlag(argumentParser, "debug", "Debug mode", {"debug"}); + + args::ValueFlag pixelRatioValue(argumentParser, "number", "Image scale factor", {'r', "ratio"}); + + args::ValueFlag zoomValue(argumentParser, "number", "Zoom level", {'z', "zoom"}); + + args::ValueFlag lonValue(argumentParser, "degrees", "Longitude", {'x', "lon"}); + args::ValueFlag latValue(argumentParser, "degrees", "Latitude", {'y', "lat"}); + args::ValueFlag bearingValue(argumentParser, "degrees", "Bearing", {'b', "bearing"}); + args::ValueFlag pitchValue(argumentParser, "degrees", "Pitch", {'p', "pitch"}); + args::ValueFlag widthValue(argumentParser, "pixels", "Image width", {'w', "width"}); + args::ValueFlag heightValue(argumentParser, "pixels", "Image height", {'h', "height"}); try { - po::variables_map vm; - po::store(po::parse_command_line(argc, argv, desc), vm); - po::notify(vm); - } catch(std::exception& e) { - std::cout << "Error: " << e.what() << std::endl << desc; + argumentParser.ParseCLI(argc, argv); + } catch (args::Help) { + std::cout << argumentParser; + exit(0); + } catch (args::ParseError e) { + std::cerr << e.what() << std::endl; + std::cerr << argumentParser; exit(1); + } catch (args::ValidationError e) { + std::cerr << e.what() << std::endl; + std::cerr << argumentParser; + exit(2); } + std::string style = styleValue ? args::get(styleValue) : mbgl::util::default_styles::streets.url; + double lat = latValue ? args::get(latValue) : 0; + double lon = lonValue ? args::get(lonValue) : 0; + double zoom = zoomValue ? args::get(zoomValue) : 0; + double bearing = bearingValue ? args::get(bearingValue) : 0; + double pitch = pitchValue ? args::get(pitchValue) : 0; + double pixelRatio = pixelRatioValue ? args::get(pixelRatioValue) : 1; + + uint32_t width = widthValue ? args::get(widthValue) : 512; + uint32_t height = heightValue ? args::get(heightValue) : 512; + static std::string output = outputValue ? args::get(outputValue) : "out.png"; + std::string cache_file = cacheValue ? args::get(cacheValue) : "cache.sqlite"; + std::string asset_root = assetsValue ? args::get(assetsValue) : "."; + + // Try to load the token from the environment. + const char* tokenEnv = getenv("MAPBOX_ACCESS_TOKEN"); + std::string token = tokenValue ? args::get(tokenValue) : (tokenEnv ? tokenEnv : std::string()); + + bool debug = debugFlag ? args::get(debugFlag) : false; + using namespace mbgl; util::RunLoop loop; DefaultFileSource fileSource(cache_file, asset_root); - // Try to load the token from the environment. - if (!token.size()) { - const char *token_ptr = getenv("MAPBOX_ACCESS_TOKEN"); - if (token_ptr) { - token = token_ptr; - } - } - // Set access token if present if (token.size()) { fileSource.setAccessToken(std::string(token)); @@ -85,11 +86,11 @@ int main(int argc, char *argv[]) { HeadlessFrontend frontend({ width, height }, pixelRatio, fileSource, threadPool); Map map(frontend, MapObserver::nullObserver(), frontend.getSize(), pixelRatio, fileSource, threadPool, MapMode::Static); - if (style_path.find("://") == std::string::npos) { - style_path = std::string("file://") + style_path; + if (style.find("://") == std::string::npos) { + style = std::string("file://") + style; } - map.getStyle().loadURL(style_path); + map.getStyle().loadURL(style); map.setLatLngZoom({ lat, lon }, zoom); map.setBearing(bearing); map.setPitch(pitch); diff --git a/cmake/offline.cmake b/cmake/offline.cmake index e2691fdfaf..13b6c3fe70 100644 --- a/cmake/offline.cmake +++ b/cmake/offline.cmake @@ -15,7 +15,7 @@ target_link_libraries(mbgl-offline ) target_add_mason_package(mbgl-offline PRIVATE boost) -target_add_mason_package(mbgl-offline PRIVATE boost_libprogram_options) +target_add_mason_package(mbgl-offline PRIVATE args) mbgl_platform_offline() diff --git a/cmake/render.cmake b/cmake/render.cmake index 40f624f7ca..c6e7d9dd59 100644 --- a/cmake/render.cmake +++ b/cmake/render.cmake @@ -11,8 +11,8 @@ target_link_libraries(mbgl-render ) target_add_mason_package(mbgl-render PRIVATE boost) -target_add_mason_package(mbgl-render PRIVATE boost_libprogram_options) target_add_mason_package(mbgl-render PRIVATE geojson) +target_add_mason_package(mbgl-render PRIVATE args) mbgl_platform_render() diff --git a/platform/linux/config.cmake b/platform/linux/config.cmake index 6027b1cd55..6de571e88b 100644 --- a/platform/linux/config.cmake +++ b/platform/linux/config.cmake @@ -1,6 +1,5 @@ mason_use(glfw VERSION 2017-07-13-67c9155) mason_use(mesa VERSION 13.0.4) -mason_use(boost_libprogram_options VERSION 1.62.0${MASON_CXXABI_SUFFIX}) mason_use(sqlite VERSION 3.14.2) mason_use(libuv VERSION 1.9.1) mason_use(nunicode VERSION 1.7.1) @@ -10,6 +9,7 @@ mason_use(webp VERSION 0.5.1) mason_use(gtest VERSION 1.8.0${MASON_CXXABI_SUFFIX}) mason_use(benchmark VERSION 1.2.0) mason_use(icu VERSION 58.1-min-size) +mason_use(args VERSION 6.2.0 HEADER_ONLY) include(cmake/loop-uv.cmake) diff --git a/platform/macos/config.cmake b/platform/macos/config.cmake index 3b7d112455..e929bb55c6 100644 --- a/platform/macos/config.cmake +++ b/platform/macos/config.cmake @@ -1,10 +1,10 @@ set(CMAKE_OSX_DEPLOYMENT_TARGET 10.10) mason_use(glfw VERSION 2017-07-13-67c9155) -mason_use(boost_libprogram_options VERSION 1.62.0) mason_use(gtest VERSION 1.8.0) mason_use(benchmark VERSION 1.2.0) mason_use(icu VERSION 58.1-min-size) +mason_use(args VERSION 6.2.0 HEADER_ONLY) include(cmake/loop-uv.cmake) include(cmake/loop-darwin.cmake) -- cgit v1.2.1