[cig-commits] r12949 - cs/cigma/trunk/src
luis at geodynamics.org
luis at geodynamics.org
Wed Sep 24 04:10:37 PDT 2008
Author: luis
Date: 2008-09-24 04:10:37 -0700 (Wed, 24 Sep 2008)
New Revision: 12949
Added:
cs/cigma/trunk/src/cli_base_cmd.cpp
cs/cigma/trunk/src/cli_base_cmd.h
cs/cigma/trunk/src/cli_command.cpp
cs/cigma/trunk/src/cli_command.h
cs/cigma/trunk/src/cli_list_cmd.cpp
cs/cigma/trunk/src/cli_list_cmd.h
cs/cigma/trunk/src/cli_skel_cmd.cpp
cs/cigma/trunk/src/cli_skel_cmd.h
Log:
More CLI-related code
Added: cs/cigma/trunk/src/cli_base_cmd.cpp
===================================================================
--- cs/cigma/trunk/src/cli_base_cmd.cpp (rev 0)
+++ cs/cigma/trunk/src/cli_base_cmd.cpp 2008-09-24 11:10:37 UTC (rev 12949)
@@ -0,0 +1,52 @@
+#include "cli_base_cmd.h"
+#include <iostream>
+
+using namespace std;
+using namespace cigma;
+
+namespace po = boost::program_options;
+
+// ----------------------------------------------------------------------------
+
+void BaseCmd::printUsage()
+{
+ if (opts.options().size() == 0)
+ {
+ this->defineOptions();
+ }
+
+ if (usage != "")
+ {
+ cout << usage << endl;
+ }
+
+ if (opts.options().size() > 0)
+ {
+ cout << opts_section << endl;
+ cout << opts << endl;
+ }
+
+ if (appendix != "")
+ {
+ cout << appendix << endl;
+ }
+}
+
+void BaseCmd::defineOptions()
+{
+ opts_section = "Options: ";
+ opts.add_options()("help,h", "produce help message");
+}
+
+void BaseCmd::parseOptions(int argc, char *argv[])
+{
+ po::store(po::command_line_parser(argc, argv).options(opts).positional(args).run(), vm);
+}
+
+void BaseCmd::configure()
+{
+ po::notify(vm);
+}
+
+
+// ----------------------------------------------------------------------------
Added: cs/cigma/trunk/src/cli_base_cmd.h
===================================================================
--- cs/cigma/trunk/src/cli_base_cmd.h (rev 0)
+++ cs/cigma/trunk/src/cli_base_cmd.h 2008-09-24 11:10:37 UTC (rev 12949)
@@ -0,0 +1,56 @@
+#ifndef __CIGMA_BASE_CMD_H__
+#define __CIGMA_BASE_CMD_H__
+
+#include "cli_command.h"
+#include "Filesystem.h"
+#include <boost/program_options.hpp>
+
+namespace cigma
+{
+ class BaseCmd;
+}
+
+/**
+ * \brief Base class for commands that use boost::program_options.
+ * This class remains an abstract class as the Command::run method
+ * remains unimplemented.
+ */
+class cigma::BaseCmd : public cigma::Command
+{
+protected:
+ /**
+ * Provide a default implementation of Command::defineOptions.
+ * Here we just add the help argument to the opts object.
+ */
+ virtual void defineOptions();
+
+ /**
+ * Provide a default implementation of Command::parseOptions.
+ * Here we simply initialize the variables_map object,
+ * assuming that the descriptions opts & args have been
+ * properly initialized in a prior call to Command::defineOptions.
+ */
+ virtual void parseOptions(int argc, char *argv[]);
+
+ /**
+ * Provide a default implementation of Command::configure.
+ * We do nothing more than simply finalize the values in the
+ * variables_map object by calling po::notify(vm).
+ */
+ virtual void configure();
+
+public:
+ /**
+ * Print out a usage message based on the available information.
+ */
+ virtual void printUsage();
+
+public:
+ std::string opts_section;
+ boost::program_options::options_description opts;
+ boost::program_options::positional_options_description args;
+ boost::program_options::variables_map vm;
+};
+
+
+#endif
Added: cs/cigma/trunk/src/cli_command.cpp
===================================================================
--- cs/cigma/trunk/src/cli_command.cpp (rev 0)
+++ cs/cigma/trunk/src/cli_command.cpp 2008-09-24 11:10:37 UTC (rev 12949)
@@ -0,0 +1,35 @@
+#include "cli_command.h"
+#include <iostream>
+
+using namespace std;
+using namespace cigma;
+
+
+// ----------------------------------------------------------------------------
+
+Command::~Command()
+{
+}
+
+int Command::main(int argc, char *argv[])
+{
+ //cout << "Command::main()" << endl;
+
+ int status = 0;
+
+ // declare the schema for our cli options
+ this->defineOptions();
+
+ // parse the command-line options
+ this->parseOptions(argc, argv);
+
+ // prepare the internal state of the command object
+ this->configure();
+
+ // run the command
+ status = this->run();
+
+ return status;
+}
+
+// ----------------------------------------------------------------------------
Added: cs/cigma/trunk/src/cli_command.h
===================================================================
--- cs/cigma/trunk/src/cli_command.h (rev 0)
+++ cs/cigma/trunk/src/cli_command.h 2008-09-24 11:10:37 UTC (rev 12949)
@@ -0,0 +1,75 @@
+#ifndef __CIGMA_COMMAND_H__
+#define __CIGMA_COMMAND_H__
+
+#include <string>
+#include <boost/noncopyable.hpp>
+
+
+namespace cigma
+{
+ class Command;
+}
+
+
+/**
+ * \brief Defines the basic command interface.
+ */
+class cigma::Command : private boost::noncopyable
+{
+public:
+
+ virtual ~Command();
+
+ /**
+ * Print out the usage string for this command.
+ */
+ virtual void printUsage() = 0;
+
+
+protected:
+ /**
+ * Define the command-line options for this command. In here,
+ * we declare the list of valid options, default values,
+ * assign help information, etc.
+ */
+ virtual void defineOptions() = 0;
+
+
+ /**
+ * Parse the command line arguments, based on the schema declared above.
+ */
+ virtual void parseOptions(int argc, char *argv[]) = 0;
+
+
+ /**
+ * Configure the initial state of the command object.
+ * Here, we can validate the option values obtained from the user.
+ */
+ virtual void configure() = 0;
+
+
+ /**
+ * Actually run the command. We can assume the user data has
+ * already been validated at this stage.
+ *
+ * @return When successful return 0. Otherwise, gives a non-zero exit code.
+ */
+ virtual int run() = 0;
+
+
+public:
+ /**
+ * Main method of command object. We call this directly to ensure
+ * the above methods are declared in the correct order.
+ */
+ int main(int argc, char *argv[]);
+
+
+public:
+ std::string name; /// Name of the command being defined
+ std::string brief; /// Brief description of the command
+ std::string usage; /// Basic usage for this command
+ std::string appendix; /// Information to show after the options
+};
+
+#endif
Added: cs/cigma/trunk/src/cli_list_cmd.cpp
===================================================================
--- cs/cigma/trunk/src/cli_list_cmd.cpp (rev 0)
+++ cs/cigma/trunk/src/cli_list_cmd.cpp 2008-09-24 11:10:37 UTC (rev 12949)
@@ -0,0 +1,105 @@
+#include "cli_list_cmd.h"
+#include "Common.h"
+
+#include <iostream>
+#include <string>
+#include <vector>
+
+using namespace std;
+using namespace cigma;
+
+namespace fs = boost::filesystem;
+namespace po = boost::program_options;
+
+typedef vector<string> string_vector;
+
+// ----------------------------------------------------------------------------
+
+ListCmd::ListCmd()
+{
+ name = "list";
+ brief = "List file contents (fields, dimensions, ...)";
+
+ usage = "Usage: cigma list <FILE>\n";
+ appendix =
+ "Supported file extensions:\n"
+ " .h5 - Cigma HDF5 file\n"
+ " .vtk - Legacy VTK file\n"
+ " .vtu, .vts, .vtr - XML VTK file\n"
+ " .pvtu, .pvts, .pvtr - XML Parallel VTK files\n"
+ ;
+}
+
+ListCmd::~ListCmd()
+{
+}
+
+// ----------------------------------------------------------------------------
+
+void ListCmd::defineOptions()
+{
+ opts_section = "List Options: ";
+
+ opts.add_options()
+ ("help,h", "produce help message")
+ ("debug,D", "print debugging information")
+ ("input-file", po::value<string>(&op.filename), "input file")
+ ;
+
+ args.add("input-file", -1);
+}
+
+void ListCmd::configure()
+{
+ BaseCmd::configure();
+
+ if (!vm.count("debug"))
+ {
+ TRI_LOG_OFF();
+ }
+
+ //
+ // Validate input data as much as possible, i.e.,
+ // - Make sure the file exists.
+ // - If so, create the appropriate file object.
+ //
+ if (op.filename != "")
+ {
+ fs::path p(op.filename);
+ if (!fs::exists(p))
+ {
+ throw std::invalid_argument(string("The file '")
+ .append(op.filename).append("' does not exist!"));
+ }
+ if (fs::is_directory(p))
+ {
+ throw std::invalid_argument(string("Please specify a file, ")
+ .append("not a directory (given '")
+ .append(op.filename).append("')"));
+ }
+ }
+}
+
+int ListCmd::run()
+{
+ if (vm.count("help"))
+ {
+ this->printUsage();
+ return 1;
+ }
+
+ if (op.filename == "")
+ {
+ cout << usage;
+ return 1;
+ }
+
+ cout << "Reading file '" << op.filename << "'\n";
+
+ int status = op.listContents();
+
+ return status;
+}
+
+// ----------------------------------------------------------------------------
+
Added: cs/cigma/trunk/src/cli_list_cmd.h
===================================================================
--- cs/cigma/trunk/src/cli_list_cmd.h (rev 0)
+++ cs/cigma/trunk/src/cli_list_cmd.h 2008-09-24 11:10:37 UTC (rev 12949)
@@ -0,0 +1,31 @@
+#ifndef __CIGMA_LIST_CMD_H__
+#define __CIGMA_LIST_CMD_H__
+
+#include "cli_base_cmd.h"
+#include "core_list_op.h"
+
+namespace cigma
+{
+ class ListCmd;
+ class ListOp;
+}
+
+/**
+ * \brief Command for listing the contents of an input file.
+ */
+class cigma::ListCmd : public BaseCmd
+{
+public:
+ ListCmd();
+ ~ListCmd();
+
+public:
+ void defineOptions();
+ void configure();
+ int run();
+
+public:
+ ListOp op;
+};
+
+#endif
Added: cs/cigma/trunk/src/cli_skel_cmd.cpp
===================================================================
--- cs/cigma/trunk/src/cli_skel_cmd.cpp (rev 0)
+++ cs/cigma/trunk/src/cli_skel_cmd.cpp 2008-09-24 11:10:37 UTC (rev 12949)
@@ -0,0 +1,15 @@
+#include "cli_skel_cmd.h"
+using namespace cigma;
+
+void SkelCmd::defineOptions()
+{
+}
+
+void SkelCmd::parseOptions(int argc, char *argv[])
+{
+}
+
+void SkelCmd::configure()
+{
+}
+
Added: cs/cigma/trunk/src/cli_skel_cmd.h
===================================================================
--- cs/cigma/trunk/src/cli_skel_cmd.h (rev 0)
+++ cs/cigma/trunk/src/cli_skel_cmd.h 2008-09-24 11:10:37 UTC (rev 12949)
@@ -0,0 +1,24 @@
+#ifndef __CIGMA_SKEL_CMD_H__
+#define __CIGMA_SKEL_CMD_H__
+
+#include "cli_command.h"
+
+namespace cigma
+{
+ class SkelCmd;
+}
+
+/**
+ * \brief Barebones subclass of cigma::Command.
+ * This class remains abstract since any derived class
+ * must still implement the Command::run method.
+ */
+class cigma::SkelCmd : public cigma::Command
+{
+protected:
+ virtual void defineOptions();
+ virtual void parseOptions(int argc, char *argv[]);
+ virtual void configure();
+};
+
+#endif
More information about the cig-commits
mailing list