Improved arg checking for several valexpr functions
This commit is contained in:
parent
77c9d7b1ff
commit
4427016b1b
3 changed files with 39 additions and 11 deletions
27
src/item.cc
27
src/item.cc
|
|
@ -32,6 +32,7 @@
|
||||||
#include <system.hh>
|
#include <system.hh>
|
||||||
|
|
||||||
#include "item.h"
|
#include "item.h"
|
||||||
|
#include "interactive.h"
|
||||||
|
|
||||||
namespace ledger {
|
namespace ledger {
|
||||||
|
|
||||||
|
|
@ -210,15 +211,31 @@ namespace {
|
||||||
return item.has_tag(args[0].as_string());
|
return item.has_tag(args[0].as_string());
|
||||||
else if (args[0].is_mask())
|
else if (args[0].is_mask())
|
||||||
return item.has_tag(args[0].as_mask());
|
return item.has_tag(args[0].as_mask());
|
||||||
} else {
|
else
|
||||||
return item.has_tag(args[0].to_mask(), args[1].to_mask());
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
value_t get_tag(call_scope_t& args) {
|
value_t get_tag(call_scope_t& scope) {
|
||||||
item_t& item(find_scope<item_t>(args));
|
in_context_t<item_t> env(scope, "s");
|
||||||
if (optional<string> value = item.get_tag(args[0].as_string()))
|
if (optional<string> value = env->get_tag(env.get<string>(0)))
|
||||||
return string_value(*value);
|
return string_value(*value);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
15
src/option.h
15
src/option.h
|
|
@ -162,10 +162,21 @@ public:
|
||||||
|
|
||||||
virtual void handler(call_scope_t& args) {
|
virtual void handler(call_scope_t& args) {
|
||||||
if (wants_arg) {
|
if (wants_arg) {
|
||||||
if (args.empty() || args.size() == 1)
|
if (args.size() < 2)
|
||||||
throw_(std::runtime_error, _("No argument provided for %1") << desc());
|
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]);
|
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());
|
on_only(args[0].as_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -235,12 +235,13 @@ value_t report_t::fn_justify(call_scope_t& scope)
|
||||||
return string_value(out.str());
|
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;
|
std::ostringstream out;
|
||||||
|
|
||||||
out << '"';
|
out << '"';
|
||||||
foreach (const char ch, args[0].to_string()) {
|
foreach (const char ch, args.get<string>(0)) {
|
||||||
if (ch == '"')
|
if (ch == '"')
|
||||||
out << "\\\"";
|
out << "\\\"";
|
||||||
else
|
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)
|
value_t report_t::fn_join(call_scope_t& scope)
|
||||||
{
|
{
|
||||||
interactive_t args(scope, "s");
|
interactive_t args(scope, "s");
|
||||||
|
|
||||||
std::ostringstream out;
|
std::ostringstream out;
|
||||||
|
|
||||||
foreach (const char ch, args.get<string>(0)) {
|
foreach (const char ch, args.get<string>(0)) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue