#include #include #include "cli.h" #include "log.h" #include "serval.h" #include "rhizome.h" int cli_usage(const struct cli_schema *commands) { printf("Usage:\n"); int i,j; for(i=0;commands[i].function;i++) { for(j=0;commands[i].words[j];j++) printf(" %s",commands[i].words[j]); printf("\n %s\n",commands[i].description); } return 0; } int cli_parse(const int argc, const char *const *args, const struct cli_schema *commands, struct cli_parsed *parsed) { int ambiguous = 0; int matched_cmd = -1; int cmd; for (cmd = 0; commands[cmd].function; ++cmd) { struct cli_parsed cmdpa; memset(&cmdpa, 0, sizeof cmdpa); cmdpa.command = &commands[cmd]; cmdpa.args = args; cmdpa.argc = argc; cmdpa.labelc = 0; cmdpa.varargi = -1; const char *word = NULL; unsigned arg = 0; unsigned opt = 0; while ((word = commands[cmd].words[opt])) { //DEBUGF("cmd=%d opt=%d word='%s' args[arg=%d]='%s'", cmd, opt, word, arg, arg < argc ? args[arg] : ""); unsigned wordlen = strlen(word); if (cmdpa.varargi != -1) return WHYF("Internal error: commands[%d].word[%d]=\"%s\" - more words not allowed after \"...\"", cmd, opt, word); /* These are the argument matching rules: * * "..." consumes all remaining arguments * * "word" consumes one argument that exactly matches "word", does not label it * * "