aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-01-27 21:39:01 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-01-27 21:39:01 +0000
commit5d7548d4e3cb010a04c6a869777b42cf736944aa (patch)
tree601df429d8820b9c88021b36ec62b0be8796ce73
parent19ec566d84215f4b060c946a674f43de8e49b81f (diff)
downloadpaludis-5d7548d4e3cb010a04c6a869777b42cf736944aa.tar.gz
paludis-5d7548d4e3cb010a04c6a869777b42cf736944aa.tar.xz
Enforce validation for command line arguments.
-rw-r--r--Makefile.am1
-rw-r--r--paludis/Makefile.am.m48
-rw-r--r--paludis/args/args_option.cc46
-rw-r--r--paludis/args/args_option.hh18
-rw-r--r--src/clients/adjutrix/Makefile.am1
-rw-r--r--src/clients/adjutrix/command_line.cc7
-rw-r--r--src/clients/paludis/Makefile.am1
-rw-r--r--src/clients/paludis/command_line.cc12
-rw-r--r--src/clients/qualudis/Makefile.am1
9 files changed, 84 insertions, 11 deletions
diff --git a/Makefile.am b/Makefile.am
index 96ab9bb..5dc8de3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -19,6 +19,7 @@ BUILT_SOURCES = automake-deps-built-hack.tmp
automake-deps-dist-hack.tmp : built-sources-subdirs
$(MAKE) -C paludis/util
$(MAKE) -C paludis/args
+ $(MAKE) -C paludis/ libpaludismanpagethings.la
$(MAKE) -C paludis/qa message-sr.hh
$(MAKE) -C src/output liboutput.a
$(MAKE) -C src/common_args libcommonargs.a
diff --git a/paludis/Makefile.am.m4 b/paludis/Makefile.am.m4
index 1c57b7a..073949e 100644
--- a/paludis/Makefile.am.m4
+++ b/paludis/Makefile.am.m4
@@ -70,11 +70,16 @@ BUILT_SOURCES = srcleanlist
libpaludis_la_SOURCES = filelist
libpaludis_la_LDFLAGS = -version-info @VERSION_LIB_CURRENT@:@VERSION_LIB_REVISION@:0
+libpaludismanpagethings_la_SOURCES = name.cc
+
if ! MONOLITHIC
libpaludis_la_LIBADD = \
$(top_builddir)/paludis/util/libpaludisutil.la
+libpaludismanpagethings_la_LIBADD = \
+ $(top_builddir)/paludis/util/libpaludisutil.la
+
endif
TESTS = testlist
@@ -91,11 +96,12 @@ check_SCRIPTS = testscriptlist
if MONOLITHIC
-noinst_LTLIBRARIES = libpaludis.la
+noinst_LTLIBRARIES = libpaludis.la libpaludismanpagethings.la
else
lib_LTLIBRARIES = libpaludis.la
+noinst_LTLIBRARIES = libpaludismanpagethings.la
endif
diff --git a/paludis/args/args_option.cc b/paludis/args/args_option.cc
index 2252806..68de31c 100644
--- a/paludis/args/args_option.cc
+++ b/paludis/args/args_option.cc
@@ -92,7 +92,16 @@ AliasArg::AliasArg(ArgsOption * const o, const std::string & our_long_name) :
StringArg::StringArg(ArgsGroup * const g, const std::string & our_long_name,
const char our_short_name, const std::string & our_description) :
- ArgsOption(g, our_long_name, our_short_name, our_description)
+ ArgsOption(g, our_long_name, our_short_name, our_description),
+ _validator(0)
+{
+}
+
+StringArg::StringArg(ArgsGroup * const g, const std::string & our_long_name,
+ const char our_short_name, const std::string & our_description,
+ void (* v) (const std::string &)) :
+ ArgsOption(g, our_long_name, our_short_name, our_description),
+ _validator(v)
{
}
@@ -128,7 +137,19 @@ StringSetArg::StringSetArg(ArgsGroup * const g, const std::string & our_long_nam
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>)
+ PrivateImplementationPattern<StringSetArg>(new Implementation<StringSetArg>),
+ _validator(0)
+{
+ std::copy(opts._imp->options.begin(), opts._imp->options.end(),
+ std::back_inserter(_imp->allowed_args));
+}
+
+StringSetArg::StringSetArg(ArgsGroup * const g, const std::string & our_long_name,
+ const char our_short_name, const std::string & our_description,
+ const StringSetArgOptions & opts, void (* v) (const std::string &)) :
+ ArgsOption(g, our_long_name, our_short_name, our_description),
+ PrivateImplementationPattern<StringSetArg>(new Implementation<StringSetArg>),
+ _validator(v)
{
std::copy(opts._imp->options.begin(), opts._imp->options.end(),
std::back_inserter(_imp->allowed_args));
@@ -149,11 +170,16 @@ StringSetArg::end_args() const
void
StringSetArg::add_argument(const std::string & arg)
{
+ Context context("When handling argument '" + arg + "' for '--" + long_name() + "':");
+
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));
+ if (_validator)
+ (*_validator)(arg);
+
_imp->args.insert(arg);
}
@@ -219,8 +245,11 @@ StringSetArg::StringSetArgOptions::StringSetArgOptions() :
{
}
-void EnumArg::set_argument(const std::string & arg)
+void
+EnumArg::set_argument(const std::string & arg)
{
+ Context context("When handling argument '" + arg + "' for '--" + long_name() + "':");
+
if (_imp->allowed_args.end() == std::find_if(_imp->allowed_args.begin(),
_imp->allowed_args.end(), ArgIs(arg)))
throw (BadValue("--" + long_name(), arg));
@@ -228,6 +257,17 @@ void EnumArg::set_argument(const std::string & arg)
_argument = arg;
}
+void
+StringArg::set_argument(const std::string & arg)
+{
+ Context context("When handling argument '" + arg + "' for '--" + long_name() + "':");
+
+ if (_validator)
+ (*_validator)(arg);
+
+ _argument = arg;
+}
+
EnumArg::~EnumArg()
{
}
diff --git a/paludis/args/args_option.hh b/paludis/args/args_option.hh
index c12165b..c152a4b 100644
--- a/paludis/args/args_option.hh
+++ b/paludis/args/args_option.hh
@@ -152,6 +152,7 @@ namespace paludis
{
private:
std::string _argument;
+ void (* _validator) (const std::string &);
public:
/**
@@ -161,6 +162,13 @@ namespace paludis
const char short_name, const std::string & description);
/**
+ * Constructor with validator.
+ */
+ StringArg(ArgsGroup * const, const std::string & long_name,
+ const char short_name, const std::string & description,
+ void (* validator) (const std::string &));
+
+ /**
* Fetch the argument that was given to this option.
*/
const std::string& argument() const { return _argument; }
@@ -168,7 +176,7 @@ namespace paludis
/**
* Set the argument returned by argument().
*/
- void set_argument(const std::string& arg) { _argument = arg; }
+ void set_argument(const std::string& arg);
};
/**
@@ -182,6 +190,9 @@ namespace paludis
public Visitable<StringSetArg, ArgsVisitorTypes>,
private PrivateImplementationPattern<StringSetArg>
{
+ private:
+ void (* _validator) (const std::string &);
+
public:
/**
* Helper class for passing available options and associated descriptions
@@ -228,6 +239,11 @@ namespace paludis
const char short_name, const std::string & description,
const StringSetArgOptions & options = StringSetArgOptions());
+ StringSetArg(ArgsGroup * const, const std::string & long_name,
+ const char short_name, const std::string & description,
+ const StringSetArgOptions & options,
+ void (* validator) (const std::string &));
+
~StringSetArg();
///\}
diff --git a/src/clients/adjutrix/Makefile.am b/src/clients/adjutrix/Makefile.am
index 1bae52e..1380fc6 100644
--- a/src/clients/adjutrix/Makefile.am
+++ b/src/clients/adjutrix/Makefile.am
@@ -27,6 +27,7 @@ man_adjutrix_SOURCES = \
man_adjutrix_LDADD = \
$(top_builddir)/paludis/args/libpaludisargs.la \
$(top_builddir)/paludis/util/libpaludisutil.la \
+ $(top_builddir)/paludis/libpaludismanpagethings.la \
$(top_builddir)/src/output/liboutput.a \
$(top_builddir)/src/common_args/libcommonargs.a \
$(DYNAMIC_LD_LIBS)
diff --git a/src/clients/adjutrix/command_line.cc b/src/clients/adjutrix/command_line.cc
index b5f05e6..e7c2fac 100644
--- a/src/clients/adjutrix/command_line.cc
+++ b/src/clients/adjutrix/command_line.cc
@@ -18,6 +18,7 @@
*/
#include "command_line.hh"
+#include <paludis/name.hh>
CommandLine::CommandLine() :
ArgsHandler(),
@@ -72,9 +73,11 @@ CommandLine::CommandLine() :
tree_args(this, "Tree action options",
"Options which are relevant for tree actions."),
a_category(&tree_args, "category", 'C',
- "Matches with this category name only (may be specified multiple times)"),
+ "Matches with this category name only (may be specified multiple times)",
+ paludis::args::StringSetArg::StringSetArgOptions(), &paludis::CategoryNamePartValidator::validate),
a_package(&tree_args, "package", 'P',
- "Matches with this package name only (may be specified multiple times)"),
+ "Matches with this package name only (may be specified multiple times)",
+ paludis::args::StringSetArg::StringSetArgOptions(), &paludis::PackageNamePartValidator::validate),
profile_args(this, "Profile action options",
"Options which are relevant for profile actions."),
diff --git a/src/clients/paludis/Makefile.am b/src/clients/paludis/Makefile.am
index f0338e8..f1017c0 100644
--- a/src/clients/paludis/Makefile.am
+++ b/src/clients/paludis/Makefile.am
@@ -26,6 +26,7 @@ man_paludis_SOURCES = \
man_paludis_LDADD = \
$(top_builddir)/paludis/args/libpaludisargs.la \
$(top_builddir)/paludis/util/libpaludisutil.la \
+ $(top_builddir)/paludis/libpaludismanpagethings.la \
$(top_builddir)/src/common_args/libcommonargs.a \
$(top_builddir)/src/output/liboutput.a \
$(DYNAMIC_LD_LIBS)
diff --git a/src/clients/paludis/command_line.cc b/src/clients/paludis/command_line.cc
index 670ec9c..f71dd83 100644
--- a/src/clients/paludis/command_line.cc
+++ b/src/clients/paludis/command_line.cc
@@ -202,11 +202,15 @@ CommandLine::CommandLine() :
list_args(this, "List options",
"Options relevant for one or more of the --list actions."),
- a_repository(&list_args, "repository", '\0', "Matches with this repository name only"),
+ a_repository(&list_args, "repository", '\0', "Matches with this repository name only",
+ paludis::args::StringSetArg::StringSetArgOptions(), &paludis::RepositoryNameValidator::validate),
a_repository_format(&list_args, "repository-format", '\0', "Matches with this repository format only"),
- a_category(&list_args, "category", '\0', "Matches with this category name only"),
- a_package(&list_args, "package", '\0', "Matches with this package name only"),
- a_set(&list_args, "set", '\0', "Matches with this package set name only"),
+ a_category(&list_args, "category", '\0', "Matches with this category name only",
+ paludis::args::StringSetArg::StringSetArgOptions(), &paludis::CategoryNamePartValidator::validate),
+ a_package(&list_args, "package", '\0', "Matches with this package name only",
+ paludis::args::StringSetArg::StringSetArgOptions(), &paludis::PackageNamePartValidator::validate),
+ a_set(&list_args, "set", '\0', "Matches with this package set name only",
+ paludis::args::StringSetArg::StringSetArgOptions(), &paludis::SetNameValidator::validate),
owner_args(this, "Owner options",
"Options relevant for the --owner actions."),
diff --git a/src/clients/qualudis/Makefile.am b/src/clients/qualudis/Makefile.am
index 6cce4c0..7cfa30f 100644
--- a/src/clients/qualudis/Makefile.am
+++ b/src/clients/qualudis/Makefile.am
@@ -78,6 +78,7 @@ man_qualudis_SOURCES = \
man_qualudis_LDADD = \
$(top_builddir)/paludis/args/libpaludisargs.la \
$(top_builddir)/paludis/util/libpaludisutil.la \
+ $(top_builddir)/paludis/libpaludismanpagethings.la \
$(top_builddir)/src/output/liboutput.a \
$(top_builddir)/src/common_args/libcommonargs.a \
$(DYNAMIC_LD_LIBS)