Interactive CLI 2.3
A cross-platform C++14 library for interactive command-line interfaces
Loading...
Searching...
No Matches
menu.h
Go to the documentation of this file.
1/*******************************************************************************
2 * CLI - A simple command line interface.
3 * Copyright (C) 2016-2026 Daniele Pallastrelli
4 *
5 * Boost Software License - Version 1.0 - August 17th, 2003
6 *
7 * Permission is hereby granted, free of charge, to any person or organization
8 * obtaining a copy of the software and accompanying documentation covered by
9 * this license (the "Software") to use, reproduce, display, distribute,
10 * execute, and transmit the Software, and to prepare derivative works of the
11 * Software, and to permit third-parties to whom the Software is furnished to
12 * do so, all subject to the following:
13 *
14 * The copyright notices in the Software and this entire statement, including
15 * the above license grant, this restriction and the following disclaimer,
16 * must be included in all copies of the Software, in whole or in part, and
17 * all derivative works of the Software, unless such copies or derivative
18 * works are solely in the form of machine-executable object code generated by
19 * a source language processor.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 ******************************************************************************/
29
30#ifndef CLI_MENU_H_
31#define CLI_MENU_H_
32
33#include <algorithm>
34#include <cassert>
35#include <cctype>
36#include <functional>
37#include <memory>
38#include <string>
39#include <utility>
40#include <vector>
41
42#include "cmdhandler.h"
43#include "command.h"
44#include "freeformcommand.h"
46
47namespace cli
48{
49 class CliSession;
50
51 class Menu : public Command
52 {
53 public:
54 Menu(const Menu&) = delete;
55 Menu& operator=(const Menu&) = delete;
56 Menu(Menu&&) = delete;
57 Menu& operator=(Menu&&) = delete;
58
59 Menu() : Command({}), parent(nullptr), description(), prompt(), onEnter(), cmds(std::make_shared<Cmds>()) {}
60
61 explicit Menu(const std::string& _name, std::string desc = "(menu)", const std::string& _prompt = "",
62 std::function<void(std::ostream&)> _onEnter = {}) :
63 Command(_name),
64 parent(nullptr),
65 description(std::move(desc)),
66 prompt(_prompt.empty() ? _name : _prompt),
67 onEnter(std::move(_onEnter)),
68 cmds(std::make_shared<Cmds>())
69 {}
70
71 template <typename R, typename ... Args>
72 CmdHandler Insert(const std::string& cmdName, R (*f)(std::ostream&, Args...), const std::string& help, const std::vector<std::string>& parDesc = {});
73
74 template <typename F>
75 CmdHandler Insert(const std::string& cmdName, F f, const std::string& help = "", const std::vector<std::string>& parDesc = {})
76 {
77 return Insert(cmdName, help, parDesc, f, &F::operator());
78 }
79
80 template <typename F>
81 CmdHandler Insert(const std::string& cmdName, const std::vector<std::string>& parDesc, F f, const std::string& help = "")
82 {
83 return Insert(cmdName, help, parDesc, f, &F::operator());
84 }
85
86 CmdHandler Insert(std::unique_ptr<Command>&& cmd)
87 {
88 std::shared_ptr<Command> scmd(std::move(cmd));
89 CmdHandler c(scmd, cmds);
90 cmds->push_back(scmd);
91 return c;
92 }
93
94 CmdHandler Insert(std::unique_ptr<Menu>&& menu)
95 {
96 std::shared_ptr<Menu> smenu(std::move(menu));
97 CmdHandler c(smenu, cmds);
98 smenu->parent = this;
99 cmds->push_back(smenu);
100 return c;
101 }
102
103 bool Exec(const std::vector<std::string>& cmdLine, CliSession& session) override
104 {
105 return HandleCommand({Name()}, cmdLine, session);
106 }
107
108 bool ExecParent(const std::vector<std::string>& cmdLine, CliSession& session)
109 {
110 return HandleCommand({Name(), ParentShortcut()}, cmdLine, session);
111 }
112
113 bool ScanCmds(const std::vector<std::string>& cmdLine, CliSession& session)
114 {
115 if (!IsEnabled())
116 return false;
117 for (auto& cmd : *cmds)
118 if (cmd->Exec(cmdLine, session))
119 return true;
120 assert(!cmdLine.empty());
121 return (parent && parent->ExecParent(cmdLine, session));
122 }
123
124 std::string Prompt() const
125 {
126 return prompt;
127 }
128
129 void EnterAction(const std::function<void(std::ostream&)>& action)
130 {
131 onEnter = action;
132 }
133
134 void OnEnter(std::ostream& out) const
135 {
136 if (onEnter)
137 onEnter(out);
138 }
139
140 void MainHelp(detail::HelpGroup& entries)
141 {
142 if (!IsEnabled()) return;
143 for (const auto& cmd : *cmds)
144 cmd->Help(entries);
145 if (parent != nullptr)
146 parent->Help(entries);
147 }
148
149 void Help(detail::HelpGroup& entries) const override
150 {
151 if (!IsEnabled()) return;
152 detail::HelpEntry e;
153 e.name = Name();
154 e.description = description;
155 e.isMenu = true;
156 entries.push_back(std::move(e));
157 }
158
159 std::vector<std::string> GetCompletions(const std::string& currentLine) const
160 {
161 auto result = cli::GetCompletions(cmds, currentLine);
162 if (parent != nullptr)
163 {
164 auto c = parent->GetCompletionWithParent(currentLine);
165 result.insert(result.end(), std::make_move_iterator(c.begin()), std::make_move_iterator(c.end()));
166 }
167 return result;
168 }
169
170 std::vector<std::string> GetCompletionRecursive(const std::string& line) const override
171 {
172 if (line.rfind(Name(), 0) == 0)
173 {
174 return GetCompletionRecursiveHelper(line, Name());
175 }
177 }
178
179 private:
180 std::vector<std::string> GetCompletionWithParent(const std::string& line) const
181 {
182 if (line.rfind(Name(), 0) == 0)
183 {
184 return GetCompletionRecursiveHelper(line, Name());
185 }
186
187 if (line.rfind(ParentShortcut(), 0) == 0)
188 {
189 return GetCompletionRecursiveHelper(line, ParentShortcut());
190 }
191
193 }
194
195 std::vector<std::string> GetCompletionRecursiveHelper(const std::string& line, const std::string& prefix) const
196 {
197 auto rest = line;
198 rest.erase(0, prefix.size());
199 rest.erase(rest.begin(), std::find_if(rest.begin(), rest.end(), [](int ch) { return !std::isspace(ch); }));
200 std::vector<std::string> result;
201 for (const auto& cmd : *cmds)
202 {
203 auto cs = cmd->GetCompletionRecursive(rest);
204 for (const auto& c : cs)
205 result.push_back(c);
206 }
207 if (parent != nullptr)
208 {
209 auto cs = parent->GetCompletionWithParent(rest);
210 for (const auto& c : cs)
211 result.push_back(c);
212 }
213 return result;
214 }
215
216 bool HandleCommand(const std::vector<std::string>& cmdNames, const std::vector<std::string>& cmdLine, CliSession& session);
217
218 static std::string ParentShortcut()
219 {
220 return "..";
221 }
222
223 template <typename F, typename R, typename ... Args>
224 CmdHandler Insert(const std::string& name, const std::string& help, const std::vector<std::string>& parDesc, F& f, R (F::*)(std::ostream& out, Args...) const);
225
226 template <typename F, typename R>
227 CmdHandler Insert(const std::string& name, const std::string& help, const std::vector<std::string>& parDesc, F& f, R (F::*)(std::ostream& out, const std::vector<std::string>&) const);
228
229 template <typename F, typename R>
230 CmdHandler Insert(const std::string& name, const std::string& help, const std::vector<std::string>& parDesc, F& f, R (F::*)(std::ostream& out, std::vector<std::string>) const);
231
232 Menu* parent{nullptr};
233 const std::string description;
234 const std::string prompt;
235 std::function<void(std::ostream&)> onEnter;
236 using Cmds = std::vector<std::shared_ptr<Command>>;
237 std::shared_ptr<Cmds> cmds;
238 };
239
240 template <typename R, typename ... Args>
241 CmdHandler Menu::Insert(const std::string& cmdName, R (*f)(std::ostream&, Args...), const std::string& help, const std::vector<std::string>& parDesc)
242 {
243 using F = R (*)(std::ostream&, Args...);
244 return Insert(std::make_unique<VariadicFunctionCommand<F, Args ...>>(cmdName, f, help, parDesc));
245 }
246
247 template <typename F, typename R, typename ... Args>
248 CmdHandler Menu::Insert(const std::string& cmdName, const std::string& help, const std::vector<std::string>& parDesc, F& f, R (F::*)(std::ostream& out, Args...) const)
249 {
250 return Insert(std::make_unique<VariadicFunctionCommand<F, Args ...>>(cmdName, f, help, parDesc));
251 }
252
253 template <typename F, typename R>
254 CmdHandler Menu::Insert(const std::string& cmdName, const std::string& help, const std::vector<std::string>& parDesc, F& f, R (F::*)(std::ostream& out, const std::vector<std::string>& args) const)
255 {
256 return Insert(std::make_unique<FreeformCommand<F>>(cmdName, f, help, parDesc));
257 }
258
259 template <typename F, typename R>
260 CmdHandler Menu::Insert(const std::string& cmdName, const std::string& help, const std::vector<std::string>& parDesc, F& f, R (F::*)(std::ostream& out, std::vector<std::string> args) const)
261 {
262 return Insert(std::make_unique<FreeformCommand<F>>(cmdName, f, help, parDesc));
263 }
264}
265
266#endif // CLI_MENU_H
virtual std::vector< std::string > GetCompletionRecursive(const std::string &line) const
Definition command.h:60
Command(std::string _name)
Definition command.h:48
bool IsEnabled() const
Definition command.h:69
const std::string & Name() const
Definition command.h:68
bool ScanCmds(const std::vector< std::string > &cmdLine, CliSession &session)
Definition menu.h:113
std::vector< std::string > GetCompletions(const std::string &currentLine) const
Definition menu.h:159
Menu(const std::string &_name, std::string desc="(menu)", const std::string &_prompt="", std::function< void(std::ostream &)> _onEnter={})
Definition menu.h:61
CmdHandler Insert(std::unique_ptr< Command > &&cmd)
Definition menu.h:86
CmdHandler Insert(const std::string &cmdName, const std::vector< std::string > &parDesc, F f, const std::string &help="")
Definition menu.h:81
CmdHandler Insert(const std::string &cmdName, R(*f)(std::ostream &, Args...), const std::string &help, const std::vector< std::string > &parDesc={})
Definition menu.h:241
std::string Prompt() const
Definition menu.h:124
void EnterAction(const std::function< void(std::ostream &)> &action)
Definition menu.h:129
void MainHelp(detail::HelpGroup &entries)
Definition menu.h:140
bool ExecParent(const std::vector< std::string > &cmdLine, CliSession &session)
Definition menu.h:108
Menu & operator=(const Menu &)=delete
bool Exec(const std::vector< std::string > &cmdLine, CliSession &session) override
Definition menu.h:103
Menu(Menu &&)=delete
void OnEnter(std::ostream &out) const
Definition menu.h:134
CmdHandler Insert(std::unique_ptr< Menu > &&menu)
Definition menu.h:94
std::vector< std::string > GetCompletionRecursive(const std::string &line) const override
Definition menu.h:170
Menu()
Definition menu.h:59
CmdHandler Insert(const std::string &cmdName, F f, const std::string &help="", const std::vector< std::string > &parDesc={})
Definition menu.h:75
Menu(const Menu &)=delete
void Help(detail::HelpGroup &entries) const override
Definition menu.h:149
Menu & operator=(Menu &&)=delete
std::vector< std::string > GetCompletions(const std::shared_ptr< std::vector< std::shared_ptr< Command > > > &cmds, const std::string &currentLine)
Definition command.h:76