Skip to content

Commit

Permalink
Better error messages for command line errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
joto committed Jan 19, 2017
1 parent 5945175 commit 22cc6fb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Changed

- Build with warnings in all build types, not only "Dev".
- Better error messages for command line errors.

### Fixed

- Make `--overwrite` and `--fsync` work in `derive_changes` command.
- A dereference of end iterator in `derive_changes`.
- You can not specify the special file name "-" (to read from STDIN) several
times for commands reading multiple files.


## [1.5.0] - 2017-01-18
Expand Down
6 changes: 3 additions & 3 deletions src/cmd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class with_single_osm_input {

protected:

std::string m_input_filename = "-"; // default: stdin
std::string m_input_filename;
std::string m_input_format;
osmium::io::File m_input_file;

Expand Down Expand Up @@ -160,7 +160,7 @@ class with_multiple_osm_inputs {

public:

void setup_input_files(const boost::program_options::variables_map& vm, bool optional = false);
void setup_input_files(const boost::program_options::variables_map& vm);

po::options_description add_multiple_inputs_options();

Expand All @@ -178,7 +178,7 @@ class with_osm_output {

std::string m_generator;
std::vector<std::string> m_output_headers;
std::string m_output_filename = "-"; // default: stdout
std::string m_output_filename;
std::string m_output_format;
osmium::io::File m_output_file;
osmium::io::overwrite m_output_overwrite = osmium::io::overwrite::no;
Expand Down
2 changes: 2 additions & 0 deletions src/command_apply_changes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ bool CommandApplyChanges::setup(const std::vector<std::string>& arguments) {

if (vm.count("change-filenames")) {
m_change_filenames = vm["change-filenames"].as<std::vector<std::string>>();
} else {
throw argument_error{"Need data file and at least one change file on the command line."};
}

if (vm.count("with-history")) {
Expand Down
57 changes: 37 additions & 20 deletions src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,19 @@ void with_single_osm_input::setup_input_file(const boost::program_options::varia
m_input_format = vm["input-format"].as<std::string>();
}

if ((m_input_filename == "-" || m_input_filename == "") && m_input_format.empty()) {
throw argument_error{"When reading from STDIN you need to use the --input-format/-F option to declare the file format."};
if (m_input_format.empty()) {
if (m_input_filename == "-") {
throw argument_error{"When reading from STDIN you need to use the --input-format/-F option\n"
"to specify the file format."};
}

if (m_input_filename == "") {
throw argument_error{"Missing input file. Use '-' to read from STDIN and add the --input-format/-F\n"
"option to specify the file format or specify the input file name."};
}
}

m_input_file = osmium::io::File(m_input_filename, m_input_format);
m_input_file = osmium::io::File{m_input_filename, m_input_format};
}

po::options_description with_single_osm_input::add_single_input_options() {
Expand All @@ -70,31 +78,34 @@ void with_single_osm_input::show_single_input_arguments(osmium::util::VerboseOut
vout << " file format: " << m_input_format << "\n";
}

void with_multiple_osm_inputs::setup_input_files(const boost::program_options::variables_map& vm, bool optional) {
void with_multiple_osm_inputs::setup_input_files(const boost::program_options::variables_map& vm) {
if (vm.count("input-filenames")) {
m_input_filenames = vm["input-filenames"].as<std::vector<std::string>>();
} else if (!optional) {
} else {
m_input_filenames.push_back("-"); // default is stdin
}

bool uses_stdin = false;
for (auto& filename : m_input_filenames) {
if (filename == "-") {
if (uses_stdin) {
throw argument_error{"Can read at most one file from STDIN."};
}
uses_stdin = true;
}
}

if (vm.count("input-format")) {
m_input_format = vm["input-format"].as<std::string>();
}

if (m_input_format.empty()) {
bool uses_stdin = false;
for (auto& filename : m_input_filenames) {
if (filename.empty() || filename == "-") {
uses_stdin = true;
}
}
if (uses_stdin) {
throw argument_error{"When reading from STDIN you need to use the --input-format/-F option to declare the file format."};
}
if (uses_stdin && m_input_format.empty()) {
throw argument_error{"When reading from STDIN you need to use the --input-format/-F option\n"
"to specify the file format. Or are you missing a file name argument?"};
}

for (const std::string& input_filename : m_input_filenames) {
osmium::io::File input_file(input_filename, m_input_format);
osmium::io::File input_file{input_filename, m_input_format};
m_input_files.push_back(input_file);
}
}
Expand Down Expand Up @@ -146,12 +157,18 @@ void with_osm_output::init_output_file(const po::variables_map& vm) {
}

void with_osm_output::check_output_file() {
if ((m_output_filename == "-" || m_output_filename == "") && m_output_format.empty()) {
throw argument_error{"When writing to STDOUT you need to use the --output-format/-f option\n"
"to declare the file format. Or did you miss the --output/-O option?"};
if (m_output_format.empty()) {
if (m_output_filename == "-") {
throw argument_error{"When writing to STDOUT you need to use the --output-format/-f\n"
"option to specify the file format."};
}
if (m_output_filename == "") {
throw argument_error{"Missing output file. Set the output file with --output/-o and/or\n"
"add the --input-format/-F option to specify the file format."};
}
}

m_output_file = osmium::io::File(m_output_filename, m_output_format);
m_output_file = osmium::io::File{m_output_filename, m_output_format};
m_output_file.check();
}

Expand Down

0 comments on commit 22cc6fb

Please sign in to comment.