Skip to content

Commit

Permalink
Merge pull request #1409 from joto/options-improvements
Browse files Browse the repository at this point in the history
Options improvements
  • Loading branch information
lonvia authored Feb 1, 2021
2 parents cd5bf8e + a0da5c5 commit d8aa8c6
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 20 deletions.
24 changes: 10 additions & 14 deletions src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include "config.h"
#include "format.hpp"
#include "logging.hpp"
#include "node-ram-cache.hpp"
#include "options.hpp"
#include "reprojection.hpp"
#include "sprompt.hpp"

#include <algorithm>
Expand Down Expand Up @@ -97,13 +99,7 @@ const struct option long_options[] = {
{"with-forward-dependencies", required_argument, nullptr, 217},
{nullptr, 0, nullptr, 0}};

void short_usage(char *arg0)
{
throw std::runtime_error{"Usage error. For further information call:"
" {} --help"_format(program_name(arg0))};
}

void long_usage(char const *arg0, bool verbose)
static void long_usage(char const *arg0, bool verbose)
{
char const *const name = program_name(arg0);

Expand Down Expand Up @@ -347,7 +343,7 @@ options_t::options_t(int argc, char *argv[]) : options_t()
{
// If there are no command line arguments at all, show help.
if (argc == 1) {
long_usage_bool = true;
m_print_help = true;
long_usage(argv[0], false);
return;
}
Expand Down Expand Up @@ -523,7 +519,7 @@ options_t::options_t(int argc, char *argv[]) : options_t()
}
break;
case 'h':
long_usage_bool = true;
m_print_help = true;
break;
case 'I':
parallel_indexing = false;
Expand Down Expand Up @@ -642,20 +638,20 @@ options_t::options_t(int argc, char *argv[]) : options_t()
break;
case '?':
default:
short_usage(argv[0]);
break;
throw std::runtime_error{"Usage error. Try 'osm2pgsql --help'."};
}
} //end while

//they were looking for usage info
if (long_usage_bool) {
if (m_print_help) {
long_usage(argv[0], help_verbose);
return;
}

//we require some input files!
if (argc == optind) {
short_usage(argv[0]);
if (optind >= argc) {
throw std::runtime_error{
"Missing input file(s). Try 'osm2pgsql --help'."};
}

//get the input files
Expand Down
17 changes: 13 additions & 4 deletions src/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
* For a full list of authors see the git log.
*/

#include "node-ram-cache.hpp"
#include "reprojection.hpp"

#include <osmium/osm/box.hpp>

#include <boost/optional.hpp>
#include <memory>
#include <string>
#include <vector>

class reprojection;

/// Variants for generation of hstore column
enum class hstore_column : char
{
Expand Down Expand Up @@ -61,6 +60,14 @@ class options_t
*/
options_t(int argc, char *argv[]);

/**
* Return true if the main program should end directly after the option
* parsing. This is true when a help text was printed.
*/
bool early_return() const noexcept {
return m_print_help;
}

std::string prefix{"planet_osm"}; ///< prefix for table names
std::shared_ptr<reprojection> projection; ///< SRS of projection
bool append = false; ///< Append to existing data
Expand Down Expand Up @@ -141,7 +148,6 @@ class options_t
boost::optional<std::string> tag_transform_rel_mem_func{boost::none};

bool create = false;
bool long_usage_bool = false;
bool pass_prompt = false;

database_options_t database_options;
Expand All @@ -161,6 +167,9 @@ class options_t
uint8_t way_node_index_id_shift = 0;

private:

bool m_print_help = false;

/**
* Check input options for sanity
*/
Expand Down
2 changes: 1 addition & 1 deletion src/osm2pgsql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ int main(int argc, char *argv[])
log_info("osm2pgsql version {}", get_osm2pgsql_version());

options_t const options{argc, argv};
if (options.long_usage_bool) {
if (options.early_return()) {
return 0;
}

Expand Down
1 change: 1 addition & 0 deletions src/output.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <osmium/index/id_set.hpp>

#include "options.hpp"
#include "osmtypes.hpp"
#include "thread-pool.hpp"

struct middle_query_t;
Expand Down
1 change: 1 addition & 0 deletions tests/common-options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

#include "options.hpp"
#include "reprojection.hpp"

#include "common-pg.hpp"

Expand Down
2 changes: 1 addition & 1 deletion tests/test-options-parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ TEST_CASE("Insufficient arguments", "[NoDB]")
{
std::vector<char const *> opts = {"osm2pgsql", "-a", "-c", "--slim"};
REQUIRE_THROWS_WITH(options_t((int)opts.size(), (char **)&opts[0]),
Catch::Matchers::Contains("Usage error"));
Catch::Matchers::Contains("Missing input"));
}

TEST_CASE("Incompatible arguments", "[NoDB]")
Expand Down

0 comments on commit d8aa8c6

Please sign in to comment.