Hi guys
Often I need to parse an user input from an UART and do something accordingly. eg print some internal memory info, print out some events etc...
here is how I do it, it's OK to just do a few commands, but I find it not very good if I want to do say 10 commands and 10 commands with params. It become very difficult to manage the code.
I am hoping someone can show me or point me to a better way to do this. Ideally, something like this is a lot easier to read and manage, but I am happy with any suggestions.
Often I need to parse an user input from an UART and do something accordingly. eg print some internal memory info, print out some events etc...
here is how I do it, it's OK to just do a few commands, but I find it not very good if I want to do say 10 commands and 10 commands with params. It become very difficult to manage the code.
Code:
// buffer with user input
uint8_t *buff;
// command only
if parse(buff, "command_1"){
// do stuff
}
// command with params
if parse(buff, "command_2"){
// some magical code to make buff point to after "command_1"
if (parse(buff, "params_1"){
// do stuff
}
}
Code:
// buffer with user input
uint8_t *buff;
// hopefully something like this??
void cli_parse("variable arguments", function _callback);
// then I can use this like this
// command only
cli_parse(buff, "cmd_1", cmd_1_callback);
// command with parameter
cli_parse(buff, "cmd_2", "param_2", cmd_2_callback);