Interactive CLI 2.3
A cross-platform C++14 library for interactive command-line interfaces
Loading...
Searching...
No Matches
cli.h
Go to the documentation of this file.
1/*******************************************************************************
2 * CLI - A simple command line interface.
3 * Copyright (C) 2016-2021 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_CLI_H_
31#define CLI_CLI_H_
32
33#include <algorithm>
34#include <functional>
35#include <iostream>
36#include <memory>
37#include <string>
38#include <utility>
39#include <vector>
40
41#include "colorprofile.h"
42#include "detail/fromstring.h"
43#include "detail/helpentry.h"
44#include "detail/helpformat.h"
45#include "detail/history.h"
46#include "detail/split.h"
47#include "historystorage.h"
48#include "outstream.h"
50
51namespace cli
52{
53 class Menu;
54 class CliSession;
55
56 class Cli
57 {
58 public:
59 struct Messages
60 {
61 std::string wrongCommand;
62 std::string unknownException;
63 std::string helpHeader;
64 std::string helpUsage;
65 std::string commandNotFound;
66 std::string helpName;
67 std::string helpArgName;
68 std::string helpCommand;
69 std::string exitName;
70 std::string exitCommand;
71 std::string historyName;
72 std::string historyCommand;
73 std::string historyIndexArg;
74 std::string historyExecCommand;
75
77 wrongCommand("wrong command: "),
78 unknownException("Cli. Unknown exception caught handling command line \""),
79 helpHeader("Commands available:\n\n"),
80 helpUsage("usage: help [command]"),
81 commandNotFound("command not found: "),
82 helpName("help"),
83 helpArgName("command"),
84 helpCommand("This help message"),
85 exitName("exit"),
86 exitCommand("Quit the session"),
87 historyName("history"),
88 historyCommand("Show the history"),
89 historyIndexArg("history entry index"),
90 historyExecCommand("Exec a command by index in the history")
91 {
92 }
93 };
94
96 Cli(const Cli&) = delete;
97 Cli& operator=(const Cli&) = delete;
98 Cli(Cli&&) = default;
99 Cli& operator=(Cli&&) = default;
100
101 Cli(std::unique_ptr<Menu> _rootMenu,
102 std::unique_ptr<HistoryStorage> historyStorage = std::make_unique<VolatileHistoryStorage>(),
103 Messages _messages = Messages{});
104 Cli(std::unique_ptr<Menu> _rootMenu,
105 Messages _messages,
106 std::unique_ptr<HistoryStorage> historyStorage = std::make_unique<VolatileHistoryStorage>());
107
108 void EnterAction(const std::function<void(std::ostream&)>& action) { enterAction = action; }
109 void ExitAction(const std::function<void(std::ostream&)>& action) { exitAction = action; }
110
111 void StdExceptionHandler(const std::function<void(std::ostream&, const std::string& cmd, const std::exception&)>& handler)
112 {
113 exceptionHandler = handler;
114 }
115
116 void WrongCommandHandler(const std::function<void(std::ostream&, const std::string& cmd)>& handler)
117 {
118 wrongCmdHandler = handler;
119 }
120
121 void SetMessages(const Messages& _messages)
122 {
123 messages = _messages;
124 }
125
126 const Messages& GetMessages() const
127 {
128 return messages;
129 }
130
131 static OutStream& cout()
132 {
133 return *CoutPtr();
134 }
135
136 private:
137 friend class CliSession;
138
139 static std::shared_ptr<OutStream> CoutPtr()
140 {
141 static std::shared_ptr<OutStream> s = std::make_shared<OutStream>();
142 return s;
143 }
144
145 Menu* RootMenu() { return rootMenu.get(); }
146
147 void EnterAction(std::ostream& out)
148 {
149 if (enterAction)
150 enterAction(out);
151 }
152
153 void ExitAction(std::ostream& out)
154 {
155 if (exitAction)
156 exitAction(out);
157 }
158
159 void StdExceptionHandler(std::ostream& out, const std::string& cmd, const std::exception& e)
160 {
161 if (exceptionHandler)
162 exceptionHandler(out, cmd, e);
163 else
164 out << e.what() << '\n';
165 }
166
167 void WrongCommandHandler(std::ostream& out, const std::string& cmd)
168 {
169 if (wrongCmdHandler)
170 wrongCmdHandler(out, cmd);
171 else
172 out << messages.wrongCommand << cmd << '\n';
173 }
174
175 void StoreCommands(const std::vector<std::string>& cmds)
176 {
177 globalHistoryStorage->Store(cmds);
178 }
179
180 std::vector<std::string> GetCommands() const
181 {
182 return globalHistoryStorage->Commands();
183 }
184
185 private:
186 std::unique_ptr<HistoryStorage> globalHistoryStorage;
187 std::unique_ptr<Menu> rootMenu;
188 std::function<void(std::ostream&)> enterAction;
189 std::function<void(std::ostream&)> exitAction;
190 std::function<void(std::ostream&, const std::string& cmd, const std::exception&)> exceptionHandler;
191 std::function<void(std::ostream&, const std::string& cmd)> wrongCmdHandler;
192 Messages messages;
193 };
194
195}
196
197#include "command.h"
198#include "cmdhandler.h"
199#include "select.h"
201#include "freeformcommand.h"
202#include "menu.h"
203#include "clisession.h"
204
205namespace cli
206{
207 inline Cli::Cli(std::unique_ptr<Menu> _rootMenu,
208 std::unique_ptr<HistoryStorage> historyStorage,
209 Messages _messages) :
210 globalHistoryStorage{std::move(historyStorage)},
211 rootMenu{std::move(_rootMenu)},
212 enterAction{},
213 exitAction{},
214 messages{std::move(_messages)}
215 {
216 }
217
218 inline Cli::Cli(std::unique_ptr<Menu> _rootMenu,
219 Messages _messages,
220 std::unique_ptr<HistoryStorage> historyStorage) :
221 Cli(std::move(_rootMenu), std::move(historyStorage), std::move(_messages))
222 {
223 }
224
225 inline Cli::~Cli() = default;
226}
227
228#endif // CLI_CLI_H_
const Messages & GetMessages() const
Definition cli.h:126
Cli & operator=(Cli &&)=default
void EnterAction(const std::function< void(std::ostream &)> &action)
Definition cli.h:108
void ExitAction(const std::function< void(std::ostream &)> &action)
Definition cli.h:109
static OutStream & cout()
Definition cli.h:131
Cli(Cli &&)=default
Cli(const Cli &)=delete
void StdExceptionHandler(const std::function< void(std::ostream &, const std::string &cmd, const std::exception &)> &handler)
Definition cli.h:111
void WrongCommandHandler(const std::function< void(std::ostream &, const std::string &cmd)> &handler)
Definition cli.h:116
Cli & operator=(const Cli &)=delete
void SetMessages(const Messages &_messages)
Definition cli.h:121
friend class CliSession
Definition cli.h:137
std::string historyExecCommand
Definition cli.h:74
std::string exitCommand
Definition cli.h:70
std::string helpName
Definition cli.h:66
std::string helpArgName
Definition cli.h:67
std::string helpCommand
Definition cli.h:68
std::string historyIndexArg
Definition cli.h:73
std::string historyName
Definition cli.h:71
std::string exitName
Definition cli.h:69
std::string helpUsage
Definition cli.h:64
std::string commandNotFound
Definition cli.h:65
std::string historyCommand
Definition cli.h:72
std::string unknownException
Definition cli.h:62
std::string wrongCommand
Definition cli.h:61
std::string helpHeader
Definition cli.h:63