[osmium-tool] 26/44: Refactoring of command line parsing code for input/output files.

Sebastiaan Couwenberg sebastic at moszumanska.debian.org
Tue Jul 21 20:15:56 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 a4a8f3d5aa04d9622107a8425d8ed3c4c57ddd74
Author: Jochen Topf <jochen at topf.org>
Date:   Sun Jun 28 11:13:54 2015 +0200

    Refactoring of command line parsing code for input/output files.
    
    There was a lot of duplicated code here (with some slight modifications here or
    there). This is now much cleaner.
---
 src/command_apply_changes.cpp |  33 +----------
 src/command_apply_changes.hpp |  13 +---
 src/command_cat.cpp           |  46 +-------------
 src/command_cat.hpp           |  11 +---
 src/command_check_refs.cpp    |  18 +-----
 src/command_check_refs.hpp    |   8 +--
 src/command_fileinfo.cpp      |  14 +----
 src/command_fileinfo.hpp      |   5 +-
 src/command_merge_changes.cpp |  43 ++------------
 src/command_merge_changes.hpp |  14 +----
 src/command_time_filter.cpp   |  32 +---------
 src/command_time_filter.hpp   |  15 +----
 src/main.cpp                  |   2 -
 src/osmc.hpp                  | 135 +++++++++++++++++++++++++++++++++++++-----
 14 files changed, 139 insertions(+), 250 deletions(-)

diff --git a/src/command_apply_changes.cpp b/src/command_apply_changes.cpp
index 036075e..f8f48b4 100644
--- a/src/command_apply_changes.cpp
+++ b/src/command_apply_changes.cpp
@@ -63,30 +63,10 @@ bool CommandApplyChanges::setup(const std::vector<std::string>& arguments) {
     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>>();
     }
 
-    if (vm.count("output")) {
-        m_output_filename = vm["output"].as<std::string>();
-    }
-
-    if (vm.count("input-format")) {
-        m_input_format = vm["input-format"].as<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("simplify")) {
         m_simplify_change = true;
     }
@@ -103,15 +83,8 @@ bool CommandApplyChanges::setup(const std::vector<std::string>& arguments) {
         m_vout.verbose(true);
     }
 
-    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 to declare the file format.");
-    }
-
-    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.");
-    }
-
-    m_output_file = osmium::io::File(m_output_filename, m_output_format);
+    setup_input_file(vm);
+    setup_output_file(vm);
 
     m_vout << "Started osmium apply-changes\n";
 
@@ -178,7 +151,7 @@ bool CommandApplyChanges::run() {
     }
 
     m_vout << "Opening input file...\n";
-    osmium::io::Reader reader(m_input_filename, osmium::osm_entity_bits::object);
+    osmium::io::Reader reader(m_input_file, osmium::osm_entity_bits::object);
 
     osmium::io::Header header = reader.header();
     header.set("generator", m_generator);
diff --git a/src/command_apply_changes.hpp b/src/command_apply_changes.hpp
index 770bb71..1db7eae 100644
--- a/src/command_apply_changes.hpp
+++ b/src/command_apply_changes.hpp
@@ -26,26 +26,15 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <string>
 #include <vector>
 
-#include <osmium/io/file.hpp>
-#include <osmium/io/overwrite.hpp>
-
 #include "osmc.hpp"
 
