Improved arg checking for several valexpr functions

This commit is contained in:
John Wiegley 2009-10-31 03:34:32 -04:00
parent 77c9d7b1ff
commit 4427016b1b
3 changed files with 39 additions and 11 deletions

View file

@ -32,6 +32,7 @@
#include <system.hh>
#include "item.h"
#include "interactive.h"
namespace ledger {
@ -210,15 +211,31 @@ namespace {
return item.has_tag(args[0].as_string());
else if (args[0].is_mask())
return item.has_tag(args[0].as_mask());
} else {
return item.has_tag(args[0].to_mask(), args[1].to_mask());
else
throw_(std::logic_error,
_("Expected string for argument 1, but received %1")
<< args[0].label());
}
else if (args.size() == 2) {
if (args[0].is_mask() && args[1].is_mask())
return item.has_tag(args[0].to_mask(), args[1].to_mask());
else
throw_(std::logic_error,
_("Expected masks for arguments 1 and 2, but received %1 and %2")
<< args[0].label() << args[1].label());
}
else if (args.size() == 0) {
throw_(std::logic_error, _("Too few arguments to function"));
}
else {
throw_(std::logic_error, _("Too many arguments to function"));
}
return false;
}
value_t get_tag(call_scope_t& args) {
item_t& item(find_scope<item_t>(args));
if (optional<string> value = item.get_tag(args[0].as_string()))
value_t get_tag(call_scope_t& scope) {
in_context_t<item_t> env(scope, "s");
if (optional<string> value = env->get_tag(env.get<string>(0)))
return string_value(*value);
return false;
}

View file

@ -162,10 +162,21 @@ public:
virtual void handler(call_scope_t& args) {
if (wants_arg) {
if (args.empty() || args.size() == 1)
if (args.size() < 2)
throw_(std::runtime_error, _("No argument provided for %1") << desc());
else if (args.size() > 2)
throw_(std::runtime_error, _("To many arguments provided for %1") << desc());
else if (! args[0].is_string())
throw_(std::runtime_error, _("Context argument for %1 not a string") << desc());
on_with(args[0].as_string(), args[1]);
} else {
}
else if (args.size() < 1) {
throw_(std::runtime_error, _("No argument provided for %1") << desc());
}
else if (! args[0].is_string()) {
throw_(std::runtime_error, _("Context argument for %1 not a string") << desc());
}
else {
on_only(args[0].as_string());
}

View file

@ -235,12 +235,13 @@ value_t report_t::fn_justify(call_scope_t& scope)
return string_value(out.str());
}
value_t report_t::fn_quoted(call_scope_t& args)
value_t report_t::fn_quoted(call_scope_t& scope)
{
interactive_t args(scope, "s");
std::ostringstream out;
out << '"';
foreach (const char ch, args[0].to_string()) {
foreach (const char ch, args.get<string>(0)) {
if (ch == '"')
out << "\\\"";
else
@ -253,8 +254,7 @@ value_t report_t::fn_quoted(call_scope_t& args)
value_t report_t::fn_join(call_scope_t& scope)
{
interactive_t args(scope, "s");
interactive_t args(scope, "s");
std::ostringstream out;
foreach (const char ch, args.get<string>(0)) {