docs:cli_parser
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| docs:cli_parser [2020/10/01 10:39] – root | docs:cli_parser [2020/10/01 11:34] (current) – root | ||
|---|---|---|---|
| Line 5: | Line 5: | ||
| Each node is structured as such: | Each node is structured as such: | ||
| - | ^ arg_node | + | ^ arg_node |
| - | | char* full_name | + | | char* full_name |
| - | | char* quick_name | + | | char* quick_name |
| - | | size_t nb_leaves | + | | size_t nb_leaves |
| - | | arg_node **leaves | + | | bool has_arg |
| - | | void (*callback)( const char* command, size_t offset) | + | | arg_node **leaves |
| + | | callback_t | ||
| The command parser works as such: | The command parser works as such: | ||
| <code cpp> | <code cpp> | ||
| + | |||
| + | typedef | ||
| + | |||
| arg_node cfg_root = ....; | arg_node cfg_root = ....; | ||
| Line 21: | Line 25: | ||
| void process_cmd( arg_node *cfg_cur, const char* command, size_t offset=0;) | void process_cmd( arg_node *cfg_cur, const char* command, size_t offset=0;) | ||
| { | { | ||
| + | size_t kwd_len=0; | ||
| size_t arg_len=0; | size_t arg_len=0; | ||
| - | char* arg = command+offset; | + | char* keyword |
| + | char* arg = null; | ||
| | | ||
| // Search the next argument boundary | // Search the next argument boundary | ||
| - | while( | + | while( |
| - | ++arg_len; | + | ++kwd_len; |
| - | + | ||
| - | if( arg_len | + | // If we don't have an argument, attempt to call the node's callback method. |
| + | if( kwd_len | ||
| { | { | ||
| - | if( cfg_cur-> | + | if( cfg_cur-> |
| + | { | ||
| cfg_cur-> | cfg_cur-> | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | Serial.print( F(" | ||
| + | } | ||
| + | |||
| + | return; | ||
| } | } | ||
| | | ||
| - | if( leaves != null ) | + | if ( cfg_cur-> |
| + | { | ||
| + | // This node has an argument. | ||
| + | |||
| + | arg = command+offset+kwd_len+1; | ||
| + | |||
| + | // Attempt to get the size of the argument | ||
| + | while( arg[kwd_len] != ' | ||
| + | ++arg_len; | ||
| + | |||
| + | if( arg_len == 0 ) | ||
| + | { | ||
| + | Serial.print( F(" | ||
| + | return; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | if( cfg_cur-> | ||
| { | { | ||
| for( size_t i=0; i< | for( size_t i=0; i< | ||
| { | { | ||
| - | if( strncmp( cfg_cur-> | + | if( strncmp( cfg_cur-> |
| { | { | ||
| // Positive match | // Positive match | ||
| + | process_cmd( cfg_cur-> | ||
| + | return; | ||
| } | } | ||
| } | } | ||
| } | } | ||
| + | | ||
| + | // If we reach here, none of the leaves matched the next argument. | ||
| + | Serial.print( F(" | ||
| } | } | ||
| </ | </ | ||
docs/cli_parser.1601541570.txt.gz · Last modified: 2020/10/01 10:39 by root