diff --git a/CHANGELOG.md b/CHANGELOG.md index 50cb451..f354c69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Nest namespace rang (issue [#167](https://github.com/daniele77/cli/issues/167)) - Add ascii value 8 for backspace (issue [#124](https://github.com/daniele77/cli/issues/124)) - Add check for CMAKE_SKIP_INSTALL_RULES (issue [#160](https://github.com/daniele77/cli/issues/160)) + - Add enter action (issue [#177](https://github.com/daniele77/cli/issues/177) - PR [#180](https://github.com/daniele77/cli/pull/177)) ## [2.0.2] - 2022-08-18 diff --git a/README.md b/README.md index 3185a2d..a6a4fbe 100644 --- a/README.md +++ b/README.md @@ -298,7 +298,7 @@ ioc.run(); You must provide at least a root menu for your cli: -``` +```C++ // create a menu (this is the root menu of our cli) auto rootMenu = make_unique("myprompt"); @@ -311,7 +311,7 @@ Cli cli(std::move(rootMenu)); You can add menus to existing menus, to get a hierarchy: -``` +```C++ auto rootMenu = make_unique("myprompt"); auto menuA = make_unique("a_prompt"); @@ -368,7 +368,7 @@ The library supports adding commands handler via: - `std::function` - lambdas -``` +```C++ static void foo(std::ostream& out, int x) { out << x << std::endl; } @@ -387,7 +387,7 @@ myMenu->Insert("lambda", [](std::ostream& out, int x){ out << x << std::endl; } There is no limit to the number of parameters that a command handler can take. They can be basic types and `std::string`s -``` +```C++ myMenu->Insert( "mycmd", [](std::ostream& out, int a, double b, const std::string& c, bool d, long e) @@ -405,7 +405,7 @@ myMenu->Insert( or they can be custom types overloading the `std::istream << operator`: -``` +```C++ struct Foo { friend istream & operator >> (istream &in, Foo& p); @@ -430,7 +430,7 @@ myMenu->Insert( If you need it, you can have a command handlers taking an arbitrary number of `std::string` parameters: -``` +```C++ myMenu->Insert( "mycmd", [](std::ostream& out, const std::vector& pars) @@ -443,6 +443,20 @@ myMenu->Insert( Please note that in this case your command handler must take *only one* parameter of type `std::vector`. +## Enter and exit actions + +You can add an enter action and/or an exit action (for example to print a welcome/goodbye message +every time a user opens/closes a session, even a remote one): + +```C++ +Cli cli(std::move(rootMenu)); +cli.EnterAction( + [&enterActionDone](std::ostream& out) { out << "Welcome\n"; }); +cli.ExitAction( + [&enterActionDone](std::ostream& out) { out << "Goodbye\n"; }); +``` + + ## License Distributed under the Boost Software License, Version 1.0. diff --git a/include/cli/cli.h b/include/cli/cli.h index 0788139..0b56fb0 100644 --- a/include/cli/cli.h +++ b/include/cli/cli.h @@ -194,15 +194,14 @@ namespace cli void EnterAction(std::ostream& out) { - if (enterAction) { + if (enterAction) enterAction(out); - } } - void ExitAction( std::ostream& out ) + void ExitAction(std::ostream& out) { - if ( exitAction ) - exitAction( out ); + if (exitAction) + exitAction(out); } void StdExceptionHandler(std::ostream& out, const std::string& cmd, const std::exception& e) @@ -318,9 +317,8 @@ namespace cli { cli.EnterAction(out); - if (enterAction) { + if (enterAction) enterAction(out); - } } void Exit()