aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Saleem Abdulrasool <compnerd@compnerd.org> 2008-11-16 15:50:01 -0800
committerAvatar David Leverton <levertond@googlemail.com> 2008-11-17 21:53:03 +0000
commit15f1ce1eef979e22d35241a2c0d266d5bda4e754 (patch)
tree9a5feb701697021060d41b0c9e080aa339bbd9df
parent163a8c725b8f1f2ea1884b9d825945fe56ed0d5a (diff)
downloadpaludis-15f1ce1eef979e22d35241a2c0d266d5bda4e754.tar.gz
paludis-15f1ce1eef979e22d35241a2c0d266d5bda4e754.tar.xz
add cave-help
simple first try implementation of a 'cave help'. 'cave help' will list all important commands. 'cave help command' will display the help message for the specified command. 'cave help -a' will print out all the known commands.
-rw-r--r--src/clients/cave/Makefile.am2
-rw-r--r--src/clients/cave/cmd_help.cc158
-rw-r--r--src/clients/cave/cmd_help.hh41
-rw-r--r--src/clients/cave/command.cc6
-rw-r--r--src/clients/cave/command.hh2
-rw-r--r--src/clients/cave/command_factory.cc2
6 files changed, 211 insertions, 0 deletions
diff --git a/src/clients/cave/Makefile.am b/src/clients/cave/Makefile.am
index a70ef40..8323429 100644
--- a/src/clients/cave/Makefile.am
+++ b/src/clients/cave/Makefile.am
@@ -16,6 +16,7 @@ bin_PROGRAMS = cave
noinst_PROGRAMS = man-cave
command_MANS = \
+ cave-help.1 \
cave-print-categories.1 \
cave-print-commands.1 \
cave-print-environment-metadata.1 \
@@ -66,6 +67,7 @@ libcave_a_SOURCES = \
command.cc command.hh \
command_line.cc command_line.hh \
command_factory.cc command_factory.hh \
+ cmd_help.cc cmd_help.hh \
cmd_print_categories.cc cmd_print_categories.hh \
cmd_print_commands.cc cmd_print_commands.hh \
cmd_print_environment_metadata.cc cmd_print_environment_metadata.hh \
diff --git a/src/clients/cave/cmd_help.cc b/src/clients/cave/cmd_help.cc
new file mode 100644
index 0000000..c9a4ff7
--- /dev/null
+++ b/src/clients/cave/cmd_help.cc
@@ -0,0 +1,158 @@
+/* vim: set sw=4 sts=4 et fdm=syntax: */
+
+/*
+ * Copyright (c) 2008 Saleem Abdulrasool
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 55 Temple
+ * Place, Suite 330, Boston, MA 02111-1308 USA
+ */
+
+#include "cmd_help.hh"
+
+#include <cstdlib>
+#include <iostream>
+#include <algorithm>
+#include <functional>
+#include <paludis/args/args.hh>
+#include <paludis/args/do_help.hh>
+#include <paludis/util/sequence.hh>
+#include <paludis/util/make_shared_ptr.hh>
+
+#include "command_factory.hh"
+
+using namespace paludis;
+using namespace cave;
+
+using std::cout;
+
+namespace
+{
+ struct HelpCommandLine :
+ args::ArgsHandler
+ {
+ args::ArgsGroup g_general;
+ args::SwitchArg a_all;
+
+ HelpCommandLine() :
+ g_general(this, "General Options", "General Options"),
+ a_all(&g_general, "all", 'a', "Print all available commands to standard output.")
+ {
+ add_usage_line("[-a|--all] [COMMAND]");
+ }
+
+ virtual std::string app_name() const
+ {
+ return "cave help";
+ }
+
+ virtual std::string app_synopsis() const
+ {
+ return "display help information";
+ }
+
+ virtual std::string app_description() const
+ {
+ return "Display help information for a particular command.";
+ }
+ };
+
+ struct IsImportantAndLonger :
+ std::binary_function<std::string, std::string, bool>
+ {
+ bool
+ operator() (const std::string & left, const std::string & right)
+ {
+ std::tr1::shared_ptr<Command> lhs(CommandFactory::get_instance()->create(left));
+ std::tr1::shared_ptr<Command> rhs(CommandFactory::get_instance()->create(right));
+
+ if (lhs->important() && rhs->important())
+ return left.length() < right.length();
+ else if (lhs->important() && ! rhs->important())
+ return false;
+ else if (! lhs->important() && rhs->important())
+ return true;
+ else
+ return false;
+ }
+ };
+}
+
+int
+HelpCommand::run(const std::tr1::shared_ptr<Environment> & env,
+ const std::tr1::shared_ptr< const Sequence<std::string> > & args)
+{
+ HelpCommandLine cmdline;
+ cmdline.run(args, "CAVE", "CAVE_HELP_OPTIONS", "CAVE_HELP_CMDLINE");
+
+ if (cmdline.a_help.specified())
+ {
+ cout << cmdline;
+ return EXIT_SUCCESS;
+ }
+
+ if (cmdline.a_all.specified())
+ {
+ if (cmdline.begin_parameters() == cmdline.end_parameters())
+ {
+ cout << "available cave commands:" << std::endl;
+ std::copy(CommandFactory::get_instance()->begin(), CommandFactory::get_instance()->end(),
+ std::ostream_iterator<std::string>(cout, "\n"));
+ return EXIT_SUCCESS;
+ }
+ else
+ {
+ throw args::DoHelp("--" + cmdline.a_all.long_name() + " takes no arguments");
+ }
+ }
+
+ if (std::distance(cmdline.begin_parameters(), cmdline.end_parameters()) > 1)
+ throw args::DoHelp("help takes at most one parameter");
+
+ if (cmdline.begin_parameters() == cmdline.end_parameters())
+ {
+ size_t length(0);
+
+ CommandFactory::ConstIterator name(std::max_element(CommandFactory::get_instance()->begin(),CommandFactory::get_instance()->end(),
+ IsImportantAndLonger()));
+ if (name != CommandFactory::get_instance()->end())
+ length = name->length();
+
+ cout << "The most commonly used cave commands are:" << std::endl;
+ for (CommandFactory::ConstIterator cmd(CommandFactory::get_instance()->begin()), cmd_end(CommandFactory::get_instance()->end()) ;
+ cmd != cmd_end ; ++cmd)
+ {
+ std::tr1::shared_ptr<Command> instance(CommandFactory::get_instance()->create(*cmd));
+
+ if (instance->important())
+ cout << " " << *cmd << std::string(length - cmd->length(), ' ') << " "
+ << instance->make_doc_cmdline()->app_synopsis() << std::endl;
+ }
+
+ return EXIT_SUCCESS;
+ }
+ else
+ {
+ std::tr1::shared_ptr< Sequence<std::string> > help(make_shared_ptr(new Sequence<std::string>));
+ help->push_back("--help");
+
+ return CommandFactory::get_instance()->create(*cmdline.begin_parameters())->run(env, help);
+ }
+
+ return EXIT_FAILURE;
+}
+
+std::tr1::shared_ptr<args::ArgsHandler>
+HelpCommand::make_doc_cmdline()
+{
+ return make_shared_ptr(new HelpCommandLine);
+}
+
diff --git a/src/clients/cave/cmd_help.hh b/src/clients/cave/cmd_help.hh
new file mode 100644
index 0000000..02838a0
--- /dev/null
+++ b/src/clients/cave/cmd_help.hh
@@ -0,0 +1,41 @@
+/* vim: set sw=4 sts=4 et fdm=syntax: */
+
+/*
+ * Copyright (c) 2008 Saleem Abdulrasool
+ *
+ * This file is part of teh Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 55 Temple
+ * Place, Suite 330, Boston, MA 02111-1308 USA
+ */
+
+#ifndef PALUDIS_GUARD_SRC_CLIENTS_CAVE_CMD_HELP_HH
+#define PALUDIS_GUARD_SRC_CLIENTS_CAVE_CMD_HELP_HH 1
+
+#include "command.hh"
+
+namespace paludis
+{
+ namespace cave
+ {
+ class PALUDIS_VISIBLE HelpCommand :
+ public Command
+ {
+ public:
+ int run(const std::tr1::shared_ptr<Environment> &,
+ const std::tr1::shared_ptr< const Sequence<std::string> > & args);
+
+ std::tr1::shared_ptr<args::ArgsHandler> make_doc_cmdline();
+ };
+ }
+}
+
+#endif
+
diff --git a/src/clients/cave/command.cc b/src/clients/cave/command.cc
index 0fb1e6c..d337ba1 100644
--- a/src/clients/cave/command.cc
+++ b/src/clients/cave/command.cc
@@ -26,3 +26,9 @@ Command::~Command()
{
}
+bool
+Command::important() const
+{
+ return false;
+}
+
diff --git a/src/clients/cave/command.hh b/src/clients/cave/command.hh
index 25bcae9..14f646b 100644
--- a/src/clients/cave/command.hh
+++ b/src/clients/cave/command.hh
@@ -34,6 +34,8 @@ namespace paludis
public:
virtual ~Command() = 0;
+ virtual bool important() const;
+
virtual int run(
const std::tr1::shared_ptr<Environment> &,
const std::tr1::shared_ptr<const Sequence<std::string > > & args
diff --git a/src/clients/cave/command_factory.cc b/src/clients/cave/command_factory.cc
index 9f76c1d..0c40419 100644
--- a/src/clients/cave/command_factory.cc
+++ b/src/clients/cave/command_factory.cc
@@ -26,6 +26,7 @@
#include <tr1/functional>
#include <map>
+#include "cmd_help.hh"
#include "cmd_print_categories.hh"
#include "cmd_print_commands.hh"
#include "cmd_print_environment_metadata.hh"
@@ -64,6 +65,7 @@ namespace
CommandFactory::CommandFactory() :
PrivateImplementationPattern<CommandFactory>(new Implementation<CommandFactory>)
{
+ _imp->handlers.insert(std::make_pair("help", make_command<HelpCommand>));
_imp->handlers.insert(std::make_pair("print-sync-protocols", make_command<PrintSyncProtocolsCommand>));
_imp->handlers.insert(std::make_pair("print-categories", make_command<PrintCategoriesCommand>));
_imp->handlers.insert(std::make_pair("print-commands", make_command<PrintCommandsCommand>));