diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2017-12-18 12:42:12 +0200 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2017-12-18 16:40:29 +0200 |
commit | 6a838eeffbc6ed30b1e5509b64ff50b69db8b75a (patch) | |
tree | 6ffdc403355e655cd02b0762d83624294e03b62e /bin | |
parent | f4021c4a2ee985e33bdd088985a22c591503d234 (diff) | |
download | qtlocation-mapboxgl-6a838eeffbc6ed30b1e5509b64ff50b69db8b75a.tar.gz |
[build] Replace boost_libprogram_options with args
Diffstat (limited to 'bin')
-rw-r--r-- | bin/offline.cpp | 76 | ||||
-rw-r--r-- | bin/render.cpp | 113 |
2 files changed, 100 insertions, 89 deletions
diff --git a/bin/offline.cpp b/bin/offline.cpp index ec2fb09096..5b403a6416 100644 --- a/bin/offline.cpp +++ b/bin/offline.cpp @@ -4,53 +4,63 @@ #include <mbgl/storage/default_file_source.hpp> +#include <args/args.hxx> + #include <cstdlib> #include <iostream> #include <csignal> #include <atomic> +#include <sstream> -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunknown-pragmas" -#pragma GCC diagnostic ignored "-Wunused-local-typedefs" -#pragma GCC diagnostic ignored "-Wshadow" -#include <boost/program_options.hpp> -#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<std::string> tokenValue(argumentParser, "key", "Mapbox access token", {'t', "token"}); + args::ValueFlag<std::string> styleValue(argumentParser, "URL", "Map stylesheet", {'s', "style"}); + args::ValueFlag<std::string> outputValue(argumentParser, "file", "Output database file name", {'o', "output"}); + + args::ValueFlag<double> northValue(argumentParser, "degrees", "North latitude", {"north"}); + args::ValueFlag<double> westValue(argumentParser, "degrees", "West longitude", {"west"}); + args::ValueFlag<double> southValue(argumentParser, "degrees", "South latitude", {"south"}); + args::ValueFlag<double> eastValue(argumentParser, "degrees", "East longitude", {"east"}); + args::ValueFlag<double> minZoomValue(argumentParser, "number", "Min zoom level", {"minZoom"}); + args::ValueFlag<double> maxZoomValue(argumentParser, "number", "Max zoom level", {"maxZoom"}); + args::ValueFlag<double> 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 + const double north = northValue ? args::get(northValue) : 37.2; + const double west = westValue ? args::get(westValue) : -122.8; + const double south = southValue ? args::get(southValue) : 38.1; + const double east = eastValue ? args::get(eastValue) : -121.7; + + const double minZoom = minZoomValue ? args::get(minZoomValue) : 0.0; + const double maxZoom = maxZoomValue ? args::get(maxZoomValue) : 15.0; + const double pixelRatio = pixelRatioValue ? args::get(pixelRatioValue) : 1.0; + const std::string output = outputValue ? args::get(outputValue) : "offline.db"; + + const char* tokenEnv = getenv("MAPBOX_ACCESS_TOKEN"); + const 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..6ceb32eb00 100644 --- a/bin/render.cpp +++ b/bin/render.cpp @@ -1,81 +1,82 @@ #include <mbgl/map/map.hpp> #include <mbgl/util/image.hpp> #include <mbgl/util/run_loop.hpp> +#include <mbgl/util/default_styles.hpp> #include <mbgl/gl/headless_frontend.hpp> #include <mbgl/util/default_thread_pool.hpp> #include <mbgl/storage/default_file_source.hpp> #include <mbgl/style/style.hpp> -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunknown-pragmas" -#pragma GCC diagnostic ignored "-Wunused-local-typedefs" -#pragma GCC diagnostic ignored "-Wshadow" -#include <boost/program_options.hpp> -#pragma GCC diagnostic pop - -namespace po = boost::program_options; +#include <args/args.hxx> #include <cstdlib> #include <iostream> #include <fstream> 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<std::string> tokenValue(argumentParser, "key", "Mapbox access token", {'t', "token"}); + args::ValueFlag<std::string> styleValue(argumentParser, "URL", "Map stylesheet", {'s', "style"}); + args::ValueFlag<std::string> outputValue(argumentParser, "file", "Output file name", {'o', "output"}); + args::ValueFlag<std::string> cacheValue(argumentParser, "file", "Cache database file name", {'c', "cache"}); + args::ValueFlag<std::string> assetsValue(argumentParser, "file", "Directory to which asset:// URLs will resolve", {'a', "assets"}); + + args::Flag debugFlag(argumentParser, "debug", "Debug mode", {"debug"}); + + args::ValueFlag<double> pixelRatioValue(argumentParser, "number", "Image scale factor", {'r', "ratio"}); + + args::ValueFlag<double> zoomValue(argumentParser, "number", "Zoom level", {'z', "zoom"}); + + args::ValueFlag<double> lonValue(argumentParser, "degrees", "Longitude", {'x', "lon"}); + args::ValueFlag<double> latValue(argumentParser, "degrees", "Latitude", {'y', "lat"}); + args::ValueFlag<double> bearingValue(argumentParser, "degrees", "Bearing", {'b', "bearing"}); + args::ValueFlag<double> pitchValue(argumentParser, "degrees", "Pitch", {'p', "pitch"}); + args::ValueFlag<uint32_t> widthValue(argumentParser, "pixels", "Image width", {'w', "width"}); + args::ValueFlag<uint32_t> 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; + const double lat = latValue ? args::get(latValue) : 0; + const double lon = lonValue ? args::get(lonValue) : 0; + const double zoom = zoomValue ? args::get(zoomValue) : 0; + const double bearing = bearingValue ? args::get(bearingValue) : 0; + const double pitch = pitchValue ? args::get(pitchValue) : 0; + const double pixelRatio = pixelRatioValue ? args::get(pixelRatioValue) : 1; + + const uint32_t width = widthValue ? args::get(widthValue) : 512; + const uint32_t height = heightValue ? args::get(heightValue) : 512; + const std::string output = outputValue ? args::get(outputValue) : "out.png"; + const std::string cache_file = cacheValue ? args::get(cacheValue) : "cache.sqlite"; + const std::string asset_root = assetsValue ? args::get(assetsValue) : "."; + + // Try to load the token from the environment. + const char* tokenEnv = getenv("MAPBOX_ACCESS_TOKEN"); + const std::string token = tokenValue ? args::get(tokenValue) : (tokenEnv ? tokenEnv : std::string()); + + const 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); |