[cig-commits] r8930 - cs/benchmark/cigma/trunk/src
luis at geodynamics.org
luis at geodynamics.org
Wed Dec 19 12:04:37 PST 2007
Author: luis
Date: 2007-12-19 12:04:36 -0800 (Wed, 19 Dec 2007)
New Revision: 8930
Added:
cs/benchmark/cigma/trunk/src/HelpCmd.cpp
cs/benchmark/cigma/trunk/src/HelpCmd.h
Log:
Callback command for `cigma help <subcommand>'
Added: cs/benchmark/cigma/trunk/src/HelpCmd.cpp
===================================================================
--- cs/benchmark/cigma/trunk/src/HelpCmd.cpp 2007-12-19 20:04:28 UTC (rev 8929)
+++ cs/benchmark/cigma/trunk/src/HelpCmd.cpp 2007-12-19 20:04:36 UTC (rev 8930)
@@ -0,0 +1,113 @@
+#include <iostream>
+#include <cassert>
+#include "HelpCmd.h"
+#include "CommandSet.h"
+
+// ---------------------------------------------------------------------------
+
+cigma::HelpCmd::HelpCmd()
+{
+ name = "help";
+ commands = 0;
+}
+
+
+cigma::HelpCmd::~HelpCmd()
+{
+}
+
+
+void cigma::HelpCmd::setCmdMap(CommandSet::CmdMap *cmds)
+{
+ /* pointer to set of commands */
+ this->commands = cmds;
+
+ /* prepare usage list from current set of commands */
+ usageList.clear();
+ std::string prefix = " ";
+ CommandSet::CmdMap::iterator it;
+ for (it = commands->begin(); it != commands->end(); ++it)
+ {
+ Command *cmd = it->second;
+ usageList.push_back(prefix + (cmd->name));
+ }
+}
+
+// ---------------------------------------------------------------------------
+
+void cigma::HelpCmd::setupOptions(AnyOption *opt)
+{
+ std::cout << "Calling cigma::HelpCmd::setupOptions()" << std::endl;
+
+ assert(opt != 0);
+
+ /* prepare preamble */
+ opt->addUsage("Usage: cigma <subcommand> [options] [args]");
+ opt->addUsage("Type 'cigma help <subcommand>' for help on a specific subcommand.");
+ opt->addUsage("");
+ opt->addUsage("Available subcommands");
+
+ /* pass contents of usage list to opt object */
+ std::vector<std::string>::iterator it;
+ for (it = usageList.begin(); it != usageList.end(); ++it)
+ {
+ opt->addUsage(it->c_str());
+ }
+
+ // XXX: need to set at least one flag or option (bug in AnyOption)
+ opt->setFlag("help",'h');
+}
+
+
+void cigma::HelpCmd::configure(AnyOption *opt)
+{
+ std::cout << "Calling cigma::HelpCmd::configure()" << std::endl;
+
+ assert(opt != 0);
+
+ int argc = opt->getArgc();
+
+ subcommand = "";
+ if (argc == 0)
+ {
+ subcommand = "help";
+ }
+ else
+ {
+ subcommand = opt->getArgv(0);
+ if (argc >= 2)
+ {
+ // XXX: too many args!
+ std::cerr << "Too many arguments! "
+ << "Taking only '"
+ << subcommand
+ << "'" << std::endl;
+ }
+ }
+}
+
+
+int cigma::HelpCmd::run()
+{
+ std::cout << "Calling cigma::HelpCmd::run()" << std::endl;
+
+ CommandSet::CmdMap::iterator it = commands->find(subcommand);
+
+ if (it != commands->end())
+ {
+ AnyOption opt;
+ Command *cmd = it->second;
+ cmd->setupOptions(&opt);
+ opt.printUsage();
+ return 0;
+ }
+ else
+ {
+ std::cerr << "Unknown command: '"
+ << subcommand
+ << "'" << std::endl;
+ }
+
+ return 1;
+}
+
Added: cs/benchmark/cigma/trunk/src/HelpCmd.h
===================================================================
--- cs/benchmark/cigma/trunk/src/HelpCmd.h 2007-12-19 20:04:28 UTC (rev 8929)
+++ cs/benchmark/cigma/trunk/src/HelpCmd.h 2007-12-19 20:04:36 UTC (rev 8930)
@@ -0,0 +1,39 @@
+#ifndef __HELP_CMD_H__
+#define __HELP_CMD_H__
+
+#include "Command.h"
+#include "CommandSet.h"
+#include <vector>
+
+namespace cigma
+{
+ class HelpCmd;
+}
+
+
+/**
+ * @brief Callback object for `cigma help [args ...]'
+ *
+ */
+class cigma::HelpCmd : public Command
+{
+public:
+ HelpCmd();
+ ~HelpCmd();
+
+public:
+ void setupOptions(AnyOption *opt);
+ void configure(AnyOption *opt);
+ int run();
+
+public:
+ void setCmdMap(CommandSet::CmdMap *cmds);
+
+public:
+ CommandSet::CmdMap *commands;
+ std::string subcommand;
+ std::vector<std::string> usageList;
+};
+
+
+#endif
More information about the cig-commits
mailing list