[Liblas-commits] libpc: add support for positional opts

liblas-commits at liblas.org liblas-commits at liblas.org
Thu Mar 10 17:56:35 EST 2011


details:   http://hg.liblas.orglibpc/rev/56622f0300f3
changeset: 200:56622f0300f3
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Thu Mar 10 14:55:59 2011 -0800
description:
add support for positional opts
Subject: libpc: minimally turn on pcinfo

details:   http://hg.liblas.orglibpc/rev/25fdd984e99d
changeset: 201:25fdd984e99d
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Thu Mar 10 14:56:29 2011 -0800
description:
minimally turn on pcinfo

diffstat:

 apps/Application.cpp |  20 ++++++++++-------
 apps/Application.hpp |   2 +
 apps/pcinfo.cpp      |  59 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 73 insertions(+), 8 deletions(-)

diffs (158 lines):

diff -r 29fa92ca867d -r 25fdd984e99d apps/Application.cpp
--- a/apps/Application.cpp	Thu Mar 10 14:24:29 2011 -0800
+++ b/apps/Application.cpp	Thu Mar 10 14:56:29 2011 -0800
@@ -144,10 +144,17 @@
 
 void Application::addOptionSet(po::options_description* options)
 {
+    if (!options) return;
     m_options.push_back(options);
 }
 
 
+void Application::addPositionalOption(const char* name, int max_count)
+{
+    m_positionalOptions.add(name, max_count);
+}
+
+
 void Application::outputHelp()
 {
     outputVersion();
@@ -200,19 +207,16 @@
 {
     po::options_description options;
 
-    std::vector<po::options_description*>::iterator iter;
-    for (iter = m_options.begin(); iter != m_options.end(); ++iter)
+    std::vector<po::options_description*>::iterator iter1;
+    for (iter1 = m_options.begin(); iter1 != m_options.end(); ++iter1)
     {
-        po::options_description* sub_options = *iter;
+        po::options_description* sub_options = *iter1;
         options.add(*sub_options);
     }
 
-    //po::positional_options_description p;
-    //p.add("input", 1);
-    //p.add("output", 1);
-
     po::store(po::command_line_parser(m_argc, m_argv).
-        options(options)/*.positional(p)*/.run(), m_variablesMap);
+        options(options).positional(m_positionalOptions).run(), 
+        m_variablesMap);
 
     po::notify(m_variablesMap);
 
diff -r 29fa92ca867d -r 25fdd984e99d apps/Application.hpp
--- a/apps/Application.hpp	Thu Mar 10 14:24:29 2011 -0800
+++ b/apps/Application.hpp	Thu Mar 10 14:56:29 2011 -0800
@@ -75,6 +75,7 @@
 protected:
     Application(int argc, char* argv[], const std::string& appName);
     void addOptionSet(boost::program_options::options_description* options);
+    void addPositionalOption(const char* name, int max_count);
     bool isVerbose() const;
     bool hasOption(const std::string& name);
     void usageError(const std::string&);
@@ -92,6 +93,7 @@
     const std::string m_appName;
 
     std::vector<boost::program_options::options_description*> m_options;
+    boost::program_options::positional_options_description m_positionalOptions;
     boost::program_options::variables_map m_variablesMap;
 
     Application& operator=(const Application&); // not implemented
diff -r 29fa92ca867d -r 25fdd984e99d apps/pcinfo.cpp
--- a/apps/pcinfo.cpp	Thu Mar 10 14:24:29 2011 -0800
+++ b/apps/pcinfo.cpp	Thu Mar 10 14:56:29 2011 -0800
@@ -10,10 +10,15 @@
  **************************************************************************/
 
 
+#include "libpc/LasReader.hpp"
+#include "libpc/../../src/drivers/liblas/reader.hpp"
+
 #include <iostream>
 
 #include "Application.hpp"
 
+using namespace libpc;
+namespace po = boost::program_options;
 
 class Application_pcinfo : public Application
 {
@@ -22,6 +27,9 @@
     int execute();
 private:
     void addOptions();
+    bool validateOptions();
+
+    std::string m_inputFile;
 };
 
 
@@ -31,12 +39,63 @@
 }
 
 
+bool Application_pcinfo::validateOptions()
+{
+    if (!hasOption("input"))
+    {
+        usageError("input file name required");
+        return false;
+    }
+
+    return true;
+}
+
+
 void Application_pcinfo::addOptions()
 {
+    po::options_description* file_options = new po::options_description("file options");
+
+    file_options->add_options()
+        ("input,i", po::value<std::string>(&m_inputFile), "input file name")
+        ("native", "use native LAS classes (not liblas)")
+        ;
+
+    addOptionSet(file_options);
+
+    addPositionalOption("input", 1);
+
+    return;
 }
 
+
 int Application_pcinfo::execute()
 {
+    if (!Utils::fileExists(m_inputFile))
+    {
+        runtimeError("file not found: " + m_inputFile);
+        return 1;
+    }
+
+    std::istream* ifs = Utils::openFile(m_inputFile);
+
+    libpc::Producer* reader = NULL;
+    if (hasOption("native"))
+    {
+        reader = new LasReader(*ifs);
+    }
+    else
+    {
+        reader = new LiblasReader(*ifs);
+    }
+
+    boost::uint64_t numPoints = reader->getNumPoints();
+
+    delete reader;
+
+    Utils::closeFile(ifs);
+
+    std::cout << numPoints << " points\n";
+
     return 0;
 }
 


More information about the Liblas-commits mailing list