aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-01-11 10:47:32 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-01-11 10:47:32 +0000
commitef7485e4170017907eb82a28d7f60779b5578c63 (patch)
treedf6415c56f7b3704b86f2b7b24590a3e69589706
parent006546a26261d1bfd461d0cf9eda281dd02cd3ce (diff)
downloadpaludis-ef7485e4170017907eb82a28d7f60779b5578c63.tar.gz
paludis-ef7485e4170017907eb82a28d7f60779b5578c63.tar.xz
Let StringSetArg specify restricted allowable values.
-rw-r--r--paludis/args/args_dumper.cc21
-rw-r--r--paludis/args/args_option.cc116
-rw-r--r--paludis/args/args_option.hh53
3 files changed, 159 insertions, 31 deletions
diff --git a/paludis/args/args_dumper.cc b/paludis/args/args_dumper.cc
index 425ca04..5c0e0ac 100644
--- a/paludis/args/args_dumper.cc
+++ b/paludis/args/args_dumper.cc
@@ -50,7 +50,6 @@ void ArgsDumper::visit(const ArgsOption * const a)
_os << " " << a->description() << std::endl;
}
-#ifndef DOXYGEN
#define VISIT(type) void ArgsDumper::visit(const type * const a) \
{ visit(static_cast<const ArgsOption *>(a)); }
@@ -58,8 +57,24 @@ VISIT(SwitchArg)
VISIT(StringArg)
VISIT(IntegerArg)
VISIT(AliasArg)
-VISIT(StringSetArg)
-#endif
+
+void ArgsDumper::visit(const StringSetArg * const a)
+{
+ visit(static_cast<const ArgsOption *>(a));
+
+ if (a->begin_allowed_args() != a->end_allowed_args())
+ for (StringSetArg::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;
+ _os << std::endl;
+ }
+}
void ArgsDumper::visit(const EnumArg * const a)
{
diff --git a/paludis/args/args_option.cc b/paludis/args/args_option.cc
index 9537b26..82f4db7 100644
--- a/paludis/args/args_option.cc
+++ b/paludis/args/args_option.cc
@@ -31,6 +31,32 @@
using namespace paludis::args;
+namespace
+{
+ /**
+ * Is an arg a particular value?
+ *
+ * \ingroup grplibpaludisargs
+ */
+ struct ArgIs
+ {
+ /// The argument.
+ const std::string arg;
+
+ /// Constructor.
+ ArgIs(const std::string & a) :
+ arg(a)
+ {
+ }
+
+ /// Comparator.
+ bool operator() (const std::pair<std::string, std::string> & p) const
+ {
+ return p.first == arg;
+ }
+ };
+}
+
ArgsOption::ArgsOption(ArgsGroup * const g, const std::string & our_long_name,
const char our_short_name, const std::string & our_description) :
_group(g),
@@ -82,14 +108,30 @@ namespace paludis
InternalCounted<Implementation<StringSetArg> >
{
std::set<std::string> args;
+ std::vector<std::pair<std::string, std::string> > allowed_args;
+ };
+
+ /**
+ * Implementation data for StringSetArg::StringSetArgOptions.
+ *
+ * \ingroup grplibpaludisargs
+ */
+ template<>
+ struct Implementation<StringSetArg::StringSetArgOptions> :
+ InternalCounted<Implementation<StringSetArg::StringSetArgOptions> >
+ {
+ std::vector<std::pair<std::string, std::string> > options;
};
}
StringSetArg::StringSetArg(ArgsGroup * const g, const std::string & our_long_name,
- const char our_short_name, const std::string & our_description) :
+ const char our_short_name, const std::string & our_description,
+ const StringSetArgOptions & opts) :
ArgsOption(g, our_long_name, our_short_name, our_description),
PrivateImplementationPattern<StringSetArg>(new Implementation<StringSetArg>)
{
+ std::copy(opts._imp->options.begin(), opts._imp->options.end(),
+ std::back_inserter(_imp->allowed_args));
}
StringSetArg::Iterator
@@ -107,6 +149,11 @@ StringSetArg::args_end() const
void
StringSetArg::add_argument(const std::string & arg)
{
+ if (! _imp->allowed_args.empty())
+ if (_imp->allowed_args.end() == std::find_if(_imp->allowed_args.begin(),
+ _imp->allowed_args.end(), ArgIs(arg)))
+ throw (BadValue("--" + long_name(), arg));
+
_imp->args.insert(arg);
}
@@ -116,32 +163,6 @@ IntegerArg::IntegerArg(ArgsGroup * const our_group, const std::string & our_long
{
}
-namespace
-{
- /**
- * Is an arg a particular value?
- *
- * \ingroup grplibpaludisargs
- */
- struct ArgIs
- {
- /// The argument.
- const std::string arg;
-
- /// Constructor.
- ArgIs(const std::string & a) :
- arg(a)
- {
- }
-
- /// Comparator.
- bool operator() (const std::pair<std::string, std::string> & p) const
- {
- return p.first == arg;
- }
- };
-}
-
namespace paludis
{
/**
@@ -169,6 +190,35 @@ namespace paludis
};
}
+StringSetArg::StringSetArgOptions::StringSetArgOptions(std::string opt, std::string desc) :
+ PrivateImplementationPattern<StringSetArgOptions>(new Implementation<StringSetArgOptions>)
+{
+ _imp->options.push_back(std::make_pair(opt, desc));
+}
+
+StringSetArg::StringSetArgOptions &
+StringSetArg::StringSetArgOptions::operator() (std::string opt, std::string desc)
+{
+ _imp->options.push_back(std::make_pair(opt, desc));
+ return *this;
+}
+
+StringSetArg::StringSetArgOptions::StringSetArgOptions(const StringSetArg::StringSetArgOptions & o) :
+ PrivateImplementationPattern<StringSetArgOptions>(new Implementation<StringSetArgOptions>)
+{
+ std::copy(o._imp->options.begin(), o._imp->options.end(),
+ std::back_inserter(_imp->options));
+}
+
+StringSetArg::StringSetArgOptions::~StringSetArgOptions()
+{
+}
+
+StringSetArg::StringSetArgOptions::StringSetArgOptions() :
+ PrivateImplementationPattern<StringSetArgOptions>(new Implementation<StringSetArgOptions>)
+{
+}
+
void EnumArg::set_argument(const std::string & arg)
{
if (_imp->allowed_args.end() == std::find_if(_imp->allowed_args.begin(),
@@ -225,3 +275,15 @@ StringSetArg::~StringSetArg()
{
}
+StringSetArg::AllowedArgIterator
+StringSetArg::begin_allowed_args() const
+{
+ return AllowedArgIterator(_imp->allowed_args.begin());
+}
+
+StringSetArg::AllowedArgIterator
+StringSetArg::end_allowed_args() const
+{
+ return AllowedArgIterator(_imp->allowed_args.end());
+}
+
diff --git a/paludis/args/args_option.hh b/paludis/args/args_option.hh
index 601a110..807d823 100644
--- a/paludis/args/args_option.hh
+++ b/paludis/args/args_option.hh
@@ -183,11 +183,50 @@ namespace paludis
private PrivateImplementationPattern<StringSetArg>
{
public:
+ /**
+ * Helper class for passing available options and associated descriptions
+ * to the StringSetArg constructor.
+ *
+ * \ingroup grplibpaludisargs
+ */
+ class PALUDIS_VISIBLE StringSetArgOptions :
+ private PrivateImplementationPattern<StringSetArgOptions>
+ {
+ friend class StringSetArg;
+
+ public:
+ /**
+ * Constructor
+ */
+ StringSetArgOptions(const std::string, const std::string);
+
+ /**
+ * Blank constructor
+ */
+ explicit StringSetArgOptions();
+
+ /**
+ * Copy constructor
+ */
+ StringSetArgOptions(const StringSetArgOptions &);
+
+ /**
+ * Destructor.
+ */
+ ~StringSetArgOptions();
+
+ /**
+ * Adds another (option, description) pair.
+ */
+ StringSetArgOptions & operator() (const std::string, const std::string);
+ };
+
///\name Basic operations
///\{
StringSetArg(ArgsGroup * const, const std::string & long_name,
- const char short_name, const std::string & description);
+ const char short_name, const std::string & description,
+ const StringSetArgOptions & options = StringSetArgOptions());
~StringSetArg();
@@ -208,6 +247,18 @@ namespace paludis
* Add an argument to the set.
*/
void add_argument(const std::string & arg);
+
+ ///\name Iterate over our allowed arguments and associated descriptions
+ ///\{
+
+ typedef libwrapiter::ForwardIterator<StringSetArg,
+ const std::pair<std::string, std::string> > AllowedArgIterator;
+
+ AllowedArgIterator begin_allowed_args() const;
+
+ AllowedArgIterator end_allowed_args() const;
+
+ ///\}
};