CMD.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/xia-chu/ZLToolKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef SRC_UTIL_CMD_H_
  11. #define SRC_UTIL_CMD_H_
  12. #include <map>
  13. #include <mutex>
  14. #include <string>
  15. #include <memory>
  16. #include <vector>
  17. #include <sstream>
  18. #include <iostream>
  19. #include <functional>
  20. #include "Util/mini.h"
  21. #include "Util/onceToken.h"
  22. using namespace std;
  23. namespace toolkit{
  24. class Option {
  25. public:
  26. typedef function<bool(const std::shared_ptr<ostream> &stream, const string &arg)> OptionHandler;
  27. enum ArgType {
  28. ArgNone = 0,//no_argument,
  29. ArgRequired = 1,//required_argument,
  30. ArgOptional = 2,//optional_argument
  31. };
  32. Option(){}
  33. Option(char shortOpt,
  34. const char *longOpt,
  35. enum ArgType argType,
  36. const char *defaultValue,
  37. bool mustExist,//该参数是否必须存在
  38. const char *des,
  39. const OptionHandler &cb) {
  40. _shortOpt = shortOpt;
  41. _longOpt = longOpt;
  42. _argType = argType;
  43. if(argType != ArgNone){
  44. if(defaultValue){
  45. _defaultValue = std::make_shared<string>(defaultValue);
  46. }
  47. if(!_defaultValue && mustExist){
  48. _mustExist = true;
  49. }
  50. }
  51. _des = des;
  52. _cb = cb;
  53. }
  54. ~Option() {}
  55. bool operator()(const std::shared_ptr<ostream> &stream, const string &arg){
  56. return _cb ? _cb(stream,arg): true;
  57. }
  58. private:
  59. friend class OptionParser;
  60. char _shortOpt;
  61. string _longOpt;
  62. std::shared_ptr<string> _defaultValue;
  63. enum ArgType _argType;
  64. string _des;
  65. OptionHandler _cb;
  66. bool _mustExist = false;
  67. };
  68. class OptionParser {
  69. public:
  70. typedef function< void(const std::shared_ptr<ostream> &,mINI &)> OptionCompleted;
  71. OptionParser(const OptionCompleted &cb = nullptr,bool enableEmptyArgs = true) {
  72. _onCompleted = cb;
  73. _enableEmptyArgs = enableEmptyArgs;
  74. _helper = Option('h', "help", Option::ArgNone, nullptr, false, "打印此信息",
  75. [this](const std::shared_ptr<ostream> &stream,const string &arg)->bool {
  76. static const char *argsType[] = {"无参","有参","选参"};
  77. static const char *mustExist[] = {"选填","必填"};
  78. static string defaultPrefix = "默认:";
  79. static string defaultNull = "null";
  80. stringstream printer;
  81. size_t maxLen_longOpt = 0;
  82. auto maxLen_default = defaultNull.size();
  83. for (auto &pr : _map_options) {
  84. auto &opt = pr.second;
  85. if(opt._longOpt.size() > maxLen_longOpt){
  86. maxLen_longOpt = opt._longOpt.size();
  87. }
  88. if(opt._defaultValue){
  89. if(opt._defaultValue->size() > maxLen_default){
  90. maxLen_default = opt._defaultValue->size();
  91. }
  92. }
  93. }
  94. for (auto &pr : _map_options) {
  95. auto &opt = pr.second;
  96. //打印短参和长参名
  97. if(opt._shortOpt){
  98. printer <<" -" << opt._shortOpt <<" --" << opt._longOpt;
  99. }else{
  100. printer <<" " << " " <<" --" << opt._longOpt;
  101. }
  102. for (size_t i = 0; i < maxLen_longOpt - opt._longOpt.size(); ++i) {
  103. printer << " ";
  104. }
  105. //打印是否有参
  106. printer << " " << argsType[opt._argType];
  107. //打印默认参数
  108. string defaultValue = defaultNull;
  109. if(opt._defaultValue){
  110. defaultValue = *opt._defaultValue;
  111. }
  112. printer << " " << defaultPrefix << defaultValue;
  113. for (size_t i = 0; i < maxLen_default - defaultValue.size(); ++i) {
  114. printer << " ";
  115. }
  116. //打印是否必填参数
  117. printer << " " << mustExist[opt._mustExist];
  118. //打印描述
  119. printer << " " << opt._des << endl;
  120. }
  121. throw std::invalid_argument(printer.str());
  122. });
  123. (*this) << _helper;
  124. }
  125. ~OptionParser() {
  126. }
  127. OptionParser &operator <<(Option &&option) {
  128. int index = 0xFF + (int)_map_options.size();
  129. if(option._shortOpt){
  130. _map_charIndex.emplace(option._shortOpt,index);
  131. }
  132. _map_options.emplace(index, std::forward<Option>(option));
  133. return *this;
  134. }
  135. OptionParser &operator <<(const Option &option) {
  136. int index = 0xFF + (int)_map_options.size();
  137. if(option._shortOpt){
  138. _map_charIndex.emplace(option._shortOpt,index);
  139. }
  140. _map_options.emplace(index, option);
  141. return *this;
  142. }
  143. void delOption(const char *key){
  144. for (auto &pr : _map_options) {
  145. if(pr.second._longOpt == key){
  146. if(pr.second._shortOpt){
  147. _map_charIndex.erase(pr.second._shortOpt);
  148. }
  149. _map_options.erase(pr.first);
  150. break;
  151. }
  152. }
  153. }
  154. void operator ()(mINI &allArg, int argc, char *argv[],const std::shared_ptr<ostream> &stream);
  155. private:
  156. map<int,Option> _map_options;
  157. map<char,int> _map_charIndex;
  158. OptionCompleted _onCompleted;
  159. Option _helper;
  160. static mutex s_mtx_opt;
  161. bool _enableEmptyArgs;
  162. };
  163. class CMD :public mINI{
  164. public:
  165. CMD(){};
  166. virtual ~CMD(){};
  167. virtual const char *description() const {
  168. return "description";
  169. }
  170. void operator ()(int argc, char *argv[],const std::shared_ptr<ostream> &stream = nullptr) {
  171. this->clear();
  172. std::shared_ptr<ostream> coutPtr(&cout,[](ostream *){});
  173. (*_parser)(*this,argc, argv,stream? stream : coutPtr );
  174. }
  175. bool hasKey(const char *key){
  176. return this->find(key) != this->end();
  177. }
  178. vector<variant> splitedVal(const char *key,const char *delim= ":"){
  179. vector<variant> ret;
  180. auto &val = (*this)[key];
  181. split(val,delim,ret);
  182. return ret;
  183. }
  184. void delOption(const char *key){
  185. if(_parser){
  186. _parser->delOption(key);
  187. }
  188. }
  189. protected:
  190. std::shared_ptr<OptionParser> _parser;
  191. private:
  192. void split(const string& s, const char *delim,vector<variant> &ret){
  193. size_t last = 0;
  194. auto index = s.find(delim, last);
  195. while (index != string::npos) {
  196. if(index - last > 0){
  197. ret.push_back(s.substr(last, index - last));
  198. }
  199. last = index + strlen(delim);
  200. index = s.find(delim, last);
  201. }
  202. if (s.size() - last > 0) {
  203. ret.push_back(s.substr(last));
  204. }
  205. }
  206. };
  207. class CMDRegister
  208. {
  209. public:
  210. CMDRegister() {};
  211. ~CMDRegister(){};
  212. static CMDRegister &Instance();
  213. void clear(){
  214. lock_guard<recursive_mutex> lck(_mtxCMD);
  215. _mapCMD.clear();
  216. }
  217. void registCMD(const char *name,const std::shared_ptr<CMD> &cmd){
  218. lock_guard<recursive_mutex> lck(_mtxCMD);
  219. _mapCMD.emplace(name,cmd);
  220. }
  221. void unregistCMD(const char *name){
  222. lock_guard<recursive_mutex> lck(_mtxCMD);
  223. _mapCMD.erase(name);
  224. }
  225. std::shared_ptr<CMD> operator[](const char *name){
  226. lock_guard<recursive_mutex> lck(_mtxCMD);
  227. auto it = _mapCMD.find(name);
  228. if(it == _mapCMD.end()){
  229. throw std::invalid_argument(string("命令不存在:") + name);
  230. }
  231. return it->second;
  232. }
  233. void operator()(const char *name,int argc,char *argv[],const std::shared_ptr<ostream> &stream = nullptr){
  234. auto cmd = (*this)[name];
  235. if(!cmd){
  236. throw std::invalid_argument(string("命令不存在:") + name);
  237. }
  238. (*cmd)(argc,argv,stream);
  239. };
  240. void printHelp(const std::shared_ptr<ostream> &streamTmp = nullptr){
  241. auto stream = streamTmp;
  242. if(!stream){
  243. stream.reset(&cout,[](ostream *){});
  244. }
  245. lock_guard<recursive_mutex> lck(_mtxCMD);
  246. size_t maxLen = 0;
  247. for (auto &pr : _mapCMD) {
  248. if(pr.first.size() > maxLen){
  249. maxLen = pr.first.size();
  250. }
  251. }
  252. for (auto &pr : _mapCMD) {
  253. (*stream) << " " << pr.first;
  254. for (size_t i = 0; i < maxLen - pr.first.size(); ++i) {
  255. (*stream) << " ";
  256. }
  257. (*stream) << " " << pr.second->description() << endl;
  258. }
  259. };
  260. void operator()(const string &line,const std::shared_ptr<ostream> &stream = nullptr){
  261. if(line.empty()){
  262. return;
  263. }
  264. vector<char *> argv;
  265. size_t argc = getArgs((char *)line.data(), argv);
  266. if (argc == 0) {
  267. return;
  268. }
  269. string cmd = argv[0];
  270. lock_guard<recursive_mutex> lck(_mtxCMD);
  271. auto it = _mapCMD.find(cmd);
  272. if (it == _mapCMD.end()) {
  273. stringstream ss;
  274. ss << " 未识别的命令\"" << cmd << "\",输入 \"help\" 获取帮助.";
  275. throw std::invalid_argument(ss.str());
  276. }
  277. (*it->second)((int)argc,&argv[0],stream);
  278. };
  279. private:
  280. size_t getArgs(char *buf, vector<char *> &argv) {
  281. size_t argc = 0;
  282. bool start = false;
  283. auto len = strlen(buf);
  284. for (size_t i = 0; i < len; ++i) {
  285. if (buf[i] != ' ' && buf[i] != '\t' && buf[i] != '\r' && buf[i] != '\n') {
  286. if (!start) {
  287. start = true;
  288. if(argv.size() < argc + 1){
  289. argv.resize(argc + 1);
  290. }
  291. argv[argc++] = buf + i;
  292. }
  293. } else {
  294. buf[i] = '\0';
  295. start = false;
  296. }
  297. }
  298. return argc;
  299. }
  300. private:
  301. map<string,std::shared_ptr<CMD> > _mapCMD;
  302. recursive_mutex _mtxCMD;
  303. };
  304. //帮助命令(help),该命令默认已注册
  305. class CMD_help: public CMD {
  306. public:
  307. CMD_help(){
  308. _parser = std::make_shared<OptionParser>(nullptr);
  309. (*_parser) << Option('c', "cmd", Option::ArgNone, nullptr ,false,"列出所有命令",
  310. [](const std::shared_ptr<ostream> &stream,const string &arg) {
  311. CMDRegister::Instance().printHelp(stream);
  312. return true;
  313. });
  314. }
  315. ~CMD_help() {}
  316. const char *description() const override {
  317. return "打印帮助信息";
  318. }
  319. };
  320. class ExitException : public std::exception
  321. {
  322. public:
  323. ExitException(){}
  324. ~ExitException(){}
  325. };
  326. //退出程序命令(exit),该命令默认已注册
  327. class CMD_exit: public CMD {
  328. public:
  329. CMD_exit(){
  330. _parser = std::make_shared<OptionParser>([](const std::shared_ptr<ostream> &,mINI &){
  331. throw ExitException();
  332. });
  333. }
  334. ~CMD_exit() {}
  335. const char *description() const override {
  336. return "退出shell";
  337. }
  338. };
  339. //退出程序命令(quit),该命令默认已注册
  340. #define CMD_quit CMD_exit
  341. //清空屏幕信息命令(clear),该命令默认已注册
  342. class CMD_clear : public CMD
  343. {
  344. public:
  345. CMD_clear(){
  346. _parser = std::make_shared<OptionParser>([this](const std::shared_ptr<ostream> &stream,mINI &args){
  347. clear(stream);
  348. });
  349. }
  350. ~CMD_clear(){}
  351. const char *description() const {
  352. return "清空屏幕输出";
  353. }
  354. private:
  355. void clear(const std::shared_ptr<ostream> &stream){
  356. (*stream) << "\x1b[2J\x1b[H";
  357. stream->flush();
  358. }
  359. };
  360. #define GET_CMD(name) (*(CMDRegister::Instance()[name]))
  361. #define CMD_DO(name,...) (*(CMDRegister::Instance()[name]))(__VA_ARGS__)
  362. #define REGIST_CMD(name) CMDRegister::Instance().registCMD(#name,std::make_shared<CMD_##name>());
  363. }//namespace toolkit
  364. #endif /* SRC_UTIL_CMD_H_ */