From e3ecbddba06f91c3ef5b3cd8305a8f2fdb6c9cde Mon Sep 17 00:00:00 2001 From: "Alfred E. Heggestad" Date: Sun, 7 Aug 2016 10:42:47 +0200 Subject: cmd: add support for long commands - Long commands are commands that is 2 or more characters, and MUST be completed by pressing enter. - A given command can have a long-command, a short-command or both. - All long commands are using '/' (slash) prefix - a simple TAB-completion is supported, while punching in your long command you can press TAB to see a list of commands that matches the input string. Feedback is welcome, specially on the long-command strings itself. If you want to suggest changes, please make a small and non-intrusive patch and send it as a Pull Request (PR). --- test/cmd.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- test/main.c | 1 + test/test.h | 1 + 3 files changed, 71 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/cmd.c b/test/cmd.c index 92325b7..f4c738c 100644 --- a/test/cmd.c +++ b/test/cmd.c @@ -32,7 +32,7 @@ static int cmd_test(struct re_printf *pf, void *arg) static const struct cmd cmdv[] = { - {'@', 0, "Test command", cmd_test}, + {NULL, '@', 0, "Test command", cmd_test}, }; @@ -77,3 +77,71 @@ int test_cmd(void) out: return err; } + + +static int long_handler(struct re_printf *pf, void *arg) +{ + struct cmd_arg *carg = arg; + struct test *test = carg->data; + int err = 0; + (void)pf; + + ASSERT_STREQ("123", carg->prm); + + ++test->cmd_called; + + out: + return err; +} + + +static const struct cmd longcmdv[] = { + { "test", 0, 0, "Test Command", long_handler}, +}; + + +int test_cmd_long(void) +{ + struct test test; + const struct cmd *cmd; + static const char *input_str = "/test 123\n"; + struct cmd_ctx *ctx = NULL; + size_t i; + int err; + + memset(&test, 0, sizeof(test)); + + /* Verify that the command does not exist */ + cmd = cmd_find_long("test"); + ASSERT_TRUE(cmd == NULL); + + /* Register and verify command */ + err = cmd_register(longcmdv, ARRAY_SIZE(longcmdv)); + ASSERT_EQ(0, err); + + cmd = cmd_find_long("test"); + ASSERT_TRUE(cmd != NULL); + + /* Feed it some input data .. */ + + for (i=0; i