-class CommandApplyChanges : public Command {
+class CommandApplyChanges : public Command, with_single_osm_input, with_osm_output {
 
-    std::string m_input_filename = "-"; // default: stdin
-    std::string m_output_filename = "-"; // default: stdout
     std::vector<std::string> m_change_filenames;
 
-    std::string m_input_format;
-    std::string m_output_format;
-
-    osmium::io::overwrite m_output_overwrite = osmium::io::overwrite::no;
     bool m_simplify_change = false;
     bool m_remove_deleted = false;
 
-    osmium::io::File m_output_file;
-
 public:
 
     CommandApplyChanges() = default;
diff --git a/src/command_cat.cpp b/src/command_cat.cpp
index 3605f49..426d2b1 100644
--- a/src/command_cat.cpp
+++ b/src/command_cat.cpp
@@ -68,32 +68,10 @@ bool CommandCat::setup(const std::vector<std::string>& arguments) {
         m_generator = vm["generator"].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("output")) {
-        m_output_filename = vm["output"].as<std::string>();
-    }
-
-    if (vm.count("input-format")) {
-        m_input_format = vm["input-format"].as<std::string>();
-    }
-
-    if (vm.count("output-format")) {
-        m_output_format = vm["output-format"].as<std::string>();
-    }
-
     if (vm.count("output-header")) {
         m_output_headers = vm["output-header"].as<std::vector<std::string>>();
     }
 
-    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>>()) {
@@ -141,28 +119,8 @@ bool CommandCat::setup(const std::vector<std::string>& arguments) {
     }
     m_vout << "\n";
 
-    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 to declare the file format.");
-    }
-
-    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.");
-        }
-    }
-
-    m_output_file = osmium::io::File(m_output_filename, m_output_format);
-
-    for (const std::string& input_filename : m_input_filenames) {
-        osmium::io::File input_file(input_filename, m_input_format);
-        m_input_files.push_back(input_file);
-    }
+    setup_input_files(vm);
+    setup_output_file(vm);
 
     return true;
 }
diff --git a/src/command_cat.hpp b/src/command_cat.hpp
index b09f0b2..9673c56 100644
--- a/src/command_cat.hpp
+++ b/src/command_cat.hpp
@@ -26,22 +26,13 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <string>
 #include <vector>
 
-#include <osmium/io/file.hpp>
-#include <osmium/io/overwrite.hpp>
 #include <osmium/osm/entity_bits.hpp>
 
 #include "osmc.hpp"
 
