Skip to content

Commit 5f402c3

Browse files
committed
Better error message when command line can not be parsed
Better error when there are no input files or when there is a problem with a command line option.
1 parent cd5bf8e commit 5f402c3

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

src/options.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,7 @@ const struct option long_options[] = {
9797
{"with-forward-dependencies", required_argument, nullptr, 217},
9898
{nullptr, 0, nullptr, 0}};
9999

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)
100+
static void long_usage(char const *arg0, bool verbose)
107101
{
108102
char const *const name = program_name(arg0);
109103

@@ -642,8 +636,7 @@ options_t::options_t(int argc, char *argv[]) : options_t()
642636
break;
643637
case '?':
644638
default:
645-
short_usage(argv[0]);
646-
break;
639+
throw std::runtime_error{"Usage error. Try 'osm2pgsql --help'."};
647640
}
648641
} //end while
649642

@@ -654,8 +647,9 @@ options_t::options_t(int argc, char *argv[]) : options_t()
654647
}
655648

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

661655
//get the input files

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)