Rework dump command parsing
Includes better error message and tab completion.
Issue: https://tree.taiga.io/project/jktjkt-netconf-cli/issue/182
Issue: https://tree.taiga.io/project/jktjkt-netconf-cli/issue/183
Change-Id: I6d91870617f99423cc25926522bd1408587c2459
diff --git a/src/grammars.hpp b/src/grammars.hpp
index 8d18340..478e49f 100644
--- a/src/grammars.hpp
+++ b/src/grammars.hpp
@@ -239,9 +239,24 @@
}
} const format_table;
+struct dump_args : x3::parser<dump_args> {
+ using attribute_type = dump_;
+ template <typename It, typename Ctx, typename RCtx>
+ bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, dump_& attr) const
+ {
+ ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
+ parserContext.m_suggestions = {{"xml"}, {"json"}};
+ parserContext.m_completionIterator = begin;
+ auto res = format_table.parse(begin, end, ctx, rctx, attr);
+ if (!res) {
+ parserContext.m_errorMsg = "Expected a data format (xml, json) here:";
+ }
+ return res;
+ }
+} const dump_args;
auto const dump_def =
- dump_::name >> space_separator >> format_table;
+ dump_::name > space_separator >> dump_args;
auto const createCommandSuggestions_def =
x3::eps;
diff --git a/tests/command_completion.cpp b/tests/command_completion.cpp
index 06cedd7..cf6b188 100644
--- a/tests/command_completion.cpp
+++ b/tests/command_completion.cpp
@@ -73,5 +73,12 @@
expectedContextLength = 0;
}
+ SECTION("dump")
+ {
+ input = "dump ";
+ expectedCompletions = {"xml", "json"};
+ expectedContextLength = 0;
+ }
+
REQUIRE(parser.completeCommand(input, errorStream) == (Completions{expectedCompletions, expectedContextLength}));
}