Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0580821

Browse files
committed
Cli constructor revision. Fix daniele77#75
1 parent 5207090 commit 0580821

File tree

8 files changed

+109
-30
lines changed

8 files changed

+109
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Fix missing echo after ctrl-v paste of command (issue [#72](https://github.com/daniele77/cli/issues/72))
88
- Remove the symbol BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT (issue [#89](https://github.com/daniele77/cli/issues/89))
99
- Fix unused parameters warning in release mode (issue [#90](https://github.com/daniele77/cli/issues/90))
10+
- Cli constructor revision (issue [#75](https://github.com/daniele77/cli/issues/75))
1011

1112
## [1.2.1] - 2020-08-27
1213

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ A cross-platform header only C++14 library for interactive command line interfac
1010

1111
![C/C++ CI of Cli](https://github.com/daniele77/cli/workflows/C/C++%20CI%20of%20Cli/badge.svg)
1212

13+
[:heart: Sponsor](https://github.com/sponsors/daniele77)
14+
15+
**IMPORTANT: Breaking API changes** Version 2.0 of `cli` have made breaking changes in order to add more functionality.
16+
To migrate your application to new `cli` version your code, see the secion
17+
"Async programming and Schedulers" of this file, or the examples that come with the library.
18+
1319
## Features
1420

1521
* Header only
@@ -129,6 +135,14 @@ E.g., from a visual studio console, use one of the following commands:
129135
Set the environment variable BOOST. Then, open the file
130136
`cli/examples/examples.sln`
131137

138+
## Compilation of the Doxygen documentation
139+
140+
If you have doxygen installed on your system, you can get the html documentation
141+
of the library in this way:
142+
143+
<enter the directory doc/doxy>
144+
doxygen Doxyfile
145+
132146
## CLI usage
133147

134148
The cli interpreter can manage correctly sentences using quote (') and double quote (").

doc/doxy/Doxyfile

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,25 @@
2929

3030
#---------------------------------------------------------------------------
3131
PROJECT_NAME = "Interactive CLI"
32+
PROJECT_NUMBER = 2.0
33+
#---------------------------------------------------------------------------
34+
FULL_PATH_NAMES = NO
3235
#---------------------------------------------------------------------------
3336
EXTRACT_ALL = YES
34-
EXTRACT_PRIVATE = YES
35-
EXTRACT_STATIC = YES
37+
EXTRACT_PRIVATE = NO
38+
EXTRACT_STATIC = NO
3639
#---------------------------------------------------------------------------
3740
INPUT = ../../include/cli
3841
#---------------------------------------------------------------------------
3942
SOURCE_BROWSER = YES
40-
INLINE_SOURCES = YES
43+
VERBATIM_HEADERS = NO
44+
INLINE_SOURCES = NO
4145
#---------------------------------------------------------------------------
4246
GENERATE_HTML = YES
43-
HTML_COLORSTYLE_HUE = 58
44-
HTML_COLORSTYLE_SAT = 150
45-
HTML_COLORSTYLE_GAMMA = 240
47+
HTML_COLORSTYLE_HUE = 220
48+
HTML_COLORSTYLE_SAT = 100
49+
HTML_COLORSTYLE_GAMMA = 80
50+
SEARCHENGINE = YES
4651
#---------------------------------------------------------------------------
4752
GENERATE_LATEX = NO
4853
GENERATE_RTF = NO
@@ -62,3 +67,4 @@ CALL_GRAPH = NO
6267
CALLER_GRAPH = YES
6368
GRAPHICAL_HIERARCHY = YES
6469
DIRECTORY_GRAPH = YES
70+

include/cli/boostasioremotecli.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
#ifndef CLI_BOOSTASIOREMOTECLI_H_
3131
#define CLI_BOOSTASIOREMOTECLI_H_
3232

33-
#include "detail/genericasioremotecli.h"
3433
#include "detail/boostasiolib.h"
34+
#include "detail/genericasioremotecli.h"
3535

3636
namespace cli { using BoostAsioCliTelnetServer = detail::CliGenericTelnetServer<detail::BoostAsioLib>; }
3737

include/cli/cli.h

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,20 @@ namespace cli
122122
// end inner class
123123

124124
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")]]
125136
explicit Cli(
126137
std::unique_ptr<Menu>&& _rootMenu,
127-
std::function< void(std::ostream&)> _exitAction = {},
138+
std::function< void(std::ostream&)> _exitAction,
128139
std::unique_ptr<HistoryStorage>&& historyStorage = std::make_unique<VolatileHistoryStorage>()
129140
) :
130141
globalHistoryStorage(std::move(historyStorage)),
@@ -133,30 +144,66 @@ namespace cli
133144
{
134145
}
135146

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{}
138163
{
139164
}
140165

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;
148198

149199
Menu* RootMenu() { return rootMenu.get(); }
150-
void ExitAction( const std::function< void(std::ostream&)>& action ) { exitAction = action; }
200+
151201
void ExitAction( std::ostream& out )
152202
{
153203
if ( exitAction )
154204
exitAction( out );
155205
}
156-
void StdExceptionHandler(const std::function< void(std::ostream&, const std::string& cmd, const std::exception&) >& handler)
157-
{
158-
exceptionHandler = handler;
159-
}
206+
160207
void StdExceptionHandler(std::ostream& out, const std::string& cmd, const std::exception& e)
161208
{
162209
if (exceptionHandler)
@@ -166,13 +213,8 @@ namespace cli
166213
}
167214

168215
static void Register(std::ostream& o) { cout().Register(o); }
169-
static void UnRegister(std::ostream& o) { cout().UnRegister(o); }
170216

171-
static OutStream& cout()
172-
{
173-
static OutStream s;
174-
return s;
175-
}
217+
static void UnRegister(std::ostream& o) { cout().UnRegister(o); }
176218

177219
void StoreCommands(const std::vector<std::string>& cmds)
178220
{

include/cli/clilocalsession.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#ifndef CLI_CLILOCALSESSION_H
3131
#define CLI_CLILOCALSESSION_H
3232

33+
#include <ostream> // std::ostream
3334
#include "detail/keyboard.h"
3435
#include "detail/inputhandler.h"
3536
#include "cli.h" // CliSession
@@ -39,11 +40,25 @@ namespace cli
3940

4041
class Scheduler; // forward declaration
4142

42-
43+
/**
44+
* @brief CliLocalTerminalSession represents a local session.
45+
* You should instantiate it to start an interactive prompt on the standard
46+
* input/output of your application.
47+
* The handlers of the commands will be invoked in the same thread the @c Scheduler runs.
48+
*/
4349
class CliLocalTerminalSession : public CliSession
4450
{
4551
public:
4652

53+
/**
54+
* @brief Construct a new Cli Local Terminal Session object that uses the specified @c std::ostream
55+
* for output. You can also specify a size for the command history.
56+
*
57+
* @param _cli The cli object that defines the menu hierarchy for this session
58+
* @param scheduler The scheduler that will process the command handlers
59+
* @param _out the output stream where command output will be printed
60+
* @param historySize the size of the command history
61+
*/
4762
CliLocalTerminalSession(Cli& _cli, Scheduler& scheduler, std::ostream& _out, std::size_t historySize = 100) :
4863
CliSession(_cli, _out, historySize),
4964
kb(scheduler),

include/cli/detail/genericasioremotecli.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "inputhandler.h"
3636
#include "server.h"
3737
#include "inputdevice.h"
38+
#include "genericasioscheduler.h"
3839

3940
namespace cli
4041
{

include/cli/standaloneasioremotecli.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
#ifndef CLI_STANDALONEASIOREMOTECLI_H_
3131
#define CLI_STANDALONEASIOREMOTECLI_H_
3232

33-
#include "detail/genericasioremotecli.h"
3433
#include "detail/standaloneasiolib.h"
34+
#include "detail/genericasioremotecli.h"
3535

3636
namespace cli { using StandaloneAsioCliTelnetServer = detail::CliGenericTelnetServer<detail::StandaloneAsioLib>; }
3737

0 commit comments

Comments
 (0)