blob: 32dba64d3d32d0217043deed1158311294c55873 [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;
Ivona Oboňová88c78ca2019-07-02 18:40:07 +020024using x3::int8;
25using x3::int16;
26using x3::int32;
27using x3::int64;
Václav Kubernát24df80e2018-06-06 15:18:03 +020028using x3::lexeme;
29using x3::lit;
Ivona Oboňová88c78ca2019-07-02 18:40:07 +020030using x3::uint8;
31using x3::uint16;
32using x3::uint32;
33using x3::uint64;
Václav Kubernát24df80e2018-06-06 15:18:03 +020034
35struct parser_context_tag;
36
37using keyValue_ = std::pair<std::string, std::string>;
38
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020039enum class LsOption {
40 Recursive
41};
42
Václav Kubernát6d791432018-10-25 16:00:35 +020043struct discard_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +010044 static constexpr auto name = "discard";
Václav Kubernát054cc992019-02-21 14:23:52 +010045 static constexpr auto shortHelp = "discard - Discard current changes.";
46 static constexpr auto longHelp = R"(
47 discard
48
49 Discards current changes. Accepts no arguments.
50
51 Usage:
52 /> discard)";
Václav Kubernát6d791432018-10-25 16:00:35 +020053 bool operator==(const discard_& b) const;
54};
55
Václav Kubernát11afac72018-07-18 14:59:53 +020056struct ls_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +010057 static constexpr auto name = "ls";
Václav Kubernát054cc992019-02-21 14:23:52 +010058 static constexpr auto shortHelp = "ls - List available nodes.";
59 static constexpr auto longHelp = R"(
60 ls [--recursive] [path]
61
62 Lists available nodes in the current directory. Optionally
63 accepts a path argument. Accepts both schema paths and data
64 paths. Path starting with a forward slash means an absolute
65 path.
66
67 Usage:
68 /> ls
69 /> ls --recursive module:node
70 /> ls /module:node)";
Václav Kubernát11afac72018-07-18 14:59:53 +020071 bool operator==(const ls_& b) const;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020072 std::vector<LsOption> m_options;
Václav Kubernát5c75b252018-10-10 18:33:47 +020073 boost::optional<boost::variant<dataPath_, schemaPath_>> m_path;
Václav Kubernát11afac72018-07-18 14:59:53 +020074};
75
Václav Kubernát24df80e2018-06-06 15:18:03 +020076struct cd_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +010077 static constexpr auto name = "cd";
Václav Kubernát054cc992019-02-21 14:23:52 +010078 static constexpr auto shortHelp = "cd - Enter a different node.";
79 static constexpr auto longHelp = R"(
80 cd path
81
82 Enters a node specified by path. Only accepts data paths.
83
84 Usage:
85 /> cd /module:node/node2
86 /> cd ..)";
Václav Kubernát24df80e2018-06-06 15:18:03 +020087 bool operator==(const cd_& b) const;
Václav Kubernát2eaceb82018-10-08 19:56:30 +020088 dataPath_ m_path;
Václav Kubernát24df80e2018-06-06 15:18:03 +020089};
90
91struct create_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +010092 static constexpr auto name = "create";
Václav Kubernát054cc992019-02-21 14:23:52 +010093 static constexpr auto shortHelp = "create - Create a presence container.";
94 static constexpr auto longHelp = R"(
Václav Kubernátf5f64f02019-03-19 17:15:47 +010095 create path
Václav Kubernát054cc992019-02-21 14:23:52 +010096
Václav Kubernátf5f64f02019-03-19 17:15:47 +010097 Creates a presence container or a list instance specified by path.
Václav Kubernát054cc992019-02-21 14:23:52 +010098
99 Usage:
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100100 /> create /module:pContainer
101 /> create /module:list[key=value][anotherKey=value])";
Václav Kubernát24df80e2018-06-06 15:18:03 +0200102 bool operator==(const create_& b) const;
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200103 dataPath_ m_path;
Václav Kubernát24df80e2018-06-06 15:18:03 +0200104};
105
106struct delete_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +0100107 static constexpr auto name = "delete";
Václav Kubernát054cc992019-02-21 14:23:52 +0100108 static constexpr auto shortHelp = "delete - Delete a presence container.";
109 static constexpr auto longHelp = R"(
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100110 delete path
Václav Kubernát054cc992019-02-21 14:23:52 +0100111
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100112 Deletes a presence container or a list instance specified by path.
Václav Kubernát054cc992019-02-21 14:23:52 +0100113
114 Usage:
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100115 /> delete /module:pContainer
116 /> delete /module:list[key=value][anotherKey=value])";
Václav Kubernát24df80e2018-06-06 15:18:03 +0200117 bool operator==(const delete_& b) const;
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200118 dataPath_ m_path;
Václav Kubernát24df80e2018-06-06 15:18:03 +0200119};
120
121struct set_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +0100122 static constexpr auto name = "set";
Václav Kubernát054cc992019-02-21 14:23:52 +0100123 static constexpr auto shortHelp = "set - Change value of a leaf.";
124 static constexpr auto longHelp = R"(
125 set path_to_leaf value
126
127 Changes the leaf specified by path to value.
128
129 Usage:
130 /> set /module:leaf 123
131 /> set /module:leaf abc)";
Václav Kubernát24df80e2018-06-06 15:18:03 +0200132 bool operator==(const set_& b) const;
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200133 dataPath_ m_path;
Václav Kubernátebca2552018-06-08 19:06:02 +0200134 leaf_data_ m_data;
Václav Kubernát24df80e2018-06-06 15:18:03 +0200135};
136
Václav Kubernát812ee282018-08-30 17:10:03 +0200137struct commit_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +0100138 static constexpr auto name = "commit";
Václav Kubernát054cc992019-02-21 14:23:52 +0100139 static constexpr auto shortHelp = "commit - Commit current changes.";
140 static constexpr auto longHelp = R"(
141 commit
142
143 Commits the current changes. Accepts no arguments.
144
145 Usage:
146 /> commit)";
Václav Kubernát812ee282018-08-30 17:10:03 +0200147 bool operator==(const set_& b) const;
148};
149
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200150struct get_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +0100151 static constexpr auto name = "get";
Václav Kubernát054cc992019-02-21 14:23:52 +0100152 static constexpr auto shortHelp = "get - Retrieve configuration from the server.";
153 static constexpr auto longHelp = R"(
154 get [path]
155
156 Retrieves configuration of the current node. Works recursively.
157 Optionally takes an argument specifying a path, the output will,
158 as if the user was in that node.
159
160 Usage:
161 /> get
162 /> get /module:path)";
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200163 bool operator==(const get_& b) const;
Václav Kubernát5c75b252018-10-10 18:33:47 +0200164 boost::optional<boost::variant<dataPath_, schemaPath_>> m_path;
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200165};
Václav Kubernát812ee282018-08-30 17:10:03 +0200166
Václav Kubernát054cc992019-02-21 14:23:52 +0100167struct help_;
168using CommandTypes = boost::mpl::vector<discard_, ls_, cd_, create_, delete_, set_, commit_, get_, help_>;
169struct help_ : x3::position_tagged {
170 static constexpr auto name = "help";
171 static constexpr auto shortHelp = "help - Print help for commands.";
172 static constexpr auto longHelp = R"(
173 help [command_name]
174
175 Print help for command_name. If used without an argument,
176 print short help for all commands.
177
178 Usage:
179 /> help
180 /> help cd
181 /> help help)";
182 bool operator==(const help_& b) const;
183
184 // The help command has got one optional argument – a command name (type).
185 // All commands are saved in CommandTypes, so we could just use that, but
186 // that way, Spirit would be default constructing the command structs,
187 // which is undesirable, so firstly we use mpl::transform to wrap
188 // CommandTypes with boost::type:
189 using WrappedCommandTypes = boost::mpl::transform<CommandTypes, boost::type<boost::mpl::_>>::type;
190 // Next, we create a variant over the wrapped types:
191 using CommandTypesVariant = boost::make_variant_over<WrappedCommandTypes>::type;
192 // Finally, we wrap the variant with boost::optional:
193 boost::optional<CommandTypesVariant> m_cmd;
194};
195
Václav Kubernát57272422019-02-08 12:48:24 +0100196// TODO: The usage of MPL won't be necessary after std::variant support is added to Spirit
197// https://github.com/boostorg/spirit/issues/270
Václav Kubernát57272422019-02-08 12:48:24 +0100198using command_ = boost::make_variant_over<CommandTypes>::type;
Václav Kubernát24df80e2018-06-06 15:18:03 +0200199
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200200BOOST_FUSION_ADAPT_STRUCT(ls_, m_options, m_path)
Václav Kubernát24df80e2018-06-06 15:18:03 +0200201BOOST_FUSION_ADAPT_STRUCT(cd_, m_path)
202BOOST_FUSION_ADAPT_STRUCT(create_, m_path)
203BOOST_FUSION_ADAPT_STRUCT(delete_, m_path)
Václav Kubernát24df80e2018-06-06 15:18:03 +0200204BOOST_FUSION_ADAPT_STRUCT(set_, m_path, m_data)
Václav Kubernátab538992019-03-06 15:30:50 +0100205BOOST_FUSION_ADAPT_STRUCT(enum_, m_value)
206BOOST_FUSION_ADAPT_STRUCT(binary_, m_value)
Václav Kubernáteeb38842019-03-20 19:46:05 +0100207BOOST_FUSION_ADAPT_STRUCT(identityRef_, m_prefix, m_value)
Václav Kubernát812ee282018-08-30 17:10:03 +0200208BOOST_FUSION_ADAPT_STRUCT(commit_)
Václav Kubernát054cc992019-02-21 14:23:52 +0100209BOOST_FUSION_ADAPT_STRUCT(help_, m_cmd)
Václav Kubernát6d791432018-10-25 16:00:35 +0200210BOOST_FUSION_ADAPT_STRUCT(discard_)
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200211BOOST_FUSION_ADAPT_STRUCT(get_, m_path)