-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShellHelpers.cpp
72 lines (60 loc) · 1.29 KB
/
ShellHelpers.cpp
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
#include "ShellHelpers.h"
#include <Shell.h>
#include <errno.h>
bool shellCheckArgs(Shell &s, int argc, const char* const argv[], int min, int max) {
if(min > 0 && argc < min) {
s.println("Error: too few arguments");
return false;
}
if(max > 0 && argc > max) {
s.println("Error: too many arguments");
return false;
}
return true;
}
void shellPrint(Stream &s, float v) {
s.printf("%.1f\r\n", v);
}
void shellPrint(Stream &s, int v) {
s.println(v);
}
void shellPrint(Stream &s, bool v) {
s.println(v?"true":"false");
}
void shellPrint(Stream &s, const char* v) {
s.println(v);
}
bool shellParse(const char* str, float* v) {
float val = strtof(str, NULL);
if( errno < 0 ) {
return false;
}
*v = val;
return true;
}
bool shellParse(const char* str, int* v) {
int val = strtol(str, NULL, 0);
if( errno < 0 ) {
return false;
}
*v = val;
return true;
}
bool shellParse(const char* str, bool* v) {
if( strcmp(str, "true") == 0 ) {
*v = true;
return true;
}
else if( strcmp(str, "false") == 0 ) {
*v = false;
return true;
}
return false;
}
bool shellParse(const char* str, const char** v) {
*v = str;
return true;
}
ShellCommand(reboot, "Reboot system", [](Shell &shell, int argc, const ShellArguments &argv){
ESP.restart();
});