-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmdmgr.hpp
More file actions
90 lines (78 loc) · 2.75 KB
/
cmdmgr.hpp
File metadata and controls
90 lines (78 loc) · 2.75 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
82
83
84
85
86
87
88
89
90
/**
* @ Author: luoqi
* @ Create Time: 2025-05-22 15:12
* @ Modified by: luoqi
* @ Modified time: 2025-05-22 17:29
* @ Description:
*/
#pragma once
#include <vector>
#include "qshell.h"
class CmdMgr {
public:
private:
typedef int (*Callback)(int, char **);
struct Cmd {
std::string parent{ "" };
std::string name;
Callback cb;
std::string help;
};
public:
inline static std::vector<Cmd> cmd_list;
inline static QShell *cli{ nullptr };
static int init(QShell &inst)
{
static bool inited = false;
if(inited) {
return -1;
}
if(cli != nullptr) {
return -1;
}
cli = &inst;
for(auto &cmd : cmd_list) {
if(cmd.parent.empty()) {
inst.cmd_add(cmd.name.c_str(), cmd.cb, cmd.help.c_str());
}
}
for(auto &cmd : cmd_list) {
if(!cmd.parent.empty()) {
inst.cmd_sub_add(cmd.parent.c_str(), cmd.name.c_str(), cmd.cb, cmd.help.c_str());
}
}
inited = true;
return 0;
}
CmdMgr(std::string name, Callback cb, std::string help) { cmd_list.push_back({ "", name, cb, help }); }
CmdMgr(std::string parent, std::string name, Callback cb, std::string help)
{
cmd_list.push_back({ parent, name, cb, help });
}
};
#ifdef USE_CMDDBG
using CmdTable = QShell::ArgsTable;
#define DBG_PRINTLN(fmt, ...) CmdMgr::cli->println(fmt, ##__VA_ARGS__)
#define DBG_PRINT(fmt, ...) CmdMgr::cli->print(fmt, ##__VA_ARGS__)
/* register a new command */
#define CMD_REGIST(name, cb, help) static CmdMgr __cmd_##cb(name, cb, help)
/* register a new sub command */
#define CMD_SUB_REGIST(parent, name, cb, help) static CmdMgr __cmd_##cb(parent, name, cb, help)
/* trick to parse command line arguments */
#define CMD_ARGS_TRICK(argc, argv, table) \
if(CmdMgr::cli == nullptr) { \
return -1; \
} \
if(ISARGV(1, ?) && ISARGC(2)) { \
return CmdMgr::cli->args_help(table, sizeof(table)); \
} else { \
return CmdMgr::cli->args_exec(argc, argv, table, sizeof(table)); \
}
#else
#define CmdTable ((void)0)
#define DBG_PRINTLN(fmt, ...) ((void)0)
#define DBG_PRINT(fmt, ...) ((void)0)
#define CMD_REGIST(name, cb, help)
#define CMD_SUB_REGIST(parent, name, cb, help)
#define CMD_ARGS_TRICK(argc, argv, table)
#endif