aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Stephen P. Bennett <spb@exherbo.org> 2006-02-18 03:34:25 +0000
committerAvatar Stephen P. Bennett <spb@exherbo.org> 2006-02-18 03:34:25 +0000
commitbc764f5b47f5c6bd8580db7098f2f347acd91afb (patch)
tree6e6ccf62046673ad41c76843797844b67579ed2b
parent174e8f6c0cc10f488ea24b4998e1f41c978be315 (diff)
downloadpaludis-bc764f5b47f5c6bd8580db7098f2f347acd91afb.tar.gz
paludis-bc764f5b47f5c6bd8580db7098f2f347acd91afb.tar.xz
Fix handling when an option requiring a parameter is specified, but the parameter is not. Also make the IntegerArg handling use destringify.
-rw-r--r--paludis/args/args_TEST.cc13
-rw-r--r--paludis/args/args_handler.cc2
-rw-r--r--paludis/args/args_visitor.cc39
-rw-r--r--paludis/args/args_visitor.hh6
4 files changed, 44 insertions, 16 deletions
diff --git a/paludis/args/args_TEST.cc b/paludis/args/args_TEST.cc
index 789659b..79b9783 100644
--- a/paludis/args/args_TEST.cc
+++ b/paludis/args/args_TEST.cc
@@ -18,6 +18,7 @@
*/
#include <paludis/args/args.hh>
+#include <paludis/args/bad_value.hh>
#include <test/test_framework.hh>
#include <test/test_runner.hh>
@@ -107,5 +108,17 @@ namespace test_cases
TEST_CHECK_EQUAL(*++(++(c1.begin_parameters())), "two");
}
} test_args_simple;
+
+ struct ArgsTestNoParam : TestCase
+ {
+ ArgsTestNoParam() : TestCase("Missing parameters") { }
+
+ void run()
+ {
+ char *args[] = { "program-name", "-e" };
+ CommandLine c1;
+ TEST_CHECK_THROWS(c1.run(2, args), BadValue);
+ }
+ } test_args_no_param;
}
diff --git a/paludis/args/args_handler.cc b/paludis/args/args_handler.cc
index 37a8887..7aa1f2b 100644
--- a/paludis/args/args_handler.cc
+++ b/paludis/args/args_handler.cc
@@ -61,7 +61,7 @@ ArgsHandler::run(const int argc, const char * const * const argv)
std::list<std::string>::iterator argit = args.begin(), arge = args.end();
- ArgsVisitor visitor(&argit);
+ ArgsVisitor visitor(&argit, arge);
for ( ; argit != arge; ++argit )
{
diff --git a/paludis/args/args_visitor.cc b/paludis/args/args_visitor.cc
index b66b457..9cfee95 100644
--- a/paludis/args/args_visitor.cc
+++ b/paludis/args/args_visitor.cc
@@ -1,6 +1,4 @@
-#include "args_visitor.hh"
-#include "args_option.hh"
-
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
* Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@gentoo.org>
*
@@ -18,15 +16,29 @@
* Place, Suite 330, Boston, MA 02111-1307 USA
*/
+#include "args_visitor.hh"
+#include "args_option.hh"
#include "bad_value.hh"
+#include <paludis/destringify.hh>
+
#include <sstream>
using namespace paludis;
using namespace args;
-ArgsVisitor::ArgsVisitor(std::list<std::string>::iterator *ai) : _args_index(ai)
+ArgsVisitor::ArgsVisitor(std::list<std::string>::iterator *ai,
+ std::list<std::string>::iterator ae) : _args_index(ai), _args_end(ae)
+{
+}
+
+const std::string& ArgsVisitor::get_param(const ArgsOption * const arg)
{
+ if (++(*_args_index) == _args_end)
+ {
+ throw BadValue("--" + arg->long_name(), "<none>");
+ }
+ return **_args_index;
}
void ArgsVisitor::visit(ArgsOption * const arg)
@@ -37,7 +49,7 @@ void ArgsVisitor::visit(ArgsOption * const arg)
void ArgsVisitor::visit(StringArg * const arg)
{
visit(static_cast<ArgsOption *>(arg));
- arg->set_argument(*++(*_args_index));
+ arg->set_argument(get_param(arg));
}
void ArgsVisitor::visit(AliasArg * const arg)
@@ -53,18 +65,19 @@ void ArgsVisitor::visit(SwitchArg * const arg)
void ArgsVisitor::visit(IntegerArg * const arg)
{
visit(static_cast<ArgsOption*>(arg));
- std::stringstream ss;
- std::string param = *++(*_args_index);
- ss << param;
- int i;
- ss >> i;
- if (!ss.eof() || ss.bad())
+ std::string param = get_param(arg);
+ try
+ {
+ arg->set_argument(destringify<int>(param));
+ }
+ catch(DestringifyError &e)
+ {
throw BadValue("--" + arg->long_name(), param);
- arg->set_argument(i);
+ }
}
void ArgsVisitor::visit(EnumArg * const arg)
{
visit(static_cast<ArgsOption*>(arg));
- arg->set_argument(*++(*_args_index));
+ arg->set_argument(get_param(arg));
}
diff --git a/paludis/args/args_visitor.hh b/paludis/args/args_visitor.hh
index a42cf5a..823ce96 100644
--- a/paludis/args/args_visitor.hh
+++ b/paludis/args/args_visitor.hh
@@ -54,13 +54,15 @@ namespace paludis
class ArgsVisitor : public ArgsVisitorTypes::Visitor
{
private:
- std::list<std::string>::iterator *_args_index;
+ std::list<std::string>::iterator *_args_index, _args_end;
+
+ const std::string& get_param(const ArgsOption * const);
public:
/**
* Constructor
*/
- ArgsVisitor(std::list<std::string>::iterator *);
+ ArgsVisitor(std::list<std::string>::iterator *, std::list<std::string>::iterator);
/// Visit an ArgsOption.
void visit(ArgsOption * const);