diff options
author | Alfred E. Heggestad <aeh@db.org> | 2016-08-07 10:42:47 +0200 |
---|---|---|
committer | Alfred E. Heggestad <aeh@db.org> | 2016-08-07 10:42:47 +0200 |
commit | e3ecbddba06f91c3ef5b3cd8305a8f2fdb6c9cde (patch) | |
tree | c12474268e452b242c688689bd8eba3d92f4a151 /test/cmd.c | |
parent | 7f35380ee3a39faad1f10fcbb6b115b8ddc5f2a7 (diff) |
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).
Diffstat (limited to 'test/cmd.c')
-rw-r--r-- | test/cmd.c | 70 |
1 files changed, 69 insertions, 1 deletions
@@ -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<strlen(input_str); i++) { + + err = cmd_process(&ctx, input_str[i], &pf_null, &test); + ASSERT_EQ(0, err); + } + + err = cmd_process_long("test 123", 8, &pf_null, &test); + ASSERT_EQ(0, err); + + ASSERT_EQ(2, test.cmd_called); + + /* Cleanup .. */ + + cmd_unregister(longcmdv); + + cmd = cmd_find_long("test"); + ASSERT_TRUE(cmd == NULL); + + out: + return err; +} |