Skip to content

Commit f78d652

Browse files
committed
Use boost for parsing program options
1 parent 1633641 commit f78d652

File tree

4 files changed

+60
-2594
lines changed

4 files changed

+60
-2594
lines changed

CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ include(GNUInstallDirs)
1414

1515
find_package(PkgConfig REQUIRED)
1616
find_package(nlohmann_json 3.2.0 REQUIRED)
17+
find_package(Boost 1.30 COMPONENTS program_options REQUIRED)
1718

1819
pkg_check_modules(MARISA marisa IMPORTED_TARGET)
1920
pkg_check_modules(KYOTOCABINET kyotocabinet IMPORTED_TARGET)
@@ -33,6 +34,9 @@ set(HEAD
3334
include_directories(thirdparty/sqlite3pp/headeronly_src)
3435
include_directories(src)
3536

37+
# boost
38+
include_directories(${Boost_INCLUDE_DIR})
39+
3640
# importer
3741
set(IMPSRC
3842
importer/src/config.h
@@ -53,7 +57,8 @@ target_link_libraries(geocoder-importer
5357
PkgConfig::POSTAL
5458
PkgConfig::SQLITE3
5559
PkgConfig::LIBPQXX
56-
nlohmann_json::nlohmann_json)
60+
nlohmann_json::nlohmann_json
61+
${Boost_LIBRARIES})
5762

5863
# demo codes
5964
add_executable(geocoder-nlp

importer/src/main.cpp

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "normalization.h"
1010

1111
#include <algorithm>
12-
#include <boost/tokenizer.hpp>
12+
#include <boost/program_options.hpp>
1313
#include <cctype>
1414
#include <cstdlib>
1515
#include <deque>
@@ -23,44 +23,69 @@
2323
#include <set>
2424
#include <sqlite3pp.h>
2525

26-
using json = nlohmann::json;
26+
using json = nlohmann::json;
27+
namespace po = boost::program_options;
2728

2829
////////////////////////////////////////////////////////////////////////////
2930
// MAIN
3031

3132
int main(int argc, char *argv[])
3233
{
33-
if (argc == 2)
34-
{
35-
std::string option = argv[1];
36-
if (option == "--version")
37-
{
38-
std::cout << GeoNLP::Geocoder::version << "\n";
39-
return 0;
40-
}
41-
}
42-
43-
if (argc < 3)
44-
{
45-
std::cerr << "importer <poly.json> <geocoder-nlp database directory> "
46-
"[postal_country_parser_code] [address_parser_directory] [verbose]\n";
47-
std::cerr
48-
<< "When using optional parameters, you have to specify all of the perceiving ones\n";
49-
return 1;
50-
}
51-
52-
std::string polyjson = argv[1];
53-
std::string database_path = argv[2];
34+
std::string polyjson;
35+
std::string database_path;
5436
std::string postal_country_parser;
5537
std::string postal_address_parser_dir;
5638
bool verbose_address_expansion = false;
5739

58-
if (argc > 3)
59-
postal_country_parser = argv[3];
60-
if (argc > 4)
61-
postal_address_parser_dir = argv[4];
62-
if (argc > 5 && strcmp("verbose", argv[5]) == 0)
63-
verbose_address_expansion = true;
40+
{
41+
po::options_description generic("Geocoder NLP importer options");
42+
generic.add_options()("help", "Help message")("version,v", "Data format version")(
43+
"poly,p", po::value<std::string>(&polyjson),
44+
"Boundary of the imported region in GeoJSON format")(
45+
"postal-country", po::value<std::string>(&postal_country_parser),
46+
"libpostal country preference for this database")(
47+
"postal-address", po::value<std::string>(&postal_address_parser_dir),
48+
"libpostal address parser directory. If not specified, global libpostal parser directory "
49+
"preference is used.")("verbose", "Verbose address expansion");
50+
51+
po::options_description hidden("Hidden options");
52+
hidden.add_options()("output-directory", po::value<std::string>(&database_path),
53+
"Output directory for imported database");
54+
55+
po::positional_options_description p;
56+
p.add("output-directory", 1);
57+
58+
po::options_description cmdline_options;
59+
cmdline_options.add(generic).add(hidden);
60+
61+
po::variables_map vm;
62+
po::store(po::command_line_parser(argc, argv).options(cmdline_options).positional(p).run(), vm);
63+
po::notify(vm);
64+
65+
if (vm.count("help"))
66+
{
67+
std::cout << "Geocoder NLP importer:\n\n"
68+
<< "Call as\n\n " << argv[0] << " <options> output-directory\n"
69+
<< "\nwhere output-directory is a directory for imported database.\n\n"
70+
<< generic << "\n";
71+
return 0;
72+
}
73+
74+
if (vm.count(("version")))
75+
{
76+
std::cout << GeoNLP::Geocoder::version << "\n";
77+
return 0;
78+
}
79+
80+
if (vm.count("verbose"))
81+
verbose_address_expansion = true;
82+
83+
if (!vm.count("poly"))
84+
{
85+
std::cerr << "Boundary of the imported region in GeoJSON format is missing\n";
86+
return -1;
87+
}
88+
}
6489

6590
// load GeoJSON for surrounding (multi)polygon from poly.json
6691
std::string border;

0 commit comments

Comments
 (0)