summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-10-31 03:34:32 -0400
committerJohn Wiegley <johnw@newartisans.com>2009-10-31 04:07:33 -0400
commit4427016b1bc20fac4c1700e4f1577f6c7f9ab78c (patch)
tree26ad49d11ee95667aea74e3513b168c864a569c0
parent77c9d7b1ff9fc0033e653453e08d042b2e95692b (diff)
downloadfork-ledger-4427016b1bc20fac4c1700e4f1577f6c7f9ab78c.tar.gz
fork-ledger-4427016b1bc20fac4c1700e4f1577f6c7f9ab78c.tar.bz2
fork-ledger-4427016b1bc20fac4c1700e4f1577f6c7f9ab78c.zip
Improved arg checking for several valexpr functions
-rw-r--r--src/item.cc27
-rw-r--r--src/option.h15
-rw-r--r--src/report.cc8
3 files changed, 39 insertions, 11 deletions
diff --git a/src/item.cc b/src/item.cc
index 631423a9..debda7a3 100644
--- a/src/item.cc
+++ b/src/item.cc
@@ -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;
}
diff --git a/src/option.h b/src/option.h
index 83710a1c..0600779c 100644
--- a/src/option.h
+++ b/src/option.h
@@ -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());
}
diff --git a/src/report.cc b/src/report.cc
index 565597c6..096536c9 100644
--- a/src/report.cc
+++ b/src/report.cc
@@ -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)) {