aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Stephen P. Bennett <spb@exherbo.org> 2006-02-24 18:21:41 +0000
committerAvatar Stephen P. Bennett <spb@exherbo.org> 2006-02-24 18:21:41 +0000
commit6295d4483484c63557795890270a466b26190396 (patch)
tree329d02f87e596f1a492e306869dbd3cc6ca06793
parent60e5e89174db7ee8871e51ba544546ddd28f5bab (diff)
downloadpaludis-6295d4483484c63557795890270a466b26190396.tar.gz
paludis-6295d4483484c63557795890270a466b26190396.tar.xz
Add StringSetArg.
-rw-r--r--paludis/args/args_TEST.cc25
-rw-r--r--paludis/args/args_dumper.cc1
-rw-r--r--paludis/args/args_dumper.hh3
-rw-r--r--paludis/args/args_option.cc6
-rw-r--r--paludis/args/args_option.hh37
-rw-r--r--paludis/args/args_visitor.cc7
-rw-r--r--paludis/args/args_visitor.hh6
7 files changed, 82 insertions, 3 deletions
diff --git a/paludis/args/args_TEST.cc b/paludis/args/args_TEST.cc
index 6da14b9..9981d94 100644
--- a/paludis/args/args_TEST.cc
+++ b/paludis/args/args_TEST.cc
@@ -45,7 +45,10 @@ struct CommandLine : public ArgsHandler
StringArg arg_something;
IntegerArg arg_somenum;
EnumArg arg_enum;
+
+ ArgsGroup group_three;
EnumArg arg_other_enum;
+ StringSetArg arg_stringset;
CommandLine();
~CommandLine();
@@ -63,7 +66,10 @@ CommandLine::CommandLine() :
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", 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")
+
+ group_three(this, "Group three"),
+ arg_other_enum(&group_three, "something", '\0', "Blah.", EnumArg::EnumArgOptions("a", "A")("b", "B")("c", "C"), "b"),
+ arg_stringset(&group_three, "stringset", 't', "A StringSet.")
{
}
@@ -120,5 +126,22 @@ namespace test_cases
TEST_CHECK_THROWS(c1.run(2, args), MissingValue);
}
} test_args_no_param;
+
+ struct ArgsTestStringSet : TestCase
+ {
+ ArgsTestStringSet() : TestCase("StringSet") { }
+
+ void run()
+ {
+ char *args[] = { "program-name", "--stringset", "one", "-t", "two", "-t", "three", "fnord" };
+ CommandLine c1;
+ c1.run(8, args);
+ TEST_CHECK(c1.arg_stringset.specified());
+ TEST_CHECK(std::find(c1.arg_stringset.args_begin(), c1.arg_stringset.args_end(), "one") != c1.arg_stringset.args_end());
+ TEST_CHECK(std::find(c1.arg_stringset.args_begin(), c1.arg_stringset.args_end(), "two") != c1.arg_stringset.args_end());
+ TEST_CHECK(std::find(c1.arg_stringset.args_begin(), c1.arg_stringset.args_end(), "three") != c1.arg_stringset.args_end());
+ TEST_CHECK(std::find(c1.arg_stringset.args_begin(), c1.arg_stringset.args_end(), "fnord") == c1.arg_stringset.args_end());
+ }
+ } test_args_string_set;
}
diff --git a/paludis/args/args_dumper.cc b/paludis/args/args_dumper.cc
index 7110455..fcecd8f 100644
--- a/paludis/args/args_dumper.cc
+++ b/paludis/args/args_dumper.cc
@@ -52,6 +52,7 @@ VISIT(SwitchArg)
VISIT(StringArg)
VISIT(IntegerArg)
VISIT(AliasArg)
+VISIT(StringSetArg)
void ArgsDumper::visit(const EnumArg * const a)
{
diff --git a/paludis/args/args_dumper.hh b/paludis/args/args_dumper.hh
index beb36ed..1c2a45f 100644
--- a/paludis/args/args_dumper.hh
+++ b/paludis/args/args_dumper.hh
@@ -68,6 +68,9 @@ namespace paludis
/// Visit an EnumArg.
void visit(const EnumArg * const);
+
+ /// Visit a StringSetArg.
+ void visit(const StringSetArg * const);
};
}
}
diff --git a/paludis/args/args_option.cc b/paludis/args/args_option.cc
index a338719..3e2ea62 100644
--- a/paludis/args/args_option.cc
+++ b/paludis/args/args_option.cc
@@ -67,6 +67,12 @@ StringArg::StringArg(ArgsGroup * const g, const std::string & long_name,
{
}
+StringSetArg::StringSetArg(ArgsGroup * const g, const std::string & long_name,
+ const char short_name, const std::string & description) :
+ ArgsOption(g, long_name, short_name, description)
+{
+}
+
IntegerArg::IntegerArg(ArgsGroup * const group, const std::string& long_name,
char short_name, const std::string& description) :
ArgsOption(group, long_name, short_name, description)
diff --git a/paludis/args/args_option.hh b/paludis/args/args_option.hh
index 2e96186..251360a 100644
--- a/paludis/args/args_option.hh
+++ b/paludis/args/args_option.hh
@@ -22,7 +22,7 @@
#define PALUDIS_GUARD_ARGS_ARGS_OPTION_HH 1
#include <string>
-#include <map>
+#include <set>
#include <vector>
#include "args_visitor.hh"
@@ -170,6 +170,41 @@ namespace paludis
void set_argument(const std::string& arg) { _argument = arg; }
};
+ class StringSetArg : public ArgsOption, public Visitable<StringSetArg, ArgsVisitorTypes>
+ {
+ private:
+ std::set<std::string> _args;
+
+ public:
+
+ /**
+ * Type used to iterate over specified args.
+ */
+ typedef std::set<std::string>::const_iterator Iterator;
+
+ /**
+ * Constructor
+ */
+ StringSetArg(ArgsGroup * const, const std::string & long_name,
+ const char short_name, const std::string & description);
+
+ /**
+ * Retrieve an iterator to the first arg
+ */
+ Iterator args_begin() const { return _args.begin(); }
+
+ /**
+ * Retrieve an iterator one past the last arg
+ */
+ Iterator args_end() const { return _args.end(); }
+
+ /**
+ * Add an argument to the set returned by [ args_begin(), args_end() )
+ */
+ void add_argument(const std::string & arg) { _args.insert(arg); }
+ };
+
+
/**
* An AliasArg is an alias for another argument.
*
diff --git a/paludis/args/args_visitor.cc b/paludis/args/args_visitor.cc
index 7a058e6..ac5332e 100644
--- a/paludis/args/args_visitor.cc
+++ b/paludis/args/args_visitor.cc
@@ -82,3 +82,10 @@ void ArgsVisitor::visit(EnumArg * const arg)
visit(static_cast<ArgsOption*>(arg));
arg->set_argument(get_param(arg));
}
+
+void ArgsVisitor::visit(StringSetArg * const arg)
+{
+ visit(static_cast<ArgsOption *>(arg));
+ std::string param = get_param(arg);
+ arg->add_argument(param);
+}
diff --git a/paludis/args/args_visitor.hh b/paludis/args/args_visitor.hh
index 823ce96..18564c2 100644
--- a/paludis/args/args_visitor.hh
+++ b/paludis/args/args_visitor.hh
@@ -41,12 +41,13 @@ namespace paludis
class SwitchArg;
class IntegerArg;
class EnumArg;
+ class StringSetArg;
/**
* Visitor types for visitors that can visit Args.
*/
typedef VisitorTypes<ArgsOption *, StringArg *, AliasArg *, SwitchArg *,
- IntegerArg *, EnumArg *> ArgsVisitorTypes;
+ IntegerArg *, EnumArg *, StringSetArg *> ArgsVisitorTypes;
/**
* Visitor class. Processes command-line options as they are found.
@@ -81,6 +82,9 @@ namespace paludis
/// Visit an EnumArg.
void visit(EnumArg * const);
+
+ /// Visit a StringSetArg.
+ void visit(StringSetArg * const);
};
}
}