Repository navigation
Expand file tree
/
Copy pathexec.ino
More file actions
81 lines (68 loc) · 1.86 KB
/
Copy pathexec.ino
File metadata and controls
81 lines (68 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* This example shows how to execute commands from code, e.g. to run them at
* startup, from a button or for commands received via MQTT, HTTP or any other
* source which is no stream. Try:
*
* hello
* hello libcli
* repeat 3 hello world
*
* CliCommand::exec() looks up a command by its name and executes it. Like an
* interactively typed command, the command name is not part of argv.
*
* If the command is known at compile time, cmd_<name>() can also be called
* directly. To call a command defined in another file, declare it with
* CLI_COMMAND_DEF(name).
*/
#include <Arduino.h>
#include <stdlib.h>
#include <cli/cli.hpp>
Cli cli;
CLI_COMMAND(hello)
{
ioStream.printf("Hello %s!\n", argc > 0 ? argv[0] : "world");
return 0;
}
/**
* Executes the command given as arguments several times, e.g.
* "repeat 3 hello world" executes "hello world" three times.
*/
CLI_COMMAND(repeat)
{
int8_t ret;
long count;
if (argc < 2)
{
ioStream.printf("Usage: repeat <count> <command> [arguments]\n");
return -1;
}
count = atol(argv[0]);
for (long i = 0; i < count; i++)
{
/* argv[1] is the name of the command, its arguments follow */
ret = CliCommand::exec(ioStream, argv[1], &argv[2], argc - 2);
if (ret != 0)
{
/* exec() returns -1 if the command does not exist */
ioStream.printf("%s failed: %d\n", argv[1], ret);
return -2;
}
}
return 0;
}
void setup()
{
const char *args[] = {"from setup()"};
Serial.begin(115200);
while (!Serial);
Serial.println();
/* Executes a command by its name, e.g. at startup */
CliCommand::exec(Serial, "hello", args, 1);
/* Calls a command directly, as it is known at compile time */
cmd_hello(Serial, nullptr, 0);
cli.begin();
}
void loop()
{
cli.loop();
}