aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Stephen P. Bennett <spb@exherbo.org> 2006-01-17 18:57:23 +0000
committerAvatar Stephen P. Bennett <spb@exherbo.org> 2006-01-17 18:57:23 +0000
commit21ed2b404c653392c075ec72113e7a13b1f23d2b (patch)
tree54878302073e6864c3d520e1a7b3b3668b3167de
parente9caa130926c47aaedbbe0627944cd8a82ab14ae (diff)
downloadpaludis-21ed2b404c653392c075ec72113e7a13b1f23d2b.tar.gz
paludis-21ed2b404c653392c075ec72113e7a13b1f23d2b.tar.xz
EnumArg changes. Added description fields for each allowed arg, and allow for a default arg if it's not specified.
-rw-r--r--paludis/args/args_TEST.cc8
-rw-r--r--paludis/args/args_option.cc21
-rw-r--r--paludis/args/args_option.hh50
-rw-r--r--src/command_line.cc3
-rw-r--r--src/command_line.hh2
5 files changed, 69 insertions, 15 deletions
diff --git a/paludis/args/args_TEST.cc b/paludis/args/args_TEST.cc
index 4b0207e..b7b9495 100644
--- a/paludis/args/args_TEST.cc
+++ b/paludis/args/args_TEST.cc
@@ -31,8 +31,6 @@ using namespace test;
#ifndef DOXYGEN
-static const char * const arg_enum_args[3] = { "one", "two", "three" };
-
struct CommandLine : public ArgsHandler
{
ArgsGroup group_one;
@@ -46,6 +44,7 @@ struct CommandLine : public ArgsHandler
StringArg arg_something;
IntegerArg arg_somenum;
EnumArg arg_enum;
+ EnumArg arg_other_enum;
CommandLine() :
group_one(this, "Group one"),
@@ -58,7 +57,8 @@ struct CommandLine : public ArgsHandler
arg_other_baz(&arg_baz, "other-baz"),
arg_something(&group_two, "something", 's', "Value of something"),
arg_somenum(&group_two, "num", 'n', "Some number"),
- arg_enum(&group_two, "enum", 'e', "One of three", arg_enum_args)
+ arg_enum(&group_two, "enum", 'e', "One of three", EnumArg::EnumArgOptions("one", "Option one")("two", "option two")("three", "option three"), "two"),
+ arg_other_enum(&group_two, "something", '\0', "Blah.", EnumArg::EnumArgOptions("a", "A")("b", "B")("c", "C"), "b")
{
}
};
@@ -91,6 +91,8 @@ namespace test_cases
TEST_CHECK(c1.arg_enum.specified());
TEST_CHECK(c1.arg_enum.argument() == "three");
TEST_CHECK(! c1.arg_dummy.specified());
+ TEST_CHECK(! c1.arg_other_enum.specified());
+ TEST_CHECK(c1.arg_other_enum.argument() == "b");
TEST_CHECK_EQUAL(std::distance(c1.begin_parameters(), c1.end_parameters()), 3);
TEST_CHECK_EQUAL(*c1.begin_parameters(), "--dummy");
diff --git a/paludis/args/args_option.cc b/paludis/args/args_option.cc
index 7ec879d..72311d0 100644
--- a/paludis/args/args_option.cc
+++ b/paludis/args/args_option.cc
@@ -60,9 +60,20 @@ IntegerArg::IntegerArg(ArgsGroup * const group, const std::string& long_name,
void EnumArg::set_argument(const std::string & arg)
{
- if(_allowed_args.find(arg) == _allowed_args.end())
- {
- throw(BadValue("--" + long_name(), arg));
- }
- _argument = arg;
+ if(_allowed_args.find(arg) == _allowed_args.end())
+ {
+ throw(BadValue("--" + long_name(), arg));
+ }
+ _argument = arg;
+}
+
+EnumArg::EnumArgOptions::EnumArgOptions(std::string opt, std::string desc)
+{
+ _options.insert(std::make_pair(opt, desc));
+}
+
+EnumArg::EnumArgOptions & EnumArg::EnumArgOptions::operator() (std::string opt, std::string desc)
+{
+ _options.insert(std::make_pair(opt, desc));
+ return *this;
}
diff --git a/paludis/args/args_option.hh b/paludis/args/args_option.hh
index 47d9d47..2812f7f 100644
--- a/paludis/args/args_option.hh
+++ b/paludis/args/args_option.hh
@@ -21,7 +21,7 @@
#define PALUDIS_GUARD_ARGS_ARGS_OPTION_HH 1
#include <string>
-#include <set>
+#include <map>
#include "args_visitor.hh"
/** \file
@@ -202,19 +202,43 @@ namespace paludis
class EnumArg : public ArgsOption
{
private:
- const std::set<std::string> _allowed_args;
+ const std::map<std::string, std::string> _allowed_args;
std::string _argument;
public:
+
+ /**
+ * Helper class for passing available options and associated descriptions
+ * to the EnumArg constructor.
+ *
+ * \ingroup Args
+ */
+ class EnumArgOptions
+ {
+ private:
+ friend class EnumArg;
+ std::map<std::string, std::string> _options;
+
+ public:
+ /**
+ * Constructor
+ */
+ EnumArgOptions(const std::string, const std::string);
+
+ /**
+ * Adds another (option, description) pair.
+ */
+ EnumArgOptions& operator() (const std::string, const std::string);
+ };
+
/**
* Constructor.
*/
- template <typename T_>
EnumArg(ArgsGroup * const group, const std::string & long_name,
const char short_name, const std::string & description,
- const T_ * allowed_args) :
+ const EnumArgOptions & opts, const std::string & default_arg) :
ArgsOption(group, long_name, short_name, description),
- _allowed_args(&allowed_args[0], &allowed_args[sizeof(allowed_args)-1])
+ _allowed_args(opts._options), _argument(default_arg)
{
}
@@ -229,6 +253,22 @@ namespace paludis
*/
void set_argument(const std::string & arg);
+ /**
+ * Type used to iterate over valid arguments to this option
+ */
+ typedef std::map<std::string, std::string>::const_iterator AllowedArgIterator;
+
+ /**
+ * Returns an iterator pointing to a pair containing the first valid argument,
+ * and its description.
+ */
+ AllowedArgIterator begin_allowed_args() const { return _allowed_args.begin(); }
+
+ /**
+ * Returns an iterator pointing just beyond the last valid argument.
+ */
+ AllowedArgIterator end_allowed_args() const { return _allowed_args.end(); }
+
void accept(ArgsVisitor * const v)
{
v->visit(this);
diff --git a/src/command_line.cc b/src/command_line.cc
index 9ebe91f..b45a970 100644
--- a/src/command_line.cc
+++ b/src/command_line.cc
@@ -45,7 +45,8 @@ CommandLine::CommandLine() :
a_pretend(&install_args, "pretend", 'p', "Pretend only"),
dl_args(this, "DepList behaviour (use with caution)"),
- a_dl_rdepend_post(&dl_args, "dl-rdepend-post", '\0', "Treat RDEPEND like PDEPEND where necessary"),
+ a_dl_rdepend_post(&dl_args, "dl-rdepend-post", '\0', "Treat RDEPEND like PDEPEND where necessary",
+ paludis::args::EnumArg::EnumArgOptions("always", "Always")("never", "Never")("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"),
a_dl_ignore_installed(&dl_args, "dl-ignore-installed", '\0', "Ignore installed packages"),
diff --git a/src/command_line.hh b/src/command_line.hh
index 8ed1a34..e390607 100644
--- a/src/command_line.hh
+++ b/src/command_line.hh
@@ -114,7 +114,7 @@ class CommandLine :
paludis::args::ArgsGroup dl_args;
/// --dl-rdepend-post
- paludis::args::SwitchArg a_dl_rdepend_post;
+ paludis::args::EnumArg a_dl_rdepend_post;
/// --dl-drop-self-circular
paludis::args::SwitchArg a_dl_drop_self_circular;