@@ -122,9 +122,20 @@ namespace cli
122
122
// end inner class
123
123
124
124
public:
125
+ ~Cli () = default ;
126
+ // disable value semantics
127
+ Cli (const Cli&) = delete ;
128
+ Cli& operator = (const Cli&) = delete ;
129
+ // enable move semantics
130
+ Cli (Cli&&) = default ;
131
+ Cli& operator = (Cli&&) = default ;
132
+
133
+ // / \deprecated Use the @c Cli::Cli(std::unique_ptr<Menu>,std::unique_ptr<HistoryStorage>)
134
+ // / overload version and the method @c Cli::ExitAction instead
135
+ [[deprecated(" Use the other overload of Cli constructor and the method Cli::ExitAction instead" )]]
125
136
explicit Cli (
126
137
std::unique_ptr<Menu>&& _rootMenu,
127
- std::function< void (std::ostream&)> _exitAction = {} ,
138
+ std::function< void (std::ostream&)> _exitAction,
128
139
std::unique_ptr<HistoryStorage>&& historyStorage = std::make_unique<VolatileHistoryStorage>()
129
140
) :
130
141
globalHistoryStorage(std::move(historyStorage)),
@@ -133,30 +144,66 @@ namespace cli
133
144
{
134
145
}
135
146
136
- Cli (std::unique_ptr<Menu> _rootMenu, std::unique_ptr<HistoryStorage> historyStorage) :
137
- Cli (std::move(_rootMenu), {}, std::move(historyStorage))
147
+ /* *
148
+ * @brief Construct a new Cli object having a given root menu that contains the first level commands available.
149
+ *
150
+ * @param _rootMenu is the @c Menu containing the first level commands available to the user.
151
+ * @param historyStorage is the policy for the storage of the cli commands history. You must pass an istance of
152
+ * a class derived from @c HistoryStorage. The library provides these policies:
153
+ * - @c VolatileHistoryStorage
154
+ * - @c FileHistoryStorage it's a persistent history. I.e., the command history is preserved after your application
155
+ * is restarted.
156
+ *
157
+ * However, you can develop your own, just derive a class from @c HistoryStorage .
158
+ */
159
+ Cli (std::unique_ptr<Menu> _rootMenu, std::unique_ptr<HistoryStorage> historyStorage = std::make_unique<VolatileHistoryStorage>()) :
160
+ globalHistoryStorage (std::move(historyStorage)),
161
+ rootMenu (std::move(_rootMenu)),
162
+ exitAction{}
138
163
{
139
164
}
140
165
141
- ~Cli () = default ;
142
- // disable value semantics
143
- Cli (const Cli&) = delete ;
144
- Cli& operator = (const Cli&) = delete ;
145
- // enable move semantics
146
- Cli (Cli&&) = default ;
147
- Cli& operator = (Cli&&) = default ;
166
+ /* *
167
+ * @brief Add a global exit action that is called every time a session (local or remote) gets the "exit" command.
168
+ *
169
+ * @param action the function to be called when a session exits, taking a @c std::ostream& parameter to write on that session console.
170
+ */
171
+ void ExitAction (const std::function< void (std::ostream&)>& action) { exitAction = action; }
172
+
173
+ /* *
174
+ * @brief Add an handler that will be called when a @c std::exception (or derived) is thrown inside a command handler.
175
+ * If an exception handler is not set, the exception will be logget on the session output stream.
176
+ *
177
+ * @param handler the function to be called when an exception is thrown, taking a @c std::ostream& parameter to write on that session console
178
+ * and the exception thrown.
179
+ */
180
+ void StdExceptionHandler (const std::function< void (std::ostream&, const std::string& cmd, const std::exception&) >& handler)
181
+ {
182
+ exceptionHandler = handler;
183
+ }
184
+
185
+ /* *
186
+ * @brief Get a global out stream object that can be used to print on every session currently connected (local and remote)
187
+ *
188
+ * @return OutStream& the reference to the global out stream writing on every session console.
189
+ */
190
+ static OutStream& cout ()
191
+ {
192
+ static OutStream s;
193
+ return s;
194
+ }
195
+
196
+ private:
197
+ friend class CliSession ;
148
198
149
199
Menu* RootMenu () { return rootMenu.get (); }
150
- void ExitAction ( const std::function< void (std::ostream&)>& action ) { exitAction = action; }
200
+
151
201
void ExitAction ( std::ostream& out )
152
202
{
153
203
if ( exitAction )
154
204
exitAction ( out );
155
205
}
156
- void StdExceptionHandler (const std::function< void (std::ostream&, const std::string& cmd, const std::exception&) >& handler)
157
- {
158
- exceptionHandler = handler;
159
- }
206
+
160
207
void StdExceptionHandler (std::ostream& out, const std::string& cmd, const std::exception& e)
161
208
{
162
209
if (exceptionHandler)
@@ -166,13 +213,8 @@ namespace cli
166
213
}
167
214
168
215
static void Register (std::ostream& o) { cout ().Register (o); }
169
- static void UnRegister (std::ostream& o) { cout ().UnRegister (o); }
170
216
171
- static OutStream& cout ()
172
- {
173
- static OutStream s;
174
- return s;
175
- }
217
+ static void UnRegister (std::ostream& o) { cout ().UnRegister (o); }
176
218
177
219
void StoreCommands (const std::vector<std::string>& cmds)
178
220
{
0 commit comments