aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar David Leverton <levertond@googlemail.com> 2007-10-01 13:52:55 +0000
committerAvatar David Leverton <levertond@googlemail.com> 2007-10-01 13:52:55 +0000
commit10c60e65e1c66ac8eb734b2bae9ec14a77b85d66 (patch)
tree5f3b497245f91595962f0c7cbcd075acca33a069
parentbbd949c77f39d5ca561e3f59f480ccb4362283fe (diff)
downloadpaludis-10c60e65e1c66ac8eb734b2bae9ec14a77b85d66.tar.gz
paludis-10c60e65e1c66ac8eb734b2bae9ec14a77b85d66.tar.xz
Allow removing arguments / changing the defaults after construction.
-rw-r--r--paludis/args/args_TEST.cc58
-rw-r--r--paludis/args/args_group.cc6
-rw-r--r--paludis/args/args_group.hh6
-rw-r--r--paludis/args/args_handler.cc8
-rw-r--r--paludis/args/args_handler.hh5
-rw-r--r--paludis/args/args_option.cc14
-rw-r--r--paludis/args/args_option.hh16
7 files changed, 111 insertions, 2 deletions
diff --git a/paludis/args/args_TEST.cc b/paludis/args/args_TEST.cc
index c2e46b1..cf0efc6 100644
--- a/paludis/args/args_TEST.cc
+++ b/paludis/args/args_TEST.cc
@@ -40,6 +40,7 @@ struct CommandLine : public ArgsHandler
SwitchArg arg_foo;
SwitchArg arg_bar;
SwitchArg arg_dummy;
+ SwitchArg arg_removed;
ArgsGroup group_two;
SwitchArg arg_baz;
@@ -78,6 +79,7 @@ CommandLine::CommandLine() :
arg_foo(&group_one, "foo", 'f', "Enable foo"),
arg_bar(&group_one, "bar", 'b', "Enable bar"),
arg_dummy(&group_one, "dummy", 'd', "Enable something else"),
+ arg_removed(&group_one, "removed", 'r', "Removed"),
group_two(this, "Group two", "Description of group two"),
arg_baz(&group_two, "baz", 'z', "Enable baz"),
@@ -156,6 +158,62 @@ namespace test_cases
} test_args_no_param;
/**
+ * \test Removed arguments tests.
+ *
+ */
+ struct ArgsTestRemovedArg : TestCase
+ {
+ ArgsTestRemovedArg() : TestCase("Removed arguments") { }
+
+ void run()
+ {
+ const char *args1[] = { "program-name", "--removed" };
+ const char *args2[] = { "program-name", "-r" };
+ CommandLine c1;
+ c1.run(2, args1, "", "", "");
+ TEST_CHECK(true);
+ c1.run(2, args2, "", "", "");
+ TEST_CHECK(true);
+ c1.arg_removed.remove();
+ TEST_CHECK_THROWS(c1.run(2, args1, "", "", ""), BadArgument);
+ TEST_CHECK_THROWS(c1.run(2, args2, "", "", ""), BadArgument);
+ }
+ } test_args_removed_arg;
+
+ /**
+ * \test Default arguments tests.
+ *
+ */
+ struct ArgsTestDefaultArg : TestCase
+ {
+ ArgsTestDefaultArg() : TestCase("Default arguments") { }
+
+ void run()
+ {
+ const char *args1[] = { "program-name", "--enum", "three" };
+ const char *args2[] = { "program-name" };
+ CommandLine c1, c2, c3, c4;
+ c2.arg_enum.set_default_arg("one");
+ c4.arg_enum.set_default_arg("one");
+
+ c1.run(3, args1, "", "", "");
+ c2.run(3, args1, "", "", "");
+ c1.run(1, args2, "", "", "");
+ c2.run(1, args2, "", "", "");
+
+ TEST_CHECK(c1.arg_enum.specified());
+ TEST_CHECK(c2.arg_enum.specified());
+ TEST_CHECK(! c3.arg_enum.specified());
+ TEST_CHECK(! c4.arg_enum.specified());
+
+ TEST_CHECK_EQUAL(c1.arg_enum.argument(), "three");
+ TEST_CHECK_EQUAL(c2.arg_enum.argument(), "three");
+ TEST_CHECK_EQUAL(c3.arg_enum.argument(), "two");
+ TEST_CHECK_EQUAL(c4.arg_enum.argument(), "one");
+ }
+ } test_args_default_arg;
+
+ /**
* \test String tests.
*
*/
diff --git a/paludis/args/args_group.cc b/paludis/args/args_group.cc
index 671f3a1..ea12a86 100644
--- a/paludis/args/args_group.cc
+++ b/paludis/args/args_group.cc
@@ -60,6 +60,12 @@ ArgsGroup::add(ArgsOption * const value)
_imp->args_options.push_back(value);
}
+void
+ArgsGroup::remove(ArgsOption * const value)
+{
+ _imp->args_options.remove(value);
+}
+
ArgsGroup::~ArgsGroup()
{
}
diff --git a/paludis/args/args_group.hh b/paludis/args/args_group.hh
index 31bac42..5b4b6ec 100644
--- a/paludis/args/args_group.hh
+++ b/paludis/args/args_group.hh
@@ -71,6 +71,12 @@ namespace paludis
*/
void add(ArgsOption * const value);
+ /**
+ * Remove an ArgsOption instance (called by
+ * ArgsOption::remove).
+ */
+ void remove(ArgsOption * const value);
+
///\name Iterate over our ArgsOptions.
///\{
diff --git a/paludis/args/args_handler.cc b/paludis/args/args_handler.cc
index efb3da5..055cb99 100644
--- a/paludis/args/args_handler.cc
+++ b/paludis/args/args_handler.cc
@@ -214,6 +214,14 @@ ArgsHandler::add_option(ArgsOption * const opt, const std::string & long_name,
_imp->shortopts[short_name] = opt;
}
+void
+ArgsHandler::remove_option(const std::string & long_name, const char short_name)
+{
+ _imp->longopts.erase(long_name);
+ if (short_name != '\0')
+ _imp->shortopts.erase(short_name);
+}
+
ArgsHandler::UsageLineConstIterator
ArgsHandler::begin_usage_lines() const
{
diff --git a/paludis/args/args_handler.hh b/paludis/args/args_handler.hh
index 16ae954..b7787f6 100644
--- a/paludis/args/args_handler.hh
+++ b/paludis/args/args_handler.hh
@@ -119,6 +119,11 @@ namespace paludis
void add_option(ArgsOption * const, const std::string & long_name,
const char short_name = '\0');
+ /**
+ * Remove an ArgsOption instance.
+ */
+ void remove_option(const std::string & long_name, const char short_name = '\0');
+
///\name About our application (for documentation)
///\{
diff --git a/paludis/args/args_option.cc b/paludis/args/args_option.cc
index c61a145..bab8dec 100644
--- a/paludis/args/args_option.cc
+++ b/paludis/args/args_option.cc
@@ -75,6 +75,13 @@ ArgsOption::~ArgsOption()
{
}
+void
+ArgsOption::remove()
+{
+ _group->remove(this);
+ _group->handler()->remove_option(_long_name, _short_name);
+}
+
SwitchArg::SwitchArg(ArgsGroup * const our_group, std::string our_long_name, char our_short_name,
std::string our_description) :
ArgsOption(our_group, our_long_name, our_short_name, our_description)
@@ -297,6 +304,13 @@ EnumArg::EnumArg(ArgsGroup * const our_group, const std::string & our_long_name,
_imp->allowed_args = opts._imp->options;
}
+void
+EnumArg::set_default_arg(const std::string & arg)
+{
+ _argument = arg;
+ _default_arg = arg;
+}
+
EnumArg::AllowedArgConstIterator
EnumArg::begin_allowed_args() const
{
diff --git a/paludis/args/args_option.hh b/paludis/args/args_option.hh
index e5ec965..8c28d48 100644
--- a/paludis/args/args_option.hh
+++ b/paludis/args/args_option.hh
@@ -72,6 +72,11 @@ namespace paludis
public:
/**
+ * Remove this option.
+ */
+ void remove();
+
+ /**
* Fetch our long name.
*/
const std::string & long_name() const
@@ -357,7 +362,7 @@ namespace paludis
{
private:
std::string _argument;
- const std::string _default_arg;
+ std::string _default_arg;
public:
/**
@@ -412,7 +417,14 @@ namespace paludis
void set_argument(const std::string & arg);
/**
- * Fetch the default option, as specified to the constructor.
+ * Change the default option (should be called before
+ * set_argument()).
+ */
+ void set_default_arg(const std::string & arg);
+
+ /**
+ * Fetch the default option, as specified to the
+ * constructor or set_default_arg().
*/
const std::string & default_arg() const
{