User Tools

Site Tools


docs:cli_parser

This is an old revision of the document!


CLI parser

Data structure is a lookup tree.

Each node is structured as such:

arg_node
char* full_name
char* quick_name
size_t nb_leaves
bool has_arg
arg_node **leaves
callback_t callback

The command parser works as such:

typedef  void (*callback_t)( const char* command, size_t offset);
 
arg_node cfg_root = ....;
 
arg_node *cfg_cur = cfg_root;
 
void process_cmd( arg_node *cfg_cur, const char* command, size_t offset=0;)
{
    size_t arg_len=0;
    char* arg = command+offset;
 
    // Search the next argument boundary
    while( arg[arg_len] != '\0' && arg[arg_len] != ' ')
        ++arg_len;
 
    // If we don't have an argument, attempt to call the node's callback method.
    if( arg_len == 0 )
    {
        if( cfg_cur->callback != null)
            cfg_cur->callback( command);
 
        return;
    }
 
    if( cfg_cur->leaves != null )
    {
        for( size_t i=0; i<cfg_cur->nb_leaves; ++i)
        {
            if( strncmp( cfg_cur->leaves[i]->quick_name, arg, arg_len) == 0)
            {
                // Positive match
                process_cmd( cfg_cur->leaves[i], command, offset+arg_len+1)
                return;
            }
        }
    }
 
    // If we reach here, none of the leaves matched the next argument.
    Serial.print( F("Unrecognized command.\r\n"));
}
docs/cli_parser.1601543858.txt.gz · Last modified: 2020/10/01 11:17 by root

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki