aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Stephen P. Bennett <spb@exherbo.org> 2006-01-20 00:43:06 +0000
committerAvatar Stephen P. Bennett <spb@exherbo.org> 2006-01-20 00:43:06 +0000
commit14cf958649905bf83c06ae072b8c19f051cf6c73 (patch)
tree79de670275e5a325df3cfff6ae85d8d2d5ac5e05
parentf76ef486eac573d6124b205187acd8709f62533f (diff)
downloadpaludis-14cf958649905bf83c06ae072b8c19f051cf6c73.tar.gz
paludis-14cf958649905bf83c06ae072b8c19f051cf6c73.tar.xz
Converted --help output to use a visitor, and handle EnumArg better
-rw-r--r--paludis/args/Makefile.am3
-rw-r--r--paludis/args/args_dumper.cc72
-rw-r--r--paludis/args/args_dumper.hh75
-rw-r--r--paludis/args/args_handler.cc11
-rw-r--r--paludis/args/args_option.hh10
-rw-r--r--src/command_line.cc2
6 files changed, 161 insertions, 12 deletions
diff --git a/paludis/args/Makefile.am b/paludis/args/Makefile.am
index 30bc7dd..4ff3818 100644
--- a/paludis/args/Makefile.am
+++ b/paludis/args/Makefile.am
@@ -15,7 +15,8 @@ libpaludisargs_a_SOURCES = \
bad_argument.hh bad_argument.cc \
switch_arg.hh switch_arg.cc \
args_visitor.hh args_visitor.cc \
- bad_value.hh bad_value.cc
+ bad_value.hh bad_value.cc \
+ args_dumper.hh args_dumper.cc
TESTS_ENVIRONMENT = env TEST_SCRIPT_DIR="$(srcdir)/" $(SHELL) $(top_srcdir)/test/run_test.sh
noinst_LIBRARIES = libpaludisargs.a
diff --git a/paludis/args/args_dumper.cc b/paludis/args/args_dumper.cc
new file mode 100644
index 0000000..3619b6e
--- /dev/null
+++ b/paludis/args/args_dumper.cc
@@ -0,0 +1,72 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2006 Stephen Bennett <spb@gentoo.org>
+ *
+ * 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 as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * 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., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <paludis/args/args_dumper.hh>
+#include <paludis/args/args_option.hh>
+#include <paludis/args/switch_arg.hh>
+#include <paludis/args/alias_arg.hh>
+
+#include <sstream>
+
+using namespace paludis;
+using namespace paludis::args;
+
+ArgsDumper::ArgsDumper(std::ostream & os) :
+ _os(os)
+{
+}
+
+void ArgsDumper::visit(const ArgsOption * const a)
+{
+ std::stringstream p;
+ p << " --" << a->long_name();
+ if (a->short_name())
+ p << ", -" << a->short_name();
+ if (p.str().length() < 24)
+ p << std::string(24 - p.str().length(), ' ');
+ _os << p.str();
+ _os << " " << a->description() << std::endl;
+}
+
+#define VISIT(type) void ArgsDumper::visit(const type * const a) \
+ { visit(static_cast<const ArgsOption *>(a)); }
+
+VISIT(SwitchArg)
+VISIT(StringArg)
+VISIT(IntegerArg)
+VISIT(AliasArg)
+
+void ArgsDumper::visit(const EnumArg * const a)
+{
+ visit(static_cast<const ArgsOption *>(a));
+ for(EnumArg::AllowedArgIterator it = a->begin_allowed_args(), it_end = a->end_allowed_args();
+ it != it_end; ++it)
+ {
+ std::stringstream p;
+ p << " " << (*it).first;
+ if (p.str().length() < 26)
+ p << std::string(26 - p.str().length(), ' ');
+ _os << p.str();
+ _os << " " << (*it).second;
+ if ((*it).first == a->default_arg())
+ _os << " (default)";
+ _os << std::endl;
+ }
+}
diff --git a/paludis/args/args_dumper.hh b/paludis/args/args_dumper.hh
new file mode 100644
index 0000000..beb36ed
--- /dev/null
+++ b/paludis/args/args_dumper.hh
@@ -0,0 +1,75 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2006 Stephen Bennett <spb@gentoo.org>
+ *
+ * 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 as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * 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., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef PALUDIS_GUARD_PALUDIS_ARGS_ARGS_DUMPER_HH
+#define PALUDIS_GUARD_PALUDIS_ARGS_ARGS_DUMPER_HH 1
+
+#include <paludis/visitor.hh>
+#include <ostream>
+
+#include "args_visitor.hh"
+
+namespace paludis
+{
+ namespace args
+ {
+ class ArgsOption;
+ class SwitchArg;
+ class StringArg;
+ class IntegerArg;
+ class AliasArg;
+ class EnumArg;
+
+ /**
+ * Visitor class. Prints help text appropriate to each command line option.
+ */
+ class ArgsDumper : public ArgsVisitorTypes::ConstVisitor
+ {
+ private:
+ std::ostream & _os;
+
+ public:
+ /**
+ * Constructor.
+ */
+ ArgsDumper(std::ostream & os);
+
+ /// Visit an ArgsOption.
+ void visit(const ArgsOption * const);
+
+ /// Visit a SwitchArg.
+ void visit(const SwitchArg * const);
+
+ /// Visit a StringArg.
+ void visit(const StringArg * const);
+
+ /// Visit an IntegerArg.
+ void visit(const IntegerArg * const);
+
+ /// Visit an AliasArg.
+ void visit(const AliasArg * const);
+
+ /// Visit an EnumArg.
+ void visit(const EnumArg * const);
+ };
+ }
+}
+
+#endif
diff --git a/paludis/args/args_handler.cc b/paludis/args/args_handler.cc
index 0cbfd6a..5d0be78 100644
--- a/paludis/args/args_handler.cc
+++ b/paludis/args/args_handler.cc
@@ -18,6 +18,7 @@
*/
#include "args.hh"
+#include "args_dumper.hh"
/** \file
* Implementation for ArgsHandler.
@@ -96,6 +97,7 @@ ArgsHandler::run(const int argc, const char * const * const argv)
void
ArgsHandler::dump_to_stream(std::ostream & s) const
{
+ ArgsDumper dump(s);
std::list<ArgsGroup *>::const_iterator g(_groups.begin()), g_end(_groups.end());
for ( ; g != g_end ; ++g)
{
@@ -105,14 +107,7 @@ ArgsHandler::dump_to_stream(std::ostream & s) const
a_end((*g)->_args_options.end());
for ( ; a != a_end ; ++a)
{
- std::stringstream p;
- p << " --" << (*a)->long_name();
- if ((*a)->short_name())
- p << ", -" << (*a)->short_name();
- if (p.str().length() < 24)
- p << std::string(24 - p.str().length(), ' ');
- s << p.str();
- s << " " << (*a)->description() << std::endl;
+ (*a)->accept(&dump);
}
s << std::endl;
diff --git a/paludis/args/args_option.hh b/paludis/args/args_option.hh
index c292493..4dfd3eb 100644
--- a/paludis/args/args_option.hh
+++ b/paludis/args/args_option.hh
@@ -189,7 +189,7 @@ namespace paludis
{
private:
const std::map<std::string, std::string> _allowed_args;
- std::string _argument;
+ std::string _argument, _default_arg;
public:
@@ -224,7 +224,8 @@ namespace paludis
const char short_name, const std::string & description,
const EnumArgOptions & opts, const std::string & default_arg) :
ArgsOption(group, long_name, short_name, description),
- _allowed_args(opts._options), _argument(default_arg)
+ _allowed_args(opts._options), _argument(default_arg),
+ _default_arg(default_arg)
{
}
@@ -240,6 +241,11 @@ namespace paludis
void set_argument(const std::string & arg);
/**
+ * Fetch the default option, as specified to the constructor.
+ */
+ const std::string & default_arg() const { return _default_arg; }
+
+ /**
* Type used to iterate over valid arguments to this option
*/
typedef std::map<std::string, std::string>::const_iterator AllowedArgIterator;
diff --git a/src/command_line.cc b/src/command_line.cc
index 131e15e..c691acb 100644
--- a/src/command_line.cc
+++ b/src/command_line.cc
@@ -48,7 +48,7 @@ CommandLine::CommandLine() :
a_dl_rdepend_post(&dl_args, "dl-rdepend-post", '\0', "Treat RDEPEND like PDEPEND",
paludis::args::EnumArg::EnumArgOptions("always", "Always")
("never", "Never")
- ("as-needed", "Only where needed to resolve circular dependencies (default)"),
+ ("as-needed", "Only where needed to resolve circular dependencies"),
"as-needed"),
a_dl_drop_self_circular(&dl_args, "dl-drop-self-circular", '\0', "Drop self-circular dependencies"),
a_dl_drop_circular(&dl_args, "dl-drop-circular", '\0', "Drop circular dependencies"),