[osmium-tool] 25/44: Use exceptions to communicate errors from Command setup() functions.
Sebastiaan Couwenberg
sebastic at moszumanska.debian.org
Tue Jul 21 20:15:55 UTC 2015
This is an automated email from the git hooks/post-receive script.
sebastic pushed a commit to tag v1.1.0
in repository osmium-tool.
commit ffb514e14269e64f47214a30435b0715d53a7b94
Author: Jochen Topf <jochen at topf.org>
Date: Sun Jun 28 10:05:41 2015 +0200
Use exceptions to communicate errors from Command setup() functions.
This removes some code duplication and make these functions easiert
to test.
---
src/command_apply_changes.cpp | 125 ++++++++++++++---------------
src/command_cat.cpp | 144 ++++++++++++++++-----------------
src/command_check_refs.cpp | 80 +++++++++----------
src/command_fileinfo.cpp | 182 +++++++++++++++++++-----------------------
src/command_fileinfo.hpp | 1 -
src/command_merge_changes.cpp | 107 ++++++++++++-------------
src/command_time_filter.cpp | 132 ++++++++++++++----------------
src/main.cpp | 12 ++-
src/osmc.hpp | 21 ++++-
9 files changed, 390 insertions(+), 414 deletions(-)
diff --git a/src/command_apply_changes.cpp b/src/command_apply_changes.cpp
index 42758e2..036075e 100644
--- a/src/command_apply_changes.cpp
+++ b/src/command_apply_changes.cpp
@@ -34,88 +34,81 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
bool CommandApplyChanges::setup(const std::vector<std::string>& arguments) {
namespace po = boost::program_options;
po::variables_map vm;
- try {
- po::options_description cmdline("Allowed options");
- cmdline.add_options()
- ("verbose,v", "Set verbose mode")
- ("output,o", po::value<std::string>(), "Output file")
- ("output-format,f", po::value<std::string>(), "Format of output file")
- ("input-format,F", po::value<std::string>(), "Format of input file")
- ("simplify,s", "Simplify change")
- ("remove-deleted,r", "Remove deleted objects from output")
- ("generator", po::value<std::string>(), "Generator setting for file header")
- ("overwrite,O", "Allow existing output file to be overwritten")
- ;
-
- po::options_description hidden("Hidden options");
- hidden.add_options()
- ("input-filename", po::value<std::string>(), "OSM input file")
- ("change-filenames", po::value<std::vector<std::string>>(), "OSM change input files")
- ;
-
- po::options_description desc("Allowed options");
- desc.add(cmdline).add(hidden);
-
- po::positional_options_description positional;
- positional.add("input-filename", 1);
- positional.add("change-filenames", -1);
-
- po::store(po::command_line_parser(arguments).options(desc).positional(positional).run(), vm);
- po::notify(vm);
-
- if (vm.count("input-filename")) {
- m_input_filename = vm["input-filename"].as<std::string>();
- }
- if (vm.count("change-filenames")) {
- m_change_filenames = vm["change-filenames"].as<std::vector<std::string>>();
- }
+ po::options_description cmdline("Allowed options");
+ cmdline.add_options()
+ ("verbose,v", "Set verbose mode")
+ ("output,o", po::value<std::string>(), "Output file")
+ ("output-format,f", po::value<std::string>(), "Format of output file")
+ ("input-format,F", po::value<std::string>(), "Format of input file")
+ ("simplify,s", "Simplify change")
+ ("remove-deleted,r", "Remove deleted objects from output")
+ ("generator", po::value<std::string>(), "Generator setting for file header")
+ ("overwrite,O", "Allow existing output file to be overwritten")
+ ;
+
+ po::options_description hidden("Hidden options");
+ hidden.add_options()
+ ("input-filename", po::value<std::string>(), "OSM input file")
+ ("change-filenames", po::value<std::vector<std::string>>(), "OSM change input files")
+ ;
+
+ po::options_description desc("Allowed options");
+ desc.add(cmdline).add(hidden);
+
+ po::positional_options_description positional;
+ positional.add("input-filename", 1);
+ positional.add("change-filenames", -1);
+
+ po::store(po::command_line_parser(arguments).options(desc).positional(positional).run(), vm);
+ po::notify(vm);
+
+ if (vm.count("input-filename")) {
+ m_input_filename = vm["input-filename"].as<std::string>();
+ }
- if (vm.count("output")) {
- m_output_filename = vm["output"].as<std::string>();
- }
+ if (vm.count("change-filenames")) {
+ m_change_filenames = vm["change-filenames"].as<std::vector<std::string>>();
+ }
- if (vm.count("input-format")) {
- m_input_format = vm["input-format"].as<std::string>();
- }
+ if (vm.count("output")) {
+ m_output_filename = vm["output"].as<std::string>();
+ }
- if (vm.count("output-format")) {
- m_output_format = vm["output-format"].as<std::string>();
- }
+ if (vm.count("input-format")) {
+ m_input_format = vm["input-format"].as<std::string>();
+ }
- if (vm.count("overwrite")) {
- m_output_overwrite = osmium::io::overwrite::allow;
- }
+ if (vm.count("output-format")) {
+ m_output_format = vm["output-format"].as<std::string>();
+ }
- if (vm.count("simplify")) {
- m_simplify_change = true;
- }
+ if (vm.count("overwrite")) {
+ m_output_overwrite = osmium::io::overwrite::allow;
+ }
- if (vm.count("remove-deleted")) {
- m_remove_deleted = true;
- }
+ if (vm.count("simplify")) {
+ m_simplify_change = true;
+ }
- if (vm.count("generator")) {
- m_generator = vm["generator"].as<std::string>();
- }
+ if (vm.count("remove-deleted")) {
+ m_remove_deleted = true;
+ }
- if (vm.count("verbose")) {
- m_vout.verbose(true);
- }
+ if (vm.count("generator")) {
+ m_generator = vm["generator"].as<std::string>();
+ }
- } catch (boost::program_options::error& e) {
- std::cerr << "Error parsing command line: " << e.what() << std::endl;
- return false;
+ if (vm.count("verbose")) {
+ m_vout.verbose(true);
}
if ((m_output_filename == "-" || m_output_filename == "") && m_output_format.empty()) {
- std::cerr << "When writing to STDOUT you need to use the --output-format,f option to declare the file format.\n";
- return false;
+ throw argument_error("When writing to STDOUT you need to use the --output-format,f option to declare the file format.");
}
if ((m_input_filename == "-" || m_input_filename == "") && m_input_format.empty()) {
- std::cerr << "When reading from STDIN you need to use the --input-format,F option to declare the file format.\n";
- return false;
+ throw argument_error("When reading from STDIN you need to use the --input-format,F option to declare the file format.");
}
m_output_file = osmium::io::File(m_output_filename, m_output_format);
diff --git a/src/command_cat.cpp b/src/command_cat.cpp
index 6b44cb7..3605f49 100644
--- a/src/command_cat.cpp
+++ b/src/command_cat.cpp
@@ -33,88 +33,82 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
bool CommandCat::setup(const std::vector<std::string>& arguments) {
namespace po = boost::program_options;
po::variables_map vm;
- try {
- po::options_description cmdline("Allowed options");
- cmdline.add_options()
- ("verbose,v", "Set verbose mode")
- ("output,o", po::value<std::string>(), "Output file")
- ("output-format,f", po::value<std::string>(), "Format of output file")
- ("input-format,F", po::value<std::string>(), "Format of input files")
- ("generator", po::value<std::string>(), "Generator setting for file header")
- ("output-header", po::value<std::vector<std::string>>(), "Add output header")
- ("overwrite,O", "Allow existing output file to be overwritten")
- ("object-type,t", po::value<std::vector<std::string>>(), "Read only objects of given type (node, way, relation, changeset)")
- ;
-
- po::options_description hidden("Hidden options");
- hidden.add_options()
- ("input-filenames", po::value<std::vector<std::string>>(), "Input files")
- ;
-
- po::options_description desc("Allowed options");
- desc.add(cmdline).add(hidden);
-
- po::positional_options_description positional;
- positional.add("input-filenames", -1);
-
- po::store(po::command_line_parser(arguments).options(desc).positional(positional).run(), vm);
- po::notify(vm);
-
- if (vm.count("verbose")) {
- m_vout.verbose(true);
- }
- if (vm.count("generator")) {
- m_generator = vm["generator"].as<std::string>();
- }
+ po::options_description cmdline("Allowed options");
+ cmdline.add_options()
+ ("verbose,v", "Set verbose mode")
+ ("output,o", po::value<std::string>(), "Output file")
+ ("output-format,f", po::value<std::string>(), "Format of output file")
+ ("input-format,F", po::value<std::string>(), "Format of input files")
+ ("generator", po::value<std::string>(), "Generator setting for file header")
+ ("output-header", po::value<std::vector<std::string>>(), "Add output header")
+ ("overwrite,O", "Allow existing output file to be overwritten")
+ ("object-type,t", po::value<std::vector<std::string>>(), "Read only objects of given type (node, way, relation, changeset)")
+ ;
+
+ po::options_description hidden("Hidden options");
+ hidden.add_options()
+ ("input-filenames", po::value<std::vector<std::string>>(), "Input files")
+ ;
+
+ po::options_description desc("Allowed options");
+ desc.add(cmdline).add(hidden);
+
+ po::positional_options_description positional;
+ positional.add("input-filenames", -1);
+
+ po::store(po::command_line_parser(arguments).options(desc).positional(positional).run(), vm);
+ po::notify(vm);
+
+ if (vm.count("verbose")) {
+ m_vout.verbose(true);
+ }
- if (vm.count("input-filenames")) {
- m_input_filenames = vm["input-filenames"].as<std::vector<std::string>>();
- } else {
- m_input_filenames.push_back("-"); // default is stdin
- }
+ if (vm.count("generator")) {
+ m_generator = vm["generator"].as<std::string>();
+ }
- if (vm.count("output")) {
- m_output_filename = vm["output"].as<std::string>();
- }
+ if (vm.count("input-filenames")) {
+ m_input_filenames = vm["input-filenames"].as<std::vector<std::string>>();
+ } else {
+ m_input_filenames.push_back("-"); // default is stdin
+ }
- if (vm.count("input-format")) {
- m_input_format = vm["input-format"].as<std::string>();
- }
+ if (vm.count("output")) {
+ m_output_filename = vm["output"].as<std::string>();
+ }
- if (vm.count("output-format")) {
- m_output_format = vm["output-format"].as<std::string>();
- }
+ if (vm.count("input-format")) {
+ m_input_format = vm["input-format"].as<std::string>();
+ }
- if (vm.count("output-header")) {
- m_output_headers = vm["output-header"].as<std::vector<std::string>>();
- }
+ if (vm.count("output-format")) {
+ m_output_format = vm["output-format"].as<std::string>();
+ }
- if (vm.count("overwrite")) {
- m_output_overwrite = osmium::io::overwrite::allow;
- }
+ if (vm.count("output-header")) {
+ m_output_headers = vm["output-header"].as<std::vector<std::string>>();
+ }
- if (vm.count("object-type")) {
- m_osm_entity_bits = osmium::osm_entity_bits::nothing;
- for (const auto& t : vm["object-type"].as<std::vector<std::string>>()) {
- if (t == "node") {
- m_osm_entity_bits |= osmium::osm_entity_bits::node;
- } else if (t == "way") {
- m_osm_entity_bits |= osmium::osm_entity_bits::way;
- } else if (t == "relation") {
- m_osm_entity_bits |= osmium::osm_entity_bits::relation;
- } else if (t == "changeset") {
- m_osm_entity_bits |= osmium::osm_entity_bits::changeset;
- } else {
- std::cerr << "Unknown object type '" << t << "' (Allowed are 'node', 'way', 'relation', and 'changeset').\n";
- return false;
- }
+ if (vm.count("overwrite")) {
+ m_output_overwrite = osmium::io::overwrite::allow;
+ }
+
+ if (vm.count("object-type")) {
+ m_osm_entity_bits = osmium::osm_entity_bits::nothing;
+ for (const auto& t : vm["object-type"].as<std::vector<std::string>>()) {
+ if (t == "node") {
+ m_osm_entity_bits |= osmium::osm_entity_bits::node;
+ } else if (t == "way") {
+ m_osm_entity_bits |= osmium::osm_entity_bits::way;
+ } else if (t == "relation") {
+ m_osm_entity_bits |= osmium::osm_entity_bits::relation;
+ } else if (t == "changeset") {
+ m_osm_entity_bits |= osmium::osm_entity_bits::changeset;
+ } else {
+ throw argument_error(std::string("Unknown object type '") + t + "' (Allowed are 'node', 'way', 'relation', and 'changeset').");
}
}
-
- } catch (boost::program_options::error& e) {
- std::cerr << "Error parsing command line: " << e.what() << std::endl;
- return false;
}
m_vout << "Started osmium cat\n";
@@ -148,8 +142,7 @@ bool CommandCat::setup(const std::vector<std::string>& arguments) {
m_vout << "\n";
if ((m_output_filename == "-" || m_output_filename == "") && m_output_format.empty()) {
- std::cerr << "When writing to STDOUT you need to use the --output-format,f option to declare the file format.\n";
- return false;
+ throw argument_error("When writing to STDOUT you need to use the --output-format,f option to declare the file format.");
}
if (m_input_format.empty()) {
@@ -160,8 +153,7 @@ bool CommandCat::setup(const std::vector<std::string>& arguments) {
}
}
if (uses_stdin) {
- std::cerr << "When reading from STDIN you need to use the --input-format,F option to declare the file format.\n";
- return false;
+ throw argument_error("When reading from STDIN you need to use the --input-format,F option to declare the file format.");
}
}
diff --git a/src/command_check_refs.cpp b/src/command_check_refs.cpp
index 61b53d7..579279c 100644
--- a/src/command_check_refs.cpp
+++ b/src/command_check_refs.cpp
@@ -37,52 +37,47 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
bool CommandCheckRefs::setup(const std::vector<std::string>& arguments) {
namespace po = boost::program_options;
po::variables_map vm;
- try {
- po::options_description cmdline("Allowed options");
- cmdline.add_options()
- ("verbose,v", "Set verbose mode")
- ("show-ids,i", "Show IDs of missing objects")
- ("input-format,F", po::value<std::string>(), "Format of input files")
- ("check-relations,r", "Also check relations")
- ;
-
- po::options_description hidden("Hidden options");
- hidden.add_options()
- ("input-filename", po::value<std::string>(), "Input file")
- ;
-
- po::options_description desc("Allowed options");
- desc.add(cmdline).add(hidden);
-
- po::positional_options_description positional;
- positional.add("input-filename", 1);
-
- po::store(po::command_line_parser(arguments).options(desc).positional(positional).run(), vm);
- po::notify(vm);
-
- if (vm.count("verbose")) {
- m_vout.verbose(true);
- }
- if (vm.count("show-ids")) {
- m_show_ids = true;
- }
+ po::options_description cmdline("Allowed options");
+ cmdline.add_options()
+ ("verbose,v", "Set verbose mode")
+ ("show-ids,i", "Show IDs of missing objects")
+ ("input-format,F", po::value<std::string>(), "Format of input files")
+ ("check-relations,r", "Also check relations")
+ ;
- if (vm.count("input-filename")) {
- m_input_filename = vm["input-filename"].as<std::string>();
- }
+ po::options_description hidden("Hidden options");
+ hidden.add_options()
+ ("input-filename", po::value<std::string>(), "Input file")
+ ;
- if (vm.count("input-format")) {
- m_input_format = vm["input-format"].as<std::string>();
- }
+ po::options_description desc("Allowed options");
+ desc.add(cmdline).add(hidden);
- if (vm.count("check-relations")) {
- m_check_relations = true;
- }
+ po::positional_options_description positional;
+ positional.add("input-filename", 1);
+
+ po::store(po::command_line_parser(arguments).options(desc).positional(positional).run(), vm);
+ po::notify(vm);
+
+ if (vm.count("verbose")) {
+ m_vout.verbose(true);
+ }
+
+ if (vm.count("show-ids")) {
+ m_show_ids = true;
+ }
+
+ if (vm.count("input-filename")) {
+ m_input_filename = vm["input-filename"].as<std::string>();
+ }
+
+ if (vm.count("input-format")) {
+ m_input_format = vm["input-format"].as<std::string>();
+ }
- } catch (boost::program_options::error& e) {
- std::cerr << "Error parsing command line: " << e.what() << std::endl;
- return false;
+ if (vm.count("check-relations")) {
+ m_check_relations = true;
}
m_vout << "Started osmium check-refs\n";
@@ -94,8 +89,7 @@ bool CommandCheckRefs::setup(const std::vector<std::string>& arguments) {
m_vout << " check relations: " << (m_check_relations ? "yes\n" : "no\n");
if ((m_input_filename == "-" || m_input_filename == "") && m_input_format.empty()) {
- std::cerr << "When reading from STDIN you need to use the --input-format,F option to declare the file format.\n";
- return false;
+ 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()) {
diff --git a/src/command_fileinfo.cpp b/src/command_fileinfo.cpp
index c7ca029..f27bfab 100644
--- a/src/command_fileinfo.cpp
+++ b/src/command_fileinfo.cpp
@@ -483,106 +483,96 @@ public:
bool CommandFileinfo::setup(const std::vector<std::string>& arguments) {
namespace po = boost::program_options;
po::variables_map vm;
- try {
- po::options_description cmdline("Allowed options");
- cmdline.add_options()
- ("extended,e", "Extended output")
- ("get,g", po::value<std::string>(), "Get value")
- ("show-variables,G", "Show variables for --get option")
- ("input-format,F", po::value<std::string>(), "Format of input file")
- ("json,j", "JSON output")
- ;
-
- po::options_description hidden("Hidden options");
- hidden.add_options()
- ("input-filename", po::value<std::string>(), "Input file")
- ;
-
- po::options_description desc("Allowed options");
- desc.add(cmdline).add(hidden);
-
- po::positional_options_description positional;
- positional.add("input-filename", 1);
-
- po::store(po::command_line_parser(arguments).options(desc).positional(positional).run(), vm);
- po::notify(vm);
-
- if (vm.count("extended")) {
- m_extended = true;
- }
-
- if (vm.count("json")) {
- m_json_output = true;
- }
-
- if (vm.count("input-format")) {
- m_input_format = vm["input-format"].as<std::string>();
- }
-
- if (vm.count("input-filename")) {
- m_input_filename = vm["input-filename"].as<std::string>();
- }
-
- std::vector<std::string> known_values = {
- "file.name",
- "file.format",
- "file.compression",
- "file.size",
- "header.with_history",
- "header.option.generator",
- "header.option.version",
- "header.option.pbf_dense_nodes",
- "header.option.osmosis_replication_timestamp",
- "header.option.osmosis_replication_sequence_number",
- "header.option.osmosis_replication_base_url",
- "data.bbox",
- "data.timestamp.first",
- "data.timestamp.last",
- "data.objects_ordered",
- "data.multiple_versions",
- "data.crc32",
- "data.count.nodes",
- "data.count.ways",
- "data.count.relations",
- "data.count.changesets",
- "data.maxid.nodes",
- "data.maxid.ways",
- "data.maxid.relations",
- "data.maxid.changesets"
- };
-
- if (vm.count("show-variables")) {
- std::copy(known_values.cbegin(), known_values.cend(), std::ostream_iterator<std::string>(std::cout, "\n"));
- m_do_not_run = true;
- return true;
- }
-
- if (vm.count("get")) {
- m_get_value = vm["get"].as<std::string>();
- const auto& f = std::find(known_values.cbegin(), known_values.cend(), m_get_value);
- if (f == known_values.cend()) {
- std::cerr << "Unknown value for --get/-g option '" << m_get_value << "'. Use --show-variables/-G to see list of known values.\n";
- return false;
- }
- if (m_get_value.substr(0, 5) == "data." && ! m_extended) {
- std::cerr << "You need to set --extended/-e for any 'data.*' variables to be available.\n";
- return false;
- }
- }
- if (vm.count("get") && vm.count("json")) {
- std::cerr << "You can not use --get/-g and --json/-j together.\n";
- return false;
- }
+ po::options_description cmdline("Allowed options");
+ cmdline.add_options()
+ ("extended,e", "Extended output")
+ ("get,g", po::value<std::string>(), "Get value")
+ ("show-variables,G", "Show variables for --get option")
+ ("input-format,F", po::value<std::string>(), "Format of input file")
+ ("json,j", "JSON output")
+ ;
+
+ po::options_description hidden("Hidden options");
+ hidden.add_options()
+ ("input-filename", po::value<std::string>(), "Input file")
+ ;
+
+ po::options_description desc("Allowed options");
+ desc.add(cmdline).add(hidden);
+
+ po::positional_options_description positional;
+ positional.add("input-filename", 1);
+
+ po::store(po::command_line_parser(arguments).options(desc).positional(positional).run(), vm);
+ po::notify(vm);
+
+ if (vm.count("extended")) {
+ m_extended = true;
+ }
+
+ if (vm.count("json")) {
+ m_json_output = true;
+ }
- } catch (boost::program_options::error& e) {
- std::cerr << "Error parsing command line: " << e.what() << std::endl;
+ if (vm.count("input-format")) {
+ m_input_format = vm["input-format"].as<std::string>();
+ }
+
+ if (vm.count("input-filename")) {
+ m_input_filename = vm["input-filename"].as<std::string>();
+ }
+
+ std::vector<std::string> known_values = {
+ "file.name",
+ "file.format",
+ "file.compression",
+ "file.size",
+ "header.with_history",
+ "header.option.generator",
+ "header.option.version",
+ "header.option.pbf_dense_nodes",
+ "header.option.osmosis_replication_timestamp",
+ "header.option.osmosis_replication_sequence_number",
+ "header.option.osmosis_replication_base_url",
+ "data.bbox",
+ "data.timestamp.first",
+ "data.timestamp.last",
+ "data.objects_ordered",
+ "data.multiple_versions",
+ "data.crc32",
+ "data.count.nodes",
+ "data.count.ways",
+ "data.count.relations",
+ "data.count.changesets",
+ "data.maxid.nodes",
+ "data.maxid.ways",
+ "data.maxid.relations",
+ "data.maxid.changesets"
+ };
+
+ if (vm.count("show-variables")) {
+ std::copy(known_values.cbegin(), known_values.cend(), std::ostream_iterator<std::string>(std::cout, "\n"));
return false;
}
+ if (vm.count("get")) {
+ m_get_value = vm["get"].as<std::string>();
+ const auto& f = std::find(known_values.cbegin(), known_values.cend(), m_get_value);
+ if (f == known_values.cend()) {
+ throw argument_error(std::string("Unknown value for --get/-g option '") + m_get_value + "'. Use --show-variables/-G to see list of known values.");
+ }
+ if (m_get_value.substr(0, 5) == "data." && ! m_extended) {
+ throw argument_error("You need to set --extended/-e for any 'data.*' variables to be available.");
+ }
+ }
+
+ if (vm.count("get") && vm.count("json")) {
+ throw argument_error("You can not use --get/-g and --json/-j together.");
+ }
+
if ((m_input_filename == "-" || m_input_filename == "") && m_input_format.empty()) {
- std::cerr << "When reading from STDIN you need to use the --input-format,F option to declare the file format.\n";
- return false;
+ throw argument_error("When reading from STDIN you need to use the --input-format,F option to declare the file format.");
}
m_input_file = osmium::io::File(m_input_filename, m_input_format);
@@ -591,10 +581,6 @@ bool CommandFileinfo::setup(const std::vector<std::string>& arguments) {
}
bool CommandFileinfo::run() {
- if (m_do_not_run) {
- return true;
- }
-
std::unique_ptr<Output> output;
if (m_json_output) {
output.reset(new JSONOutput());
diff --git a/src/command_fileinfo.hpp b/src/command_fileinfo.hpp
index 381087b..26bfcef 100644
--- a/src/command_fileinfo.hpp
+++ b/src/command_fileinfo.hpp
@@ -34,7 +34,6 @@ class CommandFileinfo : public Command {
std::string m_input_filename = "-"; // default: stdin
bool m_extended = false;
bool m_json_output = false;
- bool m_do_not_run = false;
std::string m_input_format;
osmium::io::File m_input_file;
std::string m_get_value;
diff --git a/src/command_merge_changes.cpp b/src/command_merge_changes.cpp
index 5e83696..b9d1417 100644
--- a/src/command_merge_changes.cpp
+++ b/src/command_merge_changes.cpp
@@ -35,72 +35,66 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
bool CommandMergeChanges::setup(const std::vector<std::string>& arguments) {
namespace po = boost::program_options;
po::variables_map vm;
- try {
- po::options_description cmdline("Allowed options");
- cmdline.add_options()
- ("verbose,v", "Set verbose mode")
- ("output,o", po::value<std::string>(), "Output file")
- ("output-format,f", po::value<std::string>(), "Format of output file")
- ("input-format,F", po::value<std::string>(), "Format of input files")
- ("simplify,s", "Simplify change")
- ("generator", po::value<std::string>(), "Generator setting for file header")
- ("overwrite,O", "Allow existing output file to be overwritten")
- ;
-
- po::options_description hidden("Hidden options");
- hidden.add_options()
- ("input-filenames", po::value<std::vector<std::string>>(), "Input files")
- ;
-
- po::options_description desc("Allowed options");
- desc.add(cmdline).add(hidden);
-
- po::positional_options_description positional;
- positional.add("input-filenames", -1);
-
- po::store(po::command_line_parser(arguments).options(desc).positional(positional).run(), vm);
- po::notify(vm);
-
- if (vm.count("input-filenames")) {
- m_input_filenames = vm["input-filenames"].as<std::vector<std::string>>();
- }
- if (vm.count("output")) {
- m_output_filename = vm["output"].as<std::string>();
- }
+ po::options_description cmdline("Allowed options");
+ cmdline.add_options()
+ ("verbose,v", "Set verbose mode")
+ ("output,o", po::value<std::string>(), "Output file")
+ ("output-format,f", po::value<std::string>(), "Format of output file")
+ ("input-format,F", po::value<std::string>(), "Format of input files")
+ ("simplify,s", "Simplify change")
+ ("generator", po::value<std::string>(), "Generator setting for file header")
+ ("overwrite,O", "Allow existing output file to be overwritten")
+ ;
+
+ po::options_description hidden("Hidden options");
+ hidden.add_options()
+ ("input-filenames", po::value<std::vector<std::string>>(), "Input files")
+ ;
+
+ po::options_description desc("Allowed options");
+ desc.add(cmdline).add(hidden);
+
+ po::positional_options_description positional;
+ positional.add("input-filenames", -1);
+
+ po::store(po::command_line_parser(arguments).options(desc).positional(positional).run(), vm);
+ po::notify(vm);
+
+ if (vm.count("input-filenames")) {
+ m_input_filenames = vm["input-filenames"].as<std::vector<std::string>>();
+ }
- if (vm.count("input-format")) {
- m_input_format = vm["input-format"].as<std::string>();
- }
+ if (vm.count("output")) {
+ m_output_filename = vm["output"].as<std::string>();
+ }
- if (vm.count("output-format")) {
- m_output_format = vm["output-format"].as<std::string>();
- }
+ if (vm.count("input-format")) {
+ m_input_format = vm["input-format"].as<std::string>();
+ }
- if (vm.count("overwrite")) {
- m_output_overwrite = osmium::io::overwrite::allow;
- }
+ if (vm.count("output-format")) {
+ m_output_format = vm["output-format"].as<std::string>();
+ }
- if (vm.count("simplify")) {
- m_simplify_change = true;
- }
+ if (vm.count("overwrite")) {
+ m_output_overwrite = osmium::io::overwrite::allow;
+ }
- if (vm.count("generator")) {
- m_generator = vm["generator"].as<std::string>();
- }
+ if (vm.count("simplify")) {
+ m_simplify_change = true;
+ }
- if (vm.count("verbose")) {
- m_vout.verbose(true);
- }
+ if (vm.count("generator")) {
+ m_generator = vm["generator"].as<std::string>();
+ }
- } catch (boost::program_options::error& e) {
- std::cerr << "Error parsing command line: " << e.what() << std::endl;
- return false;
+ if (vm.count("verbose")) {
+ m_vout.verbose(true);
}
if ((m_output_filename == "-" || m_output_filename == "") && m_output_format.empty()) {
- std::cerr << "When writing to STDOUT you need to use the --output-format,f option to declare the file format.\n";
- return false;
+ throw argument_error("When writing to STDOUT you need to use the --output-format,f option to declare the file format.");
}
if (m_input_format.empty()) {
@@ -111,8 +105,7 @@ bool CommandMergeChanges::setup(const std::vector<std::string>& arguments) {
}
}
if (uses_stdin) {
- std::cerr << "When reading from STDIN you need to use the --input-format,F option to declare the file format.\n";
- return false;
+ throw argument_error("When reading from STDIN you need to use the --input-format,F option to declare the file format.");
}
}
diff --git a/src/command_time_filter.cpp b/src/command_time_filter.cpp
index c7fa105..cb7324c 100644
--- a/src/command_time_filter.cpp
+++ b/src/command_time_filter.cpp
@@ -33,92 +33,84 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
bool CommandTimeFilter::setup(const std::vector<std::string>& arguments) {
namespace po = boost::program_options;
po::variables_map vm;
- try {
- po::options_description cmdline("Allowed options");
- cmdline.add_options()
- ("verbose,v", "Set verbose mode")
- ("output,o", po::value<std::string>(), "Output file")
- ("output-format,f", po::value<std::string>(), "Format of output file")
- ("input-format,F", po::value<std::string>(), "Format of input file")
- ("generator", po::value<std::string>(), "Generator setting for file header")
- ("overwrite,O", "Allow existing output file to be overwritten")
- ;
-
- po::options_description hidden("Hidden options");
- hidden.add_options()
- ("input-filename", po::value<std::string>(), "OSM input file")
- ("time-from", po::value<std::string>(), "Start of time range")
- ("time-to", po::value<std::string>(), "End of time range")
- ;
-
- po::options_description desc("Allowed options");
- desc.add(cmdline).add(hidden);
-
- po::positional_options_description positional;
- positional.add("input-filename", 1);
- positional.add("time-from", 1);
- positional.add("time-to", 1);
-
- po::store(po::command_line_parser(arguments).options(desc).positional(positional).run(), vm);
- po::notify(vm);
-
- if (vm.count("input-filename")) {
- m_input_filename = vm["input-filename"].as<std::string>();
- }
- if (vm.count("output")) {
- m_output_filename = vm["output"].as<std::string>();
- }
+ po::options_description cmdline("Allowed options");
+ cmdline.add_options()
+ ("verbose,v", "Set verbose mode")
+ ("output,o", po::value<std::string>(), "Output file")
+ ("output-format,f", po::value<std::string>(), "Format of output file")
+ ("input-format,F", po::value<std::string>(), "Format of input file")
+ ("generator", po::value<std::string>(), "Generator setting for file header")
+ ("overwrite,O", "Allow existing output file to be overwritten")
+ ;
+
+ po::options_description hidden("Hidden options");
+ hidden.add_options()
+ ("input-filename", po::value<std::string>(), "OSM input file")
+ ("time-from", po::value<std::string>(), "Start of time range")
+ ("time-to", po::value<std::string>(), "End of time range")
+ ;
+
+ po::options_description desc("Allowed options");
+ desc.add(cmdline).add(hidden);
+
+ po::positional_options_description positional;
+ positional.add("input-filename", 1);
+ positional.add("time-from", 1);
+ positional.add("time-to", 1);
+
+ po::store(po::command_line_parser(arguments).options(desc).positional(positional).run(), vm);
+ po::notify(vm);
+
+ if (vm.count("input-filename")) {
+ m_input_filename = vm["input-filename"].as<std::string>();
+ }
- if (vm.count("input-format")) {
- m_input_format = vm["input-format"].as<std::string>();
- }
+ if (vm.count("output")) {
+ m_output_filename = vm["output"].as<std::string>();
+ }
- if (vm.count("output-format")) {
- m_output_format = vm["output-format"].as<std::string>();
- }
+ if (vm.count("input-format")) {
+ m_input_format = vm["input-format"].as<std::string>();
+ }
- if (vm.count("overwrite")) {
- m_output_overwrite = osmium::io::overwrite::allow;
- }
+ if (vm.count("output-format")) {
+ m_output_format = vm["output-format"].as<std::string>();
+ }
- if (vm.count("verbose")) {
- m_vout.verbose(true);
- }
+ if (vm.count("overwrite")) {
+ m_output_overwrite = osmium::io::overwrite::allow;
+ }
- if (vm.count("generator")) {
- m_generator = vm["generator"].as<std::string>();
- }
+ if (vm.count("verbose")) {
+ m_vout.verbose(true);
+ }
- m_from = osmium::Timestamp(time(0));
- m_to = m_from;
+ if (vm.count("generator")) {
+ m_generator = vm["generator"].as<std::string>();
+ }
- if (vm.count("time-from")) {
- m_from = osmium::Timestamp(vm["time-from"].as<std::string>().c_str());
- m_to = m_from;
- }
+ m_from = osmium::Timestamp(time(0));
+ m_to = m_from;
- if (vm.count("time-to")) {
- m_to = osmium::Timestamp(vm["time-to"].as<std::string>().c_str());
- if (m_to < m_from) {
- std::cerr << "Second timestamp is before first one.\n";
- return false;
- }
- }
+ if (vm.count("time-from")) {
+ m_from = osmium::Timestamp(vm["time-from"].as<std::string>().c_str());
+ m_to = m_from;
+ }
- } catch (boost::program_options::error& e) {
- std::cerr << "Error parsing command line: " << e.what() << std::endl;
- return false;
+ if (vm.count("time-to")) {
+ m_to = osmium::Timestamp(vm["time-to"].as<std::string>().c_str());
+ if (m_to < m_from) {
+ throw argument_error("Second timestamp is before first one.");
+ }
}
if ((m_output_filename == "-" || m_output_filename == "") && m_output_format.empty()) {
- std::cerr << "When writing to STDOUT you need to use the --output-format,f option to declare the file format.\n";
- return false;
+ throw argument_error("When writing to STDOUT you need to use the --output-format,f option to declare the file format.");
}
if ((m_input_filename == "-" || m_input_filename == "") && m_input_format.empty()) {
- std::cerr << "When reading from STDIN you need to use the --input-format,F option to declare the file format.\n";
- return false;
+ throw argument_error("When reading from STDIN you need to use the --input-format,F option to declare the file format.");
}
m_input_file = osmium::io::File(m_input_filename, m_input_format);
diff --git a/src/main.cpp b/src/main.cpp
index 0482645..4795e5c 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -24,6 +24,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <string>
#include <vector>
+#include <boost/program_options.hpp>
+
#include <osmpbf/osmpbf.h>
#include "osmc.hpp"
@@ -92,7 +94,15 @@ int main(int argc, char *argv[]) {
return return_code::fatal;
}
- if (!cmd->setup(arguments)) {
+ try {
+ if (!cmd->setup(arguments)) {
+ return return_code::okay;
+ }
+ } catch (boost::program_options::error& e) {
+ std::cerr << "Error parsing command line: " << e.what() << std::endl;
+ return return_code::fatal;
+ } catch (std::exception& e) {
+ std::cerr << e.what() << std::endl;
return return_code::fatal;
}
diff --git a/src/osmc.hpp b/src/osmc.hpp
index 0d26b6a..014731e 100644
--- a/src/osmc.hpp
+++ b/src/osmc.hpp
@@ -53,8 +53,10 @@ public:
// This function parses the command line arguments for a
// command.
- // It returns true if the parsing was successful.
- // It returns false if the arguments could not be parsed.
+ // It returns true if the parsing was successful and the run()
+ // function should be called. It returns false if the work is
+ // done and run() should not be called.
+ // It throws if there was a problem with the arguments.
//
// This function should not attempt to open any files or
// do any other actual work. That will happen in the run()
@@ -141,4 +143,19 @@ public:
}; // class CommandFactory
+/**
+ * Thrown when there is a problem with the command line arguments.
+ */
+struct argument_error : std::runtime_error {
+
+ argument_error(const char* message) :
+ std::runtime_error(message) {
+ }
+
+ argument_error(const std::string& message) :
+ std::runtime_error(message) {
+ }
+
+};
+
#endif // OSMC_HPP
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-grass/osmium-tool.git
More information about the Pkg-grass-devel
mailing list