/* Reads test.sh and format a menu showing the various commands */ #include #include #include #include #include #include #include #include #include using namespace std; struct MENU { string verb; string catego; string descrip; MENU (const string &_verb, const string &_catego, const string &_descrip){ for (unsigned i=0; i<_verb.size(); i++){ if (_verb[i] != '"') verb += _verb[i]; } catego = _catego; descrip = _descrip; if (descrip.size()==0) descrip = "z"; } bool operator < (const MENU &s) const{ bool ret = false; int ok = catego.compare(s.catego); if (ok < 0){ ret = true; }else if (ok == 0){ ok = verb.compare (s.verb); if (ok < 0){ ret = true; }else if (ok == 0){ ret = descrip < s.descrip; } } return ret; } }; int main (int argc, char *argv[]) { glocal int ret = -1; glocal const char *scriptfile = "test.sh"; glocal bool printcommands=false; // For bash complete support glocal const char *prefix = ""; // Prefix for bash complete support glocal.ret = (argc,argv); setproginfo ("","0.0","..."); setarg ('s',"script","Script file",glocal.scriptfile,false); setgrouparg("Bash auto-complete"); setarg ('p',"printcommand","Print only command name with documentation",glocal.printcommands,false); setarg (' ',"prefix","Prefix of the command we try to complete",glocal.prefix,false); int ret = -1; glocal map sections; glocal vector menus; (glocal.scriptfile,true); if (strncmp(line,"##",2)==0){ vector tb; int n = str_splitline(line,' ',tb); if (n >= 3){ string rest = tb[2]; for (int i=3; i tb; int n = str_splitline (line,' ',tb); if ( n > 3 && strcmp(tb[2].c_str(),"\"$1\"")==0){ string &verb = tb[4]; bool found = false; for (int i=4; i 0) rest += string(" "); rest += tb[j]; } string catego = tb[i+1]; map::iterator it = glocal.sections.find(catego); if (it != glocal.sections.end()) catego = it->second; glocal.menus.push_back(MENU(verb,catego,rest)); found = true; } } if (!found){ glocal.menus.push_back(MENU(verb,"z","")); } } } return 0; sort (glocal.menus.begin(), glocal.menus.end()); if (glocal.printcommands){ unsigned len = strlen(glocal.prefix); for (auto &m:glocal.menus){ if (len == 0 || strncmp(glocal.prefix,m.verb.c_str(),len)==0){ printf ("%s\n",m.verb.c_str()); } } }else{ string last; for (vector::iterator it = glocal.menus.begin(); it != glocal.menus.end(); it++){ if (last != it->catego){ last = it->catego; printf ("\n%s\n",last.c_str()); } const char *descrip = it->descrip.c_str(); if (strcmp(descrip,"z")==0) descrip = ""; const char *catego = it->catego.c_str(); if (strcmp(catego,"z")==0) catego = " "; printf ("\t%-25s %s\n",it->verb.c_str(),descrip); } } return ret; return glocal.ret; }