-class CommandCat : public Command {
+class CommandCat : public Command, with_multiple_osm_inputs, with_osm_output {
 
-    std::vector<std::string> m_input_filenames;
-    std::string m_output_filename = "-"; // default: stdout
-    std::string m_input_format;
-    std::string m_output_format;
     std::vector<std::string> m_output_headers;
-    osmium::io::File m_output_file;
-    std::vector<osmium::io::File> m_input_files;
-    osmium::io::overwrite m_output_overwrite = osmium::io::overwrite::no;
     osmium::osm_entity_bits::type m_osm_entity_bits = osmium::osm_entity_bits::all;
 
 public:
diff --git a/src/command_check_refs.cpp b/src/command_check_refs.cpp
index 579279c..cacc32b 100644
--- a/src/command_check_refs.cpp
+++ b/src/command_check_refs.cpp
@@ -68,14 +68,6 @@ bool CommandCheckRefs::setup(const std::vector<std::string>& arguments) {
         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>();
-    }
-
     if (vm.count("check-relations")) {
         m_check_relations = true;
     }
@@ -88,15 +80,7 @@ bool CommandCheckRefs::setup(const std::vector<std::string>& arguments) {
     m_vout << "  show ids: " << (m_show_ids ? "yes\n" : "no\n");
     m_vout << "  check relations: " << (m_check_relations ? "yes\n" : "no\n");
 
-    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()) {
-        m_input_file = osmium::io::File(m_input_filename);
-    } else {
-        m_input_file = osmium::io::File(m_input_filename, m_input_format);
-    }
+    setup_input_file(vm);
 
     return true;
 }
diff --git a/src/command_check_refs.hpp b/src/command_check_refs.hpp
index ccfddc9..d8bfbea 100644
--- a/src/command_check_refs.hpp
+++ b/src/command_check_refs.hpp
@@ -25,15 +25,9 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 #include <string>
 
-#include <osmium/io/file.hpp>
-
 #include "osmc.hpp"
 
-class CommandCheckRefs : public Command {
-
-    std::string m_input_filename = "-"; // default: stdin
-    std::string m_input_format;
-    osmium::io::File m_input_file;
+class CommandCheckRefs : public Command, with_single_osm_input {
 
     bool m_show_ids = false;
     bool m_check_relations = false;
diff --git a/src/command_fileinfo.cpp b/src/command_fileinfo.cpp
index f27bfab..2f8ba06 100644
--- a/src/command_fileinfo.cpp
+++ b/src/command_fileinfo.cpp
@@ -515,14 +515,6 @@ bool CommandFileinfo::setup(const std::vector<std::string>& arguments) {
         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",
@@ -571,11 +563,7 @@ bool CommandFileinfo::setup(const std::vector<std::string>& arguments) {
         throw argument_error("You can not use --get/-g and --json/-j together.");
     }
 
-    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.");
-    }
-
-    m_input_file = osmium::io::File(m_input_filename, m_input_format);
+    setup_input_file(vm);
 
     return true;
 }
diff --git a/src/command_fileinfo.hpp b/src/command_fileinfo.hpp
index 26bfcef..497954f 100644
--- a/src/command_fileinfo.hpp
+++ b/src/command_fileinfo.hpp
@@ -29,13 +29,10 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 #include "osmc.hpp"
 
-class CommandFileinfo : public Command {
+class CommandFileinfo : public Command, with_single_osm_input {
 
-    std::string m_input_filename = "-"; // default: stdin
     bool m_extended = false;
     bool m_json_output = false;
-    std::string m_input_format;
-    osmium::io::File m_input_file;
     std::string m_get_value;
 
 public:
diff --git a/src/command_merge_changes.cpp b/src/command_merge_changes.cpp
index b9d1417..331b08d 100644
--- a/src/command_merge_changes.cpp
+++ b/src/command_merge_changes.cpp
@@ -61,26 +61,6 @@ bool CommandMergeChanges::setup(const std::vector<std::string>& arguments) {
     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>();
-    }
-
-    if (vm.count("input-format")) {
-        m_input_format = vm["input-format"].as<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("simplify")) {
         m_simplify_change = true;
     }
@@ -93,23 +73,8 @@ bool CommandMergeChanges::setup(const std::vector<std::string>& arguments) {
         m_vout.verbose(true);
     }
 
-    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 to declare the file format.");
-    }
-
-    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.");
-        }
-    }
-
-    m_output_file = osmium::io::File(m_output_filename, m_output_format);
+    setup_input_files(vm);
+    setup_output_file(vm);
 
     m_vout << "Started osmium merge-changes\n";
 
@@ -135,8 +100,8 @@ bool CommandMergeChanges::run() {
     // read all input files, keep the buffers around and add pointer
     // to each object to objects collection.
     m_vout << "Reading change file contents...\n";
-    for (const std::string& change_file_name : m_input_filenames) {
-        osmium::io::Reader reader(change_file_name, osmium::osm_entity_bits::object);
+    for (osmium::io::File& change_file : m_input_files) {
+        osmium::io::Reader reader(change_file, osmium::osm_entity_bits::object);
         while (osmium::memory::Buffer buffer = reader.read()) {
             osmium::apply(buffer, objects);
             changes.push_back(std::move(buffer));
diff --git a/src/command_merge_changes.hpp b/src/command_merge_changes.hpp
index 1708b66..42c2312 100644
--- a/src/command_merge_changes.hpp
+++ b/src/command_merge_changes.hpp
@@ -26,24 +26,12 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <string>
 #include <vector>
 
-#include <osmium/io/file.hpp>
-#include <osmium/io/overwrite.hpp>
-
 #include "osmc.hpp"
 
-class CommandMergeChanges : public Command {
-
-    std::vector<std::string> m_input_filenames;
-    std::string m_output_filename = "-"; // default: stdout
+class CommandMergeChanges : public Command, with_multiple_osm_inputs, with_osm_output {
 
-    std::string m_input_format;
-    std::string m_output_format;
-
-    osmium::io::overwrite m_output_overwrite = osmium::io::overwrite::no;
     bool m_simplify_change = false;
 
-    osmium::io::File m_output_file;
-
 public:
 
     CommandMergeChanges() = default;
diff --git a/src/command_time_filter.cpp b/src/command_time_filter.cpp
index cb7324c..59ec4cb 100644
--- a/src/command_time_filter.cpp
+++ b/src/command_time_filter.cpp
@@ -62,26 +62,6 @@ bool CommandTimeFilter::setup(const std::vector<std::string>& arguments) {
     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("input-format")) {
-        m_input_format = vm["input-format"].as<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("verbose")) {
         m_vout.verbose(true);
     }
@@ -105,16 +85,8 @@ bool CommandTimeFilter::setup(const std::vector<std::string>& arguments) {
         }
     }
 
-    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 to declare the file format.");
-    }
-
-    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.");
-    }
-
-    m_input_file = osmium::io::File(m_input_filename, m_input_format);
-    m_output_file = osmium::io::File(m_output_filename, m_output_format);
+    setup_input_file(vm);
+    setup_output_file(vm);
 
     if (m_from == m_to) { // point in time
         if (m_output_file.has_multiple_object_versions()) {
diff --git a/src/command_time_filter.hpp b/src/command_time_filter.hpp
index e4e0b71..1ec4382 100644
--- a/src/command_time_filter.hpp
+++ b/src/command_time_filter.hpp
@@ -26,24 +26,11 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <string>
 #include <vector>
 
-#include <osmium/io/file.hpp>
-#include <osmium/io/overwrite.hpp>
 #include <osmium/osm/timestamp.hpp>
 
 #include "osmc.hpp"
 
-class CommandTimeFilter : public Command {
-
-    std::string m_input_filename = "-"; // default: stdin
-    std::string m_output_filename = "-"; // default: stdout
-
-    std::string m_input_format;
-    std::string m_output_format;
-
-    osmium::io::overwrite m_output_overwrite = osmium::io::overwrite::no;
-
-    osmium::io::File m_input_file;
-    osmium::io::File m_output_file;
+class CommandTimeFilter : public Command, with_single_osm_input, with_osm_output {
 
     osmium::Timestamp m_from;
     osmium::Timestamp m_to;
diff --git a/src/main.cpp b/src/main.cpp
index 4795e5c..a39295a 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -24,8 +24,6 @@ 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"
diff --git a/src/osmc.hpp b/src/osmc.hpp
index 014731e..0b5ec2a 100644
--- a/src/osmc.hpp
+++ b/src/osmc.hpp
@@ -29,9 +29,28 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <string>
 #include <vector>
 
+#include <boost/program_options.hpp>
+
+#include <osmium/io/file.hpp>
+#include <osmium/io/overwrite.hpp>
 #include <osmium/util/verbose_output.hpp>
 
 /**
+ *  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) {
+    }
+
+};
+
+/**
  * Virtual base class for commands that can be called from the command line.
  */
 class Command {
@@ -72,6 +91,107 @@ public:
 
 }; // class Command
 
+class with_single_osm_input {
+
+protected:
+
+    std::string m_input_filename = "-"; // default: stdin
+    std::string m_input_format;
+    osmium::io::File m_input_file;
+
+public:
+
+    void setup_input_file(const boost::program_options::variables_map& 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 ((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.");
+        }
+
+        m_input_file = osmium::io::File(m_input_filename, m_input_format);
+    }
+
+}; // class with_single_osm_input
+
+class with_multiple_osm_inputs {
+
+protected:
+
+    std::vector<std::string> m_input_filenames;
+    std::string m_input_format;
+    std::vector<osmium::io::File> m_input_files;
+
+public:
+
+    void 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 {
+            m_input_filenames.push_back("-"); // default is stdin
+        }
+
+        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.");
+            }
+        }
+
+        for (const std::string& input_filename : m_input_filenames) {
+            osmium::io::File input_file(input_filename, m_input_format);
+            m_input_files.push_back(input_file);
+        }
+    }
+
+}; // class with_multiple_osm_inputs
+
+class with_osm_output {
+
+protected:
+
+    std::string m_output_filename = "-"; // default: stdout
+    std::string m_output_format;
+    osmium::io::File m_output_file;
+    osmium::io::overwrite m_output_overwrite = osmium::io::overwrite::no;
+
+public:
+
+    void setup_output_file(const boost::program_options::variables_map& vm) {
+        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("overwrite")) {
+            m_output_overwrite = osmium::io::overwrite::allow;
+        }
+
+        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 to declare the file format.");
+        }
+
+        m_output_file = osmium::io::File(m_output_filename, m_output_format);
+    }
+
+}; // class with_osm_output
 
 /**
  * All commands than can be called from the command line are registered
@@ -143,19 +263,4 @@ 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