Skip to content

Commit

Permalink
Add a Menu::Insert method working with free functions as handler
Browse files Browse the repository at this point in the history
  • Loading branch information
daniele77 committed Jan 12, 2022
1 parent 6ff6695 commit 63eb029
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Add a non-blocking exec method to schedulers (issue [#127](https://github.com/daniele77/cli/issues/127))
- Add a Menu::Insert method working with free functions as handler

## [2.0.0] - 2021-08-25

Expand Down
6 changes: 6 additions & 0 deletions examples/complete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
using namespace cli;
using namespace std;

static void foo(std::ostream& out, int x) { out << x << std::endl; }

int main()
{
try
Expand All @@ -71,6 +73,10 @@ int main()
// setup cli

auto rootMenu = make_unique<Menu>("cli");
rootMenu->Insert(
"free_function",
foo,
"Call a free function that echoes the parameter passed" );
rootMenu->Insert(
"hello",
[](std::ostream& out){ out << "Hello, world\n"; },
Expand Down
28 changes: 19 additions & 9 deletions include/cli/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ namespace cli
Command(_name), parent(nullptr), description(std::move(desc)), cmds(std::make_shared<Cmds>())
{}

template <typename R, typename ... Args>
CmdHandler Insert(const std::string& cmdName, R (*f)(std::ostream&, Args...), const std::string& help, const std::vector<std::string>& parDesc={});

template <typename F>
CmdHandler Insert(const std::string& cmdName, F f, const std::string& help = "", const std::vector<std::string>& parDesc={})
{
Expand Down Expand Up @@ -564,27 +567,27 @@ namespace cli

// ********************************************************************

template <typename F, typename ... Args>
template <typename ... Args>
struct Select;

template <typename F, typename P, typename ... Args>
struct Select<F, P, Args...>
template <typename P, typename ... Args>
struct Select<P, Args...>
{
template <typename InputIt>
template <typename F, typename InputIt>
static void Exec(const F& f, InputIt first, InputIt last)
{
assert( first != last );
assert( std::distance(first, last) == 1+sizeof...(Args) );
const P p = detail::from_string<typename std::decay<P>::type>(*first);
auto g = [&](auto ... pars){ f(p, pars...); };
Select<decltype(g), Args...>::Exec(g, std::next(first), last);
Select<Args...>::Exec(g, std::next(first), last);
}
};

template <typename F>
struct Select<F>
template <>
struct Select<>
{
template <typename InputIt>
template <typename F, typename InputIt>
static void Exec(const F& f, InputIt first, InputIt last)
{
// silence the unused warning in release mode when assert is disabled
Expand Down Expand Up @@ -646,7 +649,7 @@ namespace cli
try
{
auto g = [&](auto ... pars){ func( session.OutStream(), pars... ); };
Select<decltype(g), Args...>::Exec(g, std::next(cmdLine.begin()), cmdLine.end());
Select<Args...>::Exec(g, std::next(cmdLine.begin()), cmdLine.end());
}
catch (std::bad_cast&)
{
Expand Down Expand Up @@ -823,6 +826,13 @@ namespace cli

// Menu implementation

template <typename R, typename ... Args>
CmdHandler Menu::Insert(const std::string& cmdName, R (*f)(std::ostream&, Args...), const std::string& help, const std::vector<std::string>& parDesc)
{
using F = R (*)(std::ostream&, Args...);
return Insert(std::make_unique<VariadicFunctionCommand<F, Args ...>>(cmdName, f, help, parDesc));
}

template <typename F, typename R, typename ... Args>
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 )
{
Expand Down
6 changes: 3 additions & 3 deletions include/cli/detail/genericasioremotecli.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ class TelnetSession : public Session

// https://www.ibm.com/support/knowledgecenter/SSLTBW_1.13.0/com.ibm.zos.r13.hald001/telcmds.htm

std::string iacDoLineMode{ "\x0FF\x0FD\x022", 3 };
static const std::string iacDoLineMode{ "\x0FF\x0FD\x022", 3 };
this -> OutStream() << iacDoLineMode << std::flush;

std::string iacSbLineMode0IacSe{ "\x0FF\x0FA\x022\x001\x000\x0FF\x0F0", 7 };
static const std::string iacSbLineMode0IacSe{ "\x0FF\x0FA\x022\x001\x000\x0FF\x0F0", 7 };
this -> OutStream() << iacSbLineMode0IacSe << std::flush;

std::string iacWillEcho{ "\x0FF\x0FB\x001", 3 };
static const std::string iacWillEcho{ "\x0FF\x0FB\x001", 3 };
this -> OutStream() << iacWillEcho << std::flush;
/*
constexpr char IAC = '\x0FF'; // 255
Expand Down

0 comments on commit 63eb029

Please sign in to comment.