blob: f4cd0a95de846e4d54b69a1b31664700dc3c4072 [file] [log] [blame]
Václav Kubernát24df80e2018-06-06 15:18:03 +02001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
3 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
4 *
5 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
6 *
7*/
8#pragma once
9
Václav Kubernát57272422019-02-08 12:48:24 +010010#include <boost/mpl/vector.hpp>
Václav Kubernát24df80e2018-06-06 15:18:03 +020011#include "ast_path.hpp"
Václav Kubernát627f6152018-08-29 13:23:56 +020012#include "ast_values.hpp"
Václav Kubernát24df80e2018-06-06 15:18:03 +020013
14namespace x3 = boost::spirit::x3;
15namespace ascii = boost::spirit::x3::ascii;
16
17using ascii::space;
18using x3::_attr;
19using x3::alnum;
20using x3::alpha;
21using x3::char_;
Václav Kubernátebca2552018-06-08 19:06:02 +020022using x3::double_;
Václav Kubernát24df80e2018-06-06 15:18:03 +020023using x3::expect;
Václav Kubernátebca2552018-06-08 19:06:02 +020024using x3::int_;
Václav Kubernát24df80e2018-06-06 15:18:03 +020025using x3::lexeme;
26using x3::lit;
Václav Kubernátebca2552018-06-08 19:06:02 +020027using x3::uint_;
Václav Kubernát24df80e2018-06-06 15:18:03 +020028
29struct parser_context_tag;
30
31using keyValue_ = std::pair<std::string, std::string>;
32
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020033enum class LsOption {
34 Recursive
35};
36
Václav Kubernát6d791432018-10-25 16:00:35 +020037struct discard_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +010038 static constexpr auto name = "discard";
Václav Kubernát054cc992019-02-21 14:23:52 +010039 static constexpr auto shortHelp = "discard - Discard current changes.";
40 static constexpr auto longHelp = R"(
41 discard
42
43 Discards current changes. Accepts no arguments.
44
45 Usage:
46 /> discard)";
Václav Kubernát6d791432018-10-25 16:00:35 +020047 bool operator==(const discard_& b) const;
48};
49
Václav Kubernát11afac72018-07-18 14:59:53 +020050struct ls_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +010051 static constexpr auto name = "ls";
Václav Kubernát054cc992019-02-21 14:23:52 +010052 static constexpr auto shortHelp = "ls - List available nodes.";
53 static constexpr auto longHelp = R"(
54 ls [--recursive] [path]
55
56 Lists available nodes in the current directory. Optionally
57 accepts a path argument. Accepts both schema paths and data
58 paths. Path starting with a forward slash means an absolute
59 path.
60
61 Usage:
62 /> ls
63 /> ls --recursive module:node
64 /> ls /module:node)";
Václav Kubernát11afac72018-07-18 14:59:53 +020065 bool operator==(const ls_& b) const;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020066 std::vector<LsOption> m_options;
Václav Kubernát5c75b252018-10-10 18:33:47 +020067 boost::optional<boost::variant<dataPath_, schemaPath_>> m_path;
Václav Kubernát11afac72018-07-18 14:59:53 +020068};
69
Václav Kubernát24df80e2018-06-06 15:18:03 +020070struct cd_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +010071 static constexpr auto name = "cd";
Václav Kubernát054cc992019-02-21 14:23:52 +010072 static constexpr auto shortHelp = "cd - Enter a different node.";
73 static constexpr auto longHelp = R"(
74 cd path
75
76 Enters a node specified by path. Only accepts data paths.
77
78 Usage:
79 /> cd /module:node/node2
80 /> cd ..)";
Václav Kubernát24df80e2018-06-06 15:18:03 +020081 bool operator==(const cd_& b) const;
Václav Kubernát2eaceb82018-10-08 19:56:30 +020082 dataPath_ m_path;
Václav Kubernát24df80e2018-06-06 15:18:03 +020083};
84
85struct create_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +010086 static constexpr auto name = "create";
Václav Kubernát054cc992019-02-21 14:23:52 +010087 static constexpr auto shortHelp = "create - Create a presence container.";
88 static constexpr auto longHelp = R"(
Václav Kubernátf5f64f02019-03-19 17:15:47 +010089 create path
Václav Kubernát054cc992019-02-21 14:23:52 +010090
Václav Kubernátf5f64f02019-03-19 17:15:47 +010091 Creates a presence container or a list instance specified by path.
Václav Kubernát054cc992019-02-21 14:23:52 +010092
93 Usage:
Václav Kubernátf5f64f02019-03-19 17:15:47 +010094 /> create /module:pContainer
95 /> create /module:list[key=value][anotherKey=value])";
Václav Kubernát24df80e2018-06-06 15:18:03 +020096 bool operator==(const create_& b) const;
Václav Kubernát2eaceb82018-10-08 19:56:30 +020097 dataPath_ m_path;
Václav Kubernát24df80e2018-06-06 15:18:03 +020098};
99
100struct delete_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +0100101 static constexpr auto name = "delete";
Václav Kubernát054cc992019-02-21 14:23:52 +0100102 static constexpr auto shortHelp = "delete - Delete a presence container.";
103 static constexpr auto longHelp = R"(
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100104 delete path
Václav Kubernát054cc992019-02-21 14:23:52 +0100105
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100106 Deletes a presence container or a list instance specified by path.
Václav Kubernát054cc992019-02-21 14:23:52 +0100107
108 Usage:
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100109 /> delete /module:pContainer
110 /> delete /module:list[key=value][anotherKey=value])";
Václav Kubernát24df80e2018-06-06 15:18:03 +0200111 bool operator==(const delete_& b) const;
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200112 dataPath_ m_path;
Václav Kubernát24df80e2018-06-06 15:18:03 +0200113};
114
115struct set_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +0100116 static constexpr auto name = "set";
Václav Kubernát054cc992019-02-21 14:23:52 +0100117 static constexpr auto shortHelp = "set - Change value of a leaf.";
118 static constexpr auto longHelp = R"(
119 set path_to_leaf value
120
121 Changes the leaf specified by path to value.
122
123 Usage:
124 /> set /module:leaf 123
125 /> set /module:leaf abc)";
Václav Kubernát24df80e2018-06-06 15:18:03 +0200126 bool operator==(const set_& b) const;
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200127 dataPath_ m_path;
Václav Kubernátebca2552018-06-08 19:06:02 +0200128 leaf_data_ m_data;
Václav Kubernát24df80e2018-06-06 15:18:03 +0200129};
130
Václav Kubernát812ee282018-08-30 17:10:03 +0200131struct commit_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +0100132 static constexpr auto name = "commit";
Václav Kubernát054cc992019-02-21 14:23:52 +0100133 static constexpr auto shortHelp = "commit - Commit current changes.";
134 static constexpr auto longHelp = R"(
135 commit
136
137 Commits the current changes. Accepts no arguments.
138
139 Usage:
140 /> commit)";
Václav Kubernát812ee282018-08-30 17:10:03 +0200141 bool operator==(const set_& b) const;
142};
143
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200144struct get_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +0100145 static constexpr auto name = "get";
Václav Kubernát054cc992019-02-21 14:23:52 +0100146 static constexpr auto shortHelp = "get - Retrieve configuration from the server.";
147 static constexpr auto longHelp = R"(
148 get [path]
149
150 Retrieves configuration of the current node. Works recursively.
151 Optionally takes an argument specifying a path, the output will,
152 as if the user was in that node.
153
154 Usage:
155 /> get
156 /> get /module:path)";
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200157 bool operator==(const get_& b) const;
Václav Kubernát5c75b252018-10-10 18:33:47 +0200158 boost::optional<boost::variant<dataPath_, schemaPath_>> m_path;
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200159};
Václav Kubernát812ee282018-08-30 17:10:03 +0200160
Václav Kubernát054cc992019-02-21 14:23:52 +0100161struct help_;
162using CommandTypes = boost::mpl::vector<discard_, ls_, cd_, create_, delete_, set_, commit_, get_, help_>;
163struct help_ : x3::position_tagged {
164 static constexpr auto name = "help";
165 static constexpr auto shortHelp = "help - Print help for commands.";
166 static constexpr auto longHelp = R"(
167 help [command_name]
168
169 Print help for command_name. If used without an argument,
170 print short help for all commands.
171
172 Usage:
173 /> help
174 /> help cd
175 /> help help)";
176 bool operator==(const help_& b) const;
177
178 // The help command has got one optional argument – a command name (type).
179 // All commands are saved in CommandTypes, so we could just use that, but
180 // that way, Spirit would be default constructing the command structs,
181 // which is undesirable, so firstly we use mpl::transform to wrap
182 // CommandTypes with boost::type:
183 using WrappedCommandTypes = boost::mpl::transform<CommandTypes, boost::type<boost::mpl::_>>::type;
184 // Next, we create a variant over the wrapped types:
185 using CommandTypesVariant = boost::make_variant_over<WrappedCommandTypes>::type;
186 // Finally, we wrap the variant with boost::optional:
187 boost::optional<CommandTypesVariant> m_cmd;
188};
189
Václav Kubernát57272422019-02-08 12:48:24 +0100190// TODO: The usage of MPL won't be necessary after std::variant support is added to Spirit
191// https://github.com/boostorg/spirit/issues/270
Václav Kubernát57272422019-02-08 12:48:24 +0100192using command_ = boost::make_variant_over<CommandTypes>::type;
Václav Kubernát24df80e2018-06-06 15:18:03 +0200193
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200194BOOST_FUSION_ADAPT_STRUCT(ls_, m_options, m_path)
Václav Kubernát24df80e2018-06-06 15:18:03 +0200195BOOST_FUSION_ADAPT_STRUCT(cd_, m_path)
196BOOST_FUSION_ADAPT_STRUCT(create_, m_path)
197BOOST_FUSION_ADAPT_STRUCT(delete_, m_path)
Václav Kubernát24df80e2018-06-06 15:18:03 +0200198BOOST_FUSION_ADAPT_STRUCT(set_, m_path, m_data)
Václav Kubernátab538992019-03-06 15:30:50 +0100199BOOST_FUSION_ADAPT_STRUCT(enum_, m_value)
200BOOST_FUSION_ADAPT_STRUCT(binary_, m_value)
Václav Kubernáteeb38842019-03-20 19:46:05 +0100201BOOST_FUSION_ADAPT_STRUCT(identityRef_, m_prefix, m_value)
Václav Kubernát812ee282018-08-30 17:10:03 +0200202BOOST_FUSION_ADAPT_STRUCT(commit_)
Václav Kubernát054cc992019-02-21 14:23:52 +0100203BOOST_FUSION_ADAPT_STRUCT(help_, m_cmd)
Václav Kubernát6d791432018-10-25 16:00:35 +0200204BOOST_FUSION_ADAPT_STRUCT(discard_)
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200205BOOST_FUSION_ADAPT_STRUCT(get_, m_path)