Skip to content

Commit d8aa8c6

Browse files
authored
Merge pull request #1409 from joto/options-improvements
Options improvements
2 parents cd5bf8e + a0da5c5 commit d8aa8c6

File tree

6 files changed

+27
-20
lines changed

6 files changed

+27
-20
lines changed

src/options.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#include "config.h"
1111
#include "format.hpp"
1212
#include "logging.hpp"
13+
#include "node-ram-cache.hpp"
1314
#include "options.hpp"
15+
#include "reprojection.hpp"
1416
#include "sprompt.hpp"
1517

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

100-
void short_usage(char *arg0)
101-
{
102-
throw std::runtime_error{"Usage error. For further information call:"
103-
" {} --help"_format(program_name(arg0))};
104-
}
105-
106-
void long_usage(char const *arg0, bool verbose)
102+
static void long_usage(char const *arg0, bool verbose)
107103
{
108104
char const *const name = program_name(arg0);
109105

@@ -347,7 +343,7 @@ options_t::options_t(int argc, char *argv[]) : options_t()
347343
{
348344
// If there are no command line arguments at all, show help.
349345
if (argc == 1) {
350-
long_usage_bool = true;
346+
m_print_help = true;
351347
long_usage(argv[0], false);
352348
return;
353349
}
@@ -523,7 +519,7 @@ options_t::options_t(int argc, char *argv[]) : options_t()
523519
}
524520
break;
525521
case 'h':
526-
long_usage_bool = true;
522+
m_print_help = true;
527523
break;
528524
case 'I':
529525
parallel_indexing = false;
@@ -642,20 +638,20 @@ options_t::options_t(int argc, char *argv[]) : options_t()
642638
break;
643639
case '?':
644640
default:
645-
short_usage(argv[0]);
646-
break;
641+
throw std::runtime_error{"Usage error. Try 'osm2pgsql --help'."};
647642
}
648643
} //end while
649644

650645
//they were looking for usage info
651-
if (long_usage_bool) {
646+
if (m_print_help) {
652647
long_usage(argv[0], help_verbose);
653648
return;
654649
}
655650

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

661657
//get the input files

src/options.hpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010
* For a full list of authors see the git log.
1111
*/
1212

13-
#include "node-ram-cache.hpp"
14-
#include "reprojection.hpp"
15-
1613
#include <osmium/osm/box.hpp>
1714

1815
#include <boost/optional.hpp>
1916
#include <memory>
2017
#include <string>
2118
#include <vector>
2219

20+
class reprojection;
21+
2322
/// Variants for generation of hstore column
2423
enum class hstore_column : char
2524
{
@@ -61,6 +60,14 @@ class options_t
6160
*/
6261
options_t(int argc, char *argv[]);
6362

63+
/**
64+
* Return true if the main program should end directly after the option
65+
* parsing. This is true when a help text was printed.
66+
*/
67+
bool early_return() const noexcept {
68+
return m_print_help;
69+
}
70+
6471
std::string prefix{"planet_osm"}; ///< prefix for table names
6572
std::shared_ptr<reprojection> projection; ///< SRS of projection
6673
bool append = false; ///< Append to existing data
@@ -141,7 +148,6 @@ class options_t
141148
boost::optional<std::string> tag_transform_rel_mem_func{boost::none};
142149

143150
bool create = false;
144-
bool long_usage_bool = false;
145151
bool pass_prompt = false;
146152

147153
database_options_t database_options;
@@ -161,6 +167,9 @@ class options_t
161167
uint8_t way_node_index_id_shift = 0;
162168

163169
private:
170+
171+
bool m_print_help = false;
172+
164173
/**
165174
* Check input options for sanity
166175
*/

src/osm2pgsql.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ int main(int argc, char *argv[])
6161
log_info("osm2pgsql version {}", get_osm2pgsql_version());
6262

6363
options_t const options{argc, argv};
64-
if (options.long_usage_bool) {
64+
if (options.early_return()) {
6565
return 0;
6666
}
6767

src/output.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <osmium/index/id_set.hpp>
2323

2424
#include "options.hpp"
25+
#include "osmtypes.hpp"
2526
#include "thread-pool.hpp"
2627

2728
struct middle_query_t;

tests/common-options.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
#include "options.hpp"
14+
#include "reprojection.hpp"
1415

1516
#include "common-pg.hpp"
1617

tests/test-options-parse.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ TEST_CASE("Insufficient arguments", "[NoDB]")
3636
{
3737
std::vector<char const *> opts = {"osm2pgsql", "-a", "-c", "--slim"};
3838
REQUIRE_THROWS_WITH(options_t((int)opts.size(), (char **)&opts[0]),
39-
Catch::Matchers::Contains("Usage error"));
39+
Catch::Matchers::Contains("Missing input"));
4040
}
4141

4242
TEST_CASE("Incompatible arguments", "[NoDB]")

0 commit comments

Comments
 (0)