diff --git a/lib/utils/mu-command-handler.cc b/lib/utils/mu-command-handler.cc index 610d489f..927df0b5 100644 --- a/lib/utils/mu-command-handler.cc +++ b/lib/utils/mu-command-handler.cc @@ -1,5 +1,5 @@ /* -** Copyright (C) 2020-2022 Dirk-Jan C. Binnema +** Copyright (C) 2020-2023 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -148,20 +148,16 @@ using ArgMap = CommandHandler::ArgMap; using ArgInfo = CommandHandler::ArgInfo; using CommandInfo = CommandHandler::CommandInfo; - -static bool +static Result call(const CommandInfoMap& cmap, const std::string& str) try { - const auto cmd{Command::make_parse(str)}; - if (!cmd) - throw Error(Error::Code::Internal, "invalid sexp str"); - - const auto res{CommandHandler(cmap).invoke(*cmd)}; - return !!res; + if (const auto cmd{Command::make_parse(str)}; !cmd) + return Err(Error::Code::Internal, "invalid s-expression '{}'", str); + else + return CommandHandler(cmap).invoke(*cmd); } catch (const Error& err) { - mu_warning("{}", err.what()); - return false; + return Err(Error{err}); } static void @@ -169,18 +165,49 @@ test_command() { allow_warnings(); - CommandInfoMap cmap; - cmap.emplace( + CommandInfoMap ci_map; + ci_map.emplace( "my-command", CommandInfo{ArgMap{{":param1", ArgInfo{Sexp::Type::String, true, "some string"}}, {":param2", ArgInfo{Sexp::Type::Number, false, "some integer"}}}, "My command,", {}}); + ci_map.emplace( + "another-command", + CommandInfo{ + ArgMap{ + {":queries", ArgInfo{Sexp::Type::List, false, + "queries for which to get read/unread numbers"}}, + {":symbol", ArgInfo{Sexp::Type::Symbol, true, + "some boring symbol"}}, + {":bool", ArgInfo{Sexp::Type::Symbol, true, + "some even more boring boolean symbol"}}, + {":symbol2", ArgInfo{Sexp::Type::Symbol, false, + "some even more boring symbol"}}, + {":bool2", ArgInfo{Sexp::Type::Symbol, false, + "some boring boolean symbol"}}, + }, + "get unread/totals information for a list of queries", + [&](const auto& params) { + const auto queries{params.string_vec_arg(":queries") + .value_or(std::vector{})}; + g_assert_cmpuint(queries.size(),==,3); + g_assert_true(params.bool_arg(":bool").value_or(false) == true); + assert_equal(params.symbol_arg(":symbol").value_or("boo"), "sym"); - g_assert_true(call(cmap, "(my-command :param1 \"hello\")")); - g_assert_true(call(cmap, "(my-command :param1 \"hello\" :param2 123)")); + g_assert_false(!!params.bool_arg(":bool2")); + g_assert_false(!!params.bool_arg(":symbol2")); - g_assert_false(call(cmap, "(my-command :param1 \"hello\" :param2 123 :param3 xxx)")); + }}); + + CommandHandler handler(std::move(ci_map)); + const auto cmap{handler.info_map()}; + + assert_valid_result(call(cmap, "(my-command :param1 \"hello\")")); + assert_valid_result(call(cmap, "(my-command :param1 \"hello\" :param2 123)")); + g_assert_false(!!call(cmap, "(my-command :param1 \"hello\" :param2 123 :param3 xxx)")); + assert_valid_result(call(cmap, "(another-command :queries (\"foo\" \"bar\" \"cuux\") " + ":symbol sym :bool true)")); } static void diff --git a/lib/utils/mu-command-handler.hh b/lib/utils/mu-command-handler.hh index e892266a..755af539 100644 --- a/lib/utils/mu-command-handler.hh +++ b/lib/utils/mu-command-handler.hh @@ -1,5 +1,5 @@ /* -** Copyright (C) 2020-2022 Dirk-Jan C. Binnema +** Copyright (C) 2020-2023 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -218,7 +218,7 @@ struct CommandHandler { * first, then alphabetical. * * @return vec with the sorted names. - */ + */ /* LCOV_EXCL_START */ std::vector sorted_argnames() const { // sort args -- by required, then alphabetical. std::vector names; @@ -234,6 +234,7 @@ struct CommandHandler { }); return names; } + /* LCOV_EXCL_STOP */ }; @@ -263,6 +264,7 @@ private: const CommandInfoMap cmap_; }; +/* LCOV_EXCL_START */ static inline std::ostream& operator<<(std::ostream& os, const CommandHandler::ArgInfo& info) { @@ -270,6 +272,7 @@ operator<<(std::ostream& os, const CommandHandler::ArgInfo& info) return os; } +/* LCOV_EXCL_STOP */ static inline std::ostream& operator<<(std::ostream& os, const CommandHandler::CommandInfo& info)