libcopp  1.2.0
cmd_option.h
Go to the documentation of this file.
1 #ifndef UTIL_CLI_CMDOPTION_H
2 #define UTIL_CLI_CMDOPTION_H
3 
4 #pragma once
5 
22 #include <assert.h>
23 #include <cstdio>
24 #include <exception>
25 #include <set>
26 
27 
28 #include <map>
29 #include <ostream>
30 #include <sstream>
31 #include <vector>
32 
33 
35 #include "std/ref.h"
36 
37 
38 // 载入绑定器
39 #include "cli/cmd_option_bind.h"
41 #include "cli/cmd_option_string.h"
42 #include "cmd_option_list.h"
43 
44 #include "shell_font.h"
45 
46 namespace util {
47  namespace cli {
48  // 标准指令处理函数(无返回值,参数为选项的映射表)
49  // void function_name (cmd_option_list&, [参数]); // 函数参数可选
50  // void function_name (callback_param, [参数]); // 函数参数可选
51 
52  // 值类型
53  typedef std::shared_ptr<cli::cmd_option_value> value_type;
54 
55 
69  template <typename TCmdStr>
71  public:
72  typedef unsigned char uc_t;
76  typedef std::shared_ptr<binder::cmd_option_bind_base> func_ptr_t;
77  typedef std::map<TCmdStr, func_ptr_t> funmap_type;
78 
79  protected:
80  static short map_value_[256]; // 记录不同字符的映射关系
81  static char trans_value_[256]; // 记录特殊转义字符
82 
83  funmap_type callback_funcs_; // 记录命令的映射函数
84  funmap_type callback_children_; // 子命令组额外索引
87 
93  void run_cmd(const TCmdStr &cmd_content, callback_param params) const {
94  typename funmap_type::const_iterator iter = callback_funcs_.find(cmd_content);
95 
96  // 如果是顶层调用则添加根指令调用栈
97  if (params.get_cmd_array().empty()) {
98  params.append_cmd(ROOT_NODE_CMD, std::const_pointer_cast<binder::cmd_option_bind_base>(shared_from_this()));
99  }
100 
101  if (iter == callback_funcs_.end()) {
102  // 内定命令不报“找不到指令”错
103  if (cmd_content == "@OnDefault") return;
104 
105  iter = callback_funcs_.find("@OnError"); // 查找错误处理函数
106  if (iter != callback_funcs_.end()) {
107  // 错误附加内容(错误内容)
108  params.add("@ErrorMsg=Command Invalid");
109  params.append_cmd(cmd_content.c_str(), iter->second); // 添加当前指令调用栈
110 
111  (*iter->second)(params);
112  }
113  return;
114  }
115 
116  // 添加当前指令调用栈
117  params.append_cmd(cmd_content.c_str(), iter->second);
118  (*iter->second)(params);
119  }
120 
125  std::cout << "Help:" << std::endl;
126  std::cout << (*this);
127  }
128 
129  public:
130  void list_help_msg(help_list_t &msg, const std::string &prefix) const {
131  for (typename funmap_type::const_iterator iter = callback_funcs_.begin(); iter != callback_funcs_.end(); ++iter) {
132  if (iter->first.empty() || '@' == iter->first[0]) {
133  continue;
134  }
135 
136  help_list_t::iterator iter_m;
137  help_msg_t *obj;
138  for (iter_m = msg.begin(), obj = NULL; iter_m != msg.end(); ++iter_m) {
139  if ((*iter_m).binded_obj == iter->second) {
140  obj = &(*iter_m);
141  break;
142  }
143  }
144 
145  // all children do not make a help_msg_t
146  if (callback_children_.find(iter->first) != callback_children_.end()) {
147  self_type *child = dynamic_cast<self_type *>(iter->second.get());
148  assert(child);
149  child->list_help_msg(msg, (prefix + " ") + iter->first.c_str());
150  continue;
151  }
152 
153  if (NULL == obj) {
154  msg.push_back(help_msg_t());
155  obj = &msg.back();
156  assert(obj);
157  obj->binded_obj = iter->second;
158  obj->description = iter->second->get_help_msg();
159  }
160 
161  // any string to std::string
162  std::string full_cmd;
163  full_cmd.reserve(prefix.capacity() + iter->first.capacity());
164  if (!prefix.empty()) {
165  full_cmd = prefix;
166  full_cmd += " ";
167  }
168  full_cmd += iter->first.c_str();
169  obj->cmd_paths.push_back(full_cmd);
170  }
171  }
172 
173  static std::ostream &dump(std::ostream &os, const self_type &self, const std::string &prefix) {
174  help_list_t msgs;
175  self.list_help_msg(msgs, "");
176 
177  size_t cmd_padding = 4;
178  // sort all commands of the same callback
179  for (help_list_t::iterator iter = msgs.begin(); iter != msgs.end(); ++iter) {
180  std::sort((*iter).cmd_paths.begin(), (*iter).cmd_paths.end());
181  std::stringstream ss;
182  bool not_first = false;
183  for (std::vector<std::string>::iterator cmd_it = (*iter).cmd_paths.begin(); cmd_it != (*iter).cmd_paths.end();
184  ++cmd_it) {
185  if (not_first) {
186  ss << ", ";
187  }
188  ss << *cmd_it;
189  not_first = true;
190  }
191 
192  (*iter).all_cmds = ss.str();
193  if ((*iter).all_cmds.size() >= cmd_padding) {
194  cmd_padding = (((*iter).all_cmds.size() >> 2) + 1) << 2;
195  }
196  }
197 
198  std::sort(msgs.begin(), msgs.end(), binder::cmd_option_bind_base::sort_by_all_cmds);
199 
200  for (help_list_t::iterator iter = msgs.begin(); iter != msgs.end(); ++iter) {
201  shell_stream ss(os);
202 
203  ss().open(self.help_cmd_style_) << prefix << (*iter).all_cmds;
204  if ((*iter).all_cmds.size() < cmd_padding) {
205  std::string padding_space;
206  padding_space.resize(cmd_padding - (*iter).all_cmds.size(), ' ');
207  ss() << padding_space;
208  }
209  ss().open(self.help_description_style_) << (*iter).description << std::endl;
210  }
211 
212  return os;
213  }
214 
215  friend std::ostream &operator<<(std::ostream &os, const self_type &self) { return dump(os, self, std::string()); }
216 
217  std::ostream &dump(std::ostream &os, const std::string &prefix) { return dump(os, *this, prefix); }
218 
225  static const char *get_segment(const char *begin_str, std::string &val) {
226  val.clear();
227  char flag; // 字符串开闭字符
228 
229  // 去除分隔符前缀
230  while (*begin_str && (map_value_[(uc_t)*begin_str] & SPLITCHAR))
231  ++begin_str;
232 
233  while (*begin_str && !(map_value_[(uc_t)*begin_str] & SPLITCHAR)) {
234  if (!(map_value_[(uc_t)*begin_str] & STRINGSYM)) {
235  val += *begin_str;
236  ++begin_str;
237  } else {
238  flag = *begin_str;
239  ++begin_str;
240 
241  while (*begin_str && *begin_str != flag) {
242  char cur_byte = *begin_str;
243  if (map_value_[(uc_t)*begin_str] & TRANSLATE) {
244  if (*(begin_str + 1)) {
245  ++begin_str;
246  cur_byte = trans_value_[(uc_t)*begin_str];
247  }
248  }
249 
250  val += cur_byte;
251  ++begin_str;
252  }
253 
254  ++begin_str;
255  break; // 字符串结束后参数结束
256  }
257  }
258 
259  // 去除分隔符后缀
260  while (*begin_str && (map_value_[(uc_t)*begin_str] & SPLITCHAR))
261  ++begin_str;
262  return begin_str;
263  }
264 
270  static std::vector<std::string> split_cmd(const char *begin_str) {
271  std::vector<std::string> ret;
272  for (const char *begin_ptr = begin_str; (*begin_ptr);) {
273  std::string cmd_content;
274  // 去除命令分隔符前缀
275  while ((*begin_ptr) && (map_value_[(uc_t)*begin_ptr] & CMDSPLIT))
276  ++begin_ptr;
277 
278  // 分离命令
279  while ((*begin_ptr) && !(map_value_[(uc_t)*begin_ptr] & CMDSPLIT)) {
280  cmd_content.push_back(*begin_ptr);
281  ++begin_ptr;
282  }
283 
284  if (cmd_content.size() > 0) ret.push_back(cmd_content);
285  }
286 
287  return ret;
288  }
289 
290  private:
295  : help_cmd_style_(shell_font_style::SHELL_FONT_COLOR_YELLOW | shell_font_style::SHELL_FONT_SPEC_BOLD),
296  help_description_style_(0) {
297  // 如果已初始化则跳过
298  if (map_value_[(uc_t)' '] & SPLITCHAR) return;
299 
300  // 分隔符
301  map_value_[(uc_t)' '] = map_value_[(uc_t)'\t'] = map_value_[(uc_t)'\r'] = map_value_[(uc_t)'\n'] = SPLITCHAR;
302  // 字符串开闭符
303  map_value_[(uc_t)'\''] = map_value_[(uc_t)'\"'] = STRINGSYM;
304  // 转义标记符
305  map_value_[(uc_t)'\\'] = TRANSLATE;
306  // 指令分隔符
307  map_value_[(uc_t)' '] |= CMDSPLIT;
308  map_value_[(uc_t)','] = map_value_[(uc_t)';'] = CMDSPLIT;
309 
310  // 转义字符设置
311  for (int i = 0; i < 256; ++i)
312  trans_value_[i] = (uc_t)i;
313 
314  trans_value_[(uc_t)'0'] = '\0';
315  trans_value_[(uc_t)'a'] = '\a';
316  trans_value_[(uc_t)'b'] = '\b';
317  trans_value_[(uc_t)'f'] = '\f';
318  trans_value_[(uc_t)'r'] = '\r';
319  trans_value_[(uc_t)'n'] = '\n';
320  trans_value_[(uc_t)'t'] = '\t';
321  trans_value_[(uc_t)'v'] = '\v';
322  trans_value_[(uc_t)'\\'] = '\\';
323  trans_value_[(uc_t)'\''] = '\'';
324  trans_value_[(uc_t)'\"'] = '\"';
325  }
326 
327  public:
328  typedef std::shared_ptr<cmd_option_bind> ptr_type;
329  static ptr_type create() { return ptr_type(new cmd_option_bind()); }
330 
335  std::shared_ptr<std::vector<const char *> > get_cmd_names() const {
336  typename funmap_type::const_iterator iter = callback_funcs_.begin();
337  std::shared_ptr<std::vector<const char *> > ret_ptr =
338  std::shared_ptr<std::vector<const char *> >(new std::vector<const char *>());
339  while (iter != callback_funcs_.end()) {
340  ret_ptr->push_back(iter->first.c_str());
341  ++iter;
342  }
343  return ret_ptr;
344  }
345 
351  std::shared_ptr<binder::cmd_option_bind_base> get_binded_cmd(const char *cmd_name) const {
352  typename funmap_type::const_iterator iter = callback_funcs_.find(cmd_name);
353  if (iter == callback_funcs_.end()) return std::shared_ptr<binder::cmd_option_bind_base>();
354  return iter->second;
355  }
356 
357  size_t size() const { return callback_funcs_.size(); }
358  size_t empty() const { return callback_funcs_.empty(); }
359  const funmap_type &get_all() const { return callback_funcs_; }
360 
361  size_t children_size() const { return callback_children_.size(); }
362  size_t children_empty() const { return callback_children_.empty(); }
363  const funmap_type &get_all_children() const { return callback_children_; }
364 
365  int get_help_cmd_style() const { return help_cmd_style_; }
366  void set_help_cmd_style(int style) { help_cmd_style_ = style; }
368  void set_help_description_style(int style) { help_description_style_ = style; }
369 
384  void start(callback_param args, bool is_single_cmd = false) const {
385  int argv = static_cast<int>(args.get_params_number());
386  cmd_option_list cmd_args;
387  TCmdStr cmd_content = is_single_cmd ? "@OnError" : "@OnDefault";
388  for (int i = -1; i < argv;) {
389  ++i;
390  cmd_args.clear();
391  cmd_args.load_cmd_array(args.get_cmd_array());
392  cmd_args.set_ext_param(args.get_ext_param());
393 
394  for (; i < argv; ++i) {
395  // 把所有的非指令字符串设为指令参数
396  if (callback_funcs_.find(args[i]->to_string()) == callback_funcs_.end()) {
397  cmd_args.add(args[i]->to_string());
398  } else {
399  // 如果是单指令且有未知参数则分发@OnError错误处理
400  if (is_single_cmd && cmd_args.get_params_number() > 0) {
401  run_cmd(cmd_content, cmd_args);
402  cmd_args.clear();
403  cmd_args.load_cmd_array(args.get_cmd_array());
404  cmd_args.set_ext_param(args.get_ext_param());
405  }
406 
407  // 追加所有参数,执行单指令
408  if (is_single_cmd) {
409  cmd_content = TCmdStr(args[i]->to_cpp_string().c_str(), args[i]->to_cpp_string().size());
410  for (++i; i < argv; ++i)
411  cmd_args.add(args[i]->to_string());
412  }
413  break;
414  }
415  }
416 
417  run_cmd(cmd_content, cmd_args);
418  if (i >= argv) break;
419  cmd_content = TCmdStr(args[i]->to_cpp_string().c_str(), args[i]->to_cpp_string().size());
420  }
421  }
422 
430  inline void start(int argv, const char *argc[], bool is_single_cmd = false, void *ext_param = NULL) const {
431  cmd_option_list copt_list(argv, argc);
432  copt_list.set_ext_param(ext_param);
433 
434  start(copt_list, is_single_cmd);
435  }
436 
444  inline void start(int argv, char *argc[], bool is_single_cmd = false, void *ext_param = NULL) const {
445  typedef const char *conv_char_t;
446 
447  start(argv, (conv_char_t *)argc, is_single_cmd, ext_param);
448  }
449 
456  inline void start(const std::vector<std::string> &cmds, bool is_single_cmd = false, void *ext_param = NULL) const {
457  cmd_option_list copt_list(cmds);
458  copt_list.set_ext_param(ext_param);
459 
460  start(copt_list, is_single_cmd);
461  }
462 
468  void start(const char *cmd_content, bool is_single_cmd = false, void *ext_param = NULL) const {
469  cmd_option_list cmds;
470  std::string seg;
471 
472  // 分离指令
473  while (*cmd_content) {
474  cmd_content = get_segment(cmd_content, seg);
475  cmds.add(seg.c_str());
476  }
477 
478  cmds.set_ext_param(ext_param);
479 
480  start(cmds, is_single_cmd);
481  }
482 
488  inline void start(const std::string &cmd_content, bool is_single_cmd = false, void *ext_param = NULL) const {
489  start(cmd_content.c_str(), is_single_cmd, ext_param);
490  }
491 
496  inline void unbind_cmd(const std::string &cmd_content) {
497  TCmdStr cmd_obj = TCmdStr(cmd_content.c_str(), cmd_content.size());
498  callback_funcs_.erase(cmd_obj);
499  callback_children_.erase(cmd_content);
500  }
501 
505  inline void unbind_all_cmd() {
506  callback_funcs_.clear();
507  callback_children_.clear();
508  }
509 
514  inline std::shared_ptr<binder::cmd_option_bindt<
516 #if defined(UTIL_CONFIG_COMPILER_CXX_VARIADIC_TEMPLATES) && UTIL_CONFIG_COMPILER_CXX_VARIADIC_TEMPLATES
517  binder::cmd_option_bind_param_list<cmd_option_bind<TCmdStr> *>
518 #else
520 #endif
521  > >
522  bind_help_cmd(const char *help_cmd_content) {
523  return bind_cmd(help_cmd_content, &cmd_option_bind<TCmdStr>::on_help, this);
524  }
525 
529  virtual void operator()(callback_param arg) {
530  // 响应@OnCallFunc事件
531  typename funmap_type::const_iterator iter = callback_funcs_.find("@OnCallFunc");
532  if (iter != callback_funcs_.end()) (*iter->second)(arg);
533 
534  // 重新执行指令集, 进入子结构的一定是单指令
535  start(arg, true);
536  }
537 
542  virtual std::string get_help_msg(const char *prefix_data = "") const {
543  std::set<typename funmap_type::mapped_type> set_obj;
544  std::string help_msg_content;
545 
546  for (typename funmap_type::const_iterator iter = callback_funcs_.begin(); iter != callback_funcs_.end(); ++iter) {
547  // 删除重复的引用对象
548  if (set_obj.find(iter->second) != set_obj.end()) continue;
549 
550  // 跳过内置命令
551  if ('@' == *iter->first.c_str()) continue;
552 
553  set_obj.insert(iter->second);
554  std::string cmd_help = iter->second->get_help_msg((prefix_data + help_msg_).c_str());
555 
556  if (cmd_help.size() > 0) {
557  if (help_msg_content.size() > 0 && '\n' != *help_msg_content.rbegin()) help_msg_content += "\r\n";
558  help_msg_content += cmd_help;
559  }
560  }
561  return help_msg_content;
562  }
563 
581 #if defined(UTIL_CONFIG_COMPILER_CXX_VARIADIC_TEMPLATES) && UTIL_CONFIG_COMPILER_CXX_VARIADIC_TEMPLATES
582  template <typename _F, typename... _Args> // 绑定函数(_Arg:参数[注意值的复制发生在本函数执行时], _R: 绑定函数返回值类型)
583  std::shared_ptr<binder::cmd_option_bindt<typename binder::maybe_wrap_member_pointer<_F>::caller_type,
584  binder::cmd_option_bind_param_list<_Args...> > >
585  bind_cmd(const std::string &cmd_content, _F raw_fn, _Args... args) {
586  typedef binder::cmd_option_bind_param_list<_Args...> list_type;
587  typedef typename binder::maybe_wrap_member_pointer<_F>::caller_type caller_type;
588  typedef std::shared_ptr<binder::cmd_option_bindt<caller_type, list_type> > obj_type;
589 
590  obj_type fn = obj_type(new binder::cmd_option_bindt<caller_type, list_type>(caller_type(raw_fn), list_type(args...)));
591 
592  std::vector<std::string> cmds = split_cmd(cmd_content.c_str());
593  for (std::vector<std::string>::size_type index = 0; index < cmds.size(); ++index) {
594  TCmdStr cmd_obj = TCmdStr(cmds[index].c_str(), cmds[index].size());
595  callback_funcs_[cmd_obj] = fn;
596  }
597 
598  return fn;
599  }
600 #else
601  template <typename _F> // 绑定函数(_F: 函数对象类型)
602  std::shared_ptr<
604  bind_cmd(const std::string &cmd_content, _F raw_fn) {
605  typedef binder::cmd_option_bind_param_list0 list_type;
606  typedef typename binder::maybe_wrap_member_pointer<_F>::caller_type caller_type;
607  typedef std::shared_ptr<binder::cmd_option_bindt<caller_type, list_type> > obj_type;
608 
609  obj_type fn = obj_type(new binder::cmd_option_bindt<caller_type, list_type>(caller_type(raw_fn), list_type()));
610 
611  std::vector<std::string> cmds = split_cmd(cmd_content.c_str());
612  for (std::vector<std::string>::size_type index = 0; index < cmds.size(); ++index) {
613  TCmdStr cmd_obj = TCmdStr(cmds[index].c_str(), cmds[index].size());
614  callback_funcs_[cmd_obj] = fn;
615  }
616 
617  return fn;
618  }
619 
620  template <typename _F, typename _Arg0> // 绑定函数(_Arg:参数[注意值的复制发生在本函数执行时], _R: 绑定函数返回值类型)
621  std::shared_ptr<binder::cmd_option_bindt<typename binder::maybe_wrap_member_pointer<_F>::caller_type,
623  bind_cmd(const std::string &cmd_content, _F raw_fn, _Arg0 arg0) {
625  typedef typename binder::maybe_wrap_member_pointer<_F>::caller_type caller_type;
626  typedef std::shared_ptr<binder::cmd_option_bindt<caller_type, list_type> > obj_type;
627 
628  obj_type fn = obj_type(new binder::cmd_option_bindt<caller_type, list_type>(caller_type(raw_fn), list_type(arg0)));
629 
630  std::vector<std::string> cmds = split_cmd(cmd_content.c_str());
631  for (std::vector<std::string>::size_type index = 0; index < cmds.size(); ++index) {
632  TCmdStr cmd_obj = TCmdStr(cmds[index].c_str(), cmds[index].size());
633  callback_funcs_[cmd_obj] = fn;
634  }
635 
636  return fn;
637  }
638 
639  template <typename _F, typename _Arg0,
640  typename _Arg1> // 绑定函数(_Arg:参数[注意值的复制发生在本函数执行时], _R: 绑定函数返回值类型)
641  std::shared_ptr<binder::cmd_option_bindt<typename binder::maybe_wrap_member_pointer<_F>::caller_type,
643  bind_cmd(const std::string &cmd_content, _F raw_fn, _Arg0 arg0, _Arg1 arg1) {
645  typedef typename binder::maybe_wrap_member_pointer<_F>::caller_type caller_type;
646  typedef std::shared_ptr<binder::cmd_option_bindt<caller_type, list_type> > obj_type;
647 
648  obj_type fn = obj_type(new binder::cmd_option_bindt<caller_type, list_type>(caller_type(raw_fn), list_type(arg0, arg1)));
649 
650  std::vector<std::string> cmds = split_cmd(cmd_content.c_str());
651  for (std::vector<std::string>::size_type index = 0; index < cmds.size(); ++index) {
652  TCmdStr cmd_obj = TCmdStr(cmds[index].c_str(), cmds[index].size());
653  callback_funcs_[cmd_obj] = fn;
654  }
655 
656  return fn;
657  }
658 
659  template <typename _F, typename _Arg0, typename _Arg1,
660  typename _Arg2> // 绑定函数(_Arg:参数[注意值的复制发生在本函数执行时], _R: 绑定函数返回值类型)
661  std::shared_ptr<binder::cmd_option_bindt<typename binder::maybe_wrap_member_pointer<_F>::caller_type,
663  bind_cmd(const std::string &cmd_content, _F raw_fn, _Arg0 arg0, _Arg1 arg1, _Arg2 arg2) {
665  typedef typename binder::maybe_wrap_member_pointer<_F>::caller_type caller_type;
666  typedef std::shared_ptr<binder::cmd_option_bindt<caller_type, list_type> > obj_type;
667 
668  obj_type fn =
669  obj_type(new binder::cmd_option_bindt<caller_type, list_type>(caller_type(raw_fn), list_type(arg0, arg1, arg2)));
670 
671  std::vector<std::string> cmds = split_cmd(cmd_content.c_str());
672  for (std::vector<std::string>::size_type index = 0; index < cmds.size(); ++index) {
673  TCmdStr cmd_obj = TCmdStr(cmds[index].c_str(), cmds[index].size());
674  callback_funcs_[cmd_obj] = fn;
675  }
676 
677  return fn;
678  }
679 
680  template <typename _F, typename _Arg0, typename _Arg1, typename _Arg2,
681  typename _Arg3> // 绑定函数(_Arg:参数[注意值的复制发生在本函数执行时], _R: 绑定函数返回值类型)
682  std::shared_ptr<binder::cmd_option_bindt<typename binder::maybe_wrap_member_pointer<_F>::caller_type,
684  bind_cmd(const std::string &cmd_content, _F raw_fn, _Arg0 arg0, _Arg1 arg1, _Arg2 arg2, _Arg3 arg3) {
686  typedef typename binder::maybe_wrap_member_pointer<_F>::caller_type caller_type;
687  typedef std::shared_ptr<binder::cmd_option_bindt<caller_type, list_type> > obj_type;
688 
689  obj_type fn =
690  obj_type(new binder::cmd_option_bindt<caller_type, list_type>(caller_type(raw_fn), list_type(arg0, arg1, arg2, arg3)));
691 
692  std::vector<std::string> cmds = split_cmd(cmd_content.c_str());
693  for (std::vector<std::string>::size_type index = 0; index < cmds.size(); ++index) {
694  TCmdStr cmd_obj = TCmdStr(cmds[index].c_str(), cmds[index].size());
695  callback_funcs_[cmd_obj] = fn;
696  }
697 
698  return fn;
699  }
700 #endif
701 
710  std::shared_ptr<binder::cmd_option_bind_base> bind_child_cmd(const std::string cmd_content,
711  std::shared_ptr<binder::cmd_option_bind_base> base_node) {
712  std::vector<std::string> cmds = split_cmd(cmd_content.c_str());
713  for (std::vector<std::string>::size_type index = 0; index < cmds.size(); ++index) {
714  TCmdStr cmd_obj = TCmdStr(cmds[index].c_str(), cmds[index].size());
715  callback_funcs_[cmd_obj] = base_node;
716  callback_children_[cmd_obj] = base_node;
717  }
718 
719  return base_node;
720  }
721 
722  std::shared_ptr<binder::cmd_option_bind_base> bind_child_cmd(const std::string cmd_content, ptr_type cmd_opt) {
723  std::shared_ptr<binder::cmd_option_bind_base> base_node = std::dynamic_pointer_cast<binder::cmd_option_bind_base>(cmd_opt);
724  std::vector<std::string> cmds = split_cmd(cmd_content.c_str());
725  for (std::vector<std::string>::size_type index = 0; index < cmds.size(); ++index) {
726  TCmdStr cmd_obj = TCmdStr(cmds[index].c_str(), cmds[index].size());
727  callback_funcs_[cmd_obj] = base_node;
728  callback_children_[cmd_obj] = base_node;
729  }
730 
731  return base_node;
732  }
733  };
734 
735  template <typename Ty>
736  short cmd_option_bind<Ty>::map_value_[256] = {0};
737 
738  template <typename Ty>
739  char cmd_option_bind<Ty>::trans_value_[256] = {0};
740 
741  // 类型重定义
744  } // namespace cli
745 } // namespace util
746 #endif /* CMDOPTION_H_ */
int get_help_cmd_style() const
Definition: cmd_option.h:365
std::shared_ptr< binder::cmd_option_bindt< typename binder::maybe_wrap_member_pointer< _F >::caller_type, binder::cmd_option_bind_param_list1< _Arg0 > > > bind_cmd(const std::string &cmd_content, _F raw_fn, _Arg0 arg0)
Definition: cmd_option.h:623
void unbind_cmd(const std::string &cmd_content)
Definition: cmd_option.h:496
void start(const std::string &cmd_content, bool is_single_cmd=false, void *ext_param=NULL) const
Definition: cmd_option.h:488
static short map_value_[256]
Definition: cmd_option.h:80
size_t children_size() const
Definition: cmd_option.h:361
std::shared_ptr< cmd_option_bind_base > binded_obj
const cmd_array_type & get_cmd_array() const
void set_help_description_style(int style)
Definition: cmd_option.h:368
size_t children_empty() const
Definition: cmd_option.h:362
const funmap_type & get_all() const
Definition: cmd_option.h:359
void start(const char *cmd_content, bool is_single_cmd=false, void *ext_param=NULL) const
Definition: cmd_option.h:468
virtual void operator()(callback_param arg)
Definition: cmd_option.h:529
std::shared_ptr< binder::cmd_option_bind_base > bind_child_cmd(const std::string cmd_content, ptr_type cmd_opt)
Definition: cmd_option.h:722
导入高级库,hash、引用包装等 Licensed under the MIT licenses.
size_type get_params_number() const
std::shared_ptr< binder::cmd_option_bindt< typename binder::maybe_wrap_member_pointer< void(cmd_option_bind< TCmdStr >::*)(callback_param)>::caller_type, binder::cmd_option_bind_param_list1< cmd_option_bind< TCmdStr > * > > > bind_help_cmd(const char *help_cmd_content)
Definition: cmd_option.h:522
virtual std::string get_help_msg(const char *prefix_data="") const
Definition: cmd_option.h:542
std::vector< help_msg_t > help_list_t
cmd_option_bind< cmd_option_ci_string > cmd_option_ci
Definition: cmd_option.h:743
std::shared_ptr< binder::cmd_option_bindt< typename binder::maybe_wrap_member_pointer< _F >::caller_type, binder::cmd_option_bind_param_list4< _Arg0, _Arg1, _Arg2, _Arg3 > > > bind_cmd(const std::string &cmd_content, _F raw_fn, _Arg0 arg0, _Arg1 arg1, _Arg2 arg2, _Arg3 arg3)
Definition: cmd_option.h:684
std::shared_ptr< cmd_option_bind > ptr_type
Definition: cmd_option.h:328
void set_ext_param(void *param)
funmap_type callback_funcs_
Definition: cmd_option.h:83
void start(int argv, char *argc[], bool is_single_cmd=false, void *ext_param=NULL) const
Definition: cmd_option.h:444
void load_cmd_array(const cmd_array_type &cmds)
void set_help_cmd_style(int style)
Definition: cmd_option.h:366
funmap_type callback_children_
Definition: cmd_option.h:84
static std::ostream & dump(std::ostream &os, const self_type &self, const std::string &prefix)
Definition: cmd_option.h:173
std::shared_ptr< binder::cmd_option_bindt< typename binder::maybe_wrap_member_pointer< _F >::caller_type, binder::cmd_option_bind_param_list3< _Arg0, _Arg1, _Arg2 > > > bind_cmd(const std::string &cmd_content, _F raw_fn, _Arg0 arg0, _Arg1 arg1, _Arg2 arg2)
Definition: cmd_option.h:663
void append_cmd(const char *cmd_content, std::shared_ptr< binder::cmd_option_bind_base > base_node)
void add(const char *param)
void on_help(callback_param)
Definition: cmd_option.h:124
std::shared_ptr< binder::cmd_option_bindt< typename binder::maybe_wrap_member_pointer< _F >::caller_type, binder::cmd_option_bind_param_list2< _Arg0, _Arg1 > > > bind_cmd(const std::string &cmd_content, _F raw_fn, _Arg0 arg0, _Arg1 arg1)
Definition: cmd_option.h:643
void list_help_msg(help_list_t &msg, const std::string &prefix) const
Definition: cmd_option.h:130
std::shared_ptr< binder::cmd_option_bind_base > bind_child_cmd(const std::string cmd_content, std::shared_ptr< binder::cmd_option_bind_base > base_node)
Definition: cmd_option.h:710
binder::cmd_option_bind_base::help_list_t help_list_t
Definition: cmd_option.h:75
std::shared_ptr< binder::cmd_option_bind_base > func_ptr_t
Definition: cmd_option.h:76
const funmap_type & get_all_children() const
Definition: cmd_option.h:363
static ptr_type create()
Definition: cmd_option.h:329
导入智能指针库 Licensed under the MIT licenses.
void start(callback_param args, bool is_single_cmd=false) const
Definition: cmd_option.h:384
static bool sort_by_all_cmds(const help_msg_t &l, const help_msg_t &r)
static char trans_value_[256]
Definition: cmd_option.h:81
void run_cmd(const TCmdStr &cmd_content, callback_param params) const
Definition: cmd_option.h:93
static const char * get_segment(const char *begin_str, std::string &val)
Definition: cmd_option.h:225
cmd_option_bind< std::string > cmd_option
Definition: cmd_option.h:742
binder::cmd_option_bind_base::help_msg_t help_msg_t
Definition: cmd_option.h:74
void start(const std::vector< std::string > &cmds, bool is_single_cmd=false, void *ext_param=NULL) const
Definition: cmd_option.h:456
void start(int argv, const char *argc[], bool is_single_cmd=false, void *ext_param=NULL) const
Definition: cmd_option.h:430
std::shared_ptr< std::vector< const char * > > get_cmd_names() const
Definition: cmd_option.h:335
std::map< TCmdStr, func_ptr_t > funmap_type
Definition: cmd_option.h:77
size_t empty() const
Definition: cmd_option.h:358
std::shared_ptr< binder::cmd_option_bindt< typename binder::maybe_wrap_member_pointer< _F >::caller_type, binder::cmd_option_bind_param_list0 > > bind_cmd(const std::string &cmd_content, _F raw_fn)
Definition: cmd_option.h:604
std::shared_ptr< binder::cmd_option_bind_base > get_binded_cmd(const char *cmd_name) const
Definition: cmd_option.h:351
std::shared_ptr< cli::cmd_option_value > value_type
Definition: cmd_option.h:53
int get_help_description_style() const
Definition: cmd_option.h:367
cmd_option_bind< TCmdStr > self_type
Definition: cmd_option.h:73
std::ostream & dump(std::ostream &os, const std::string &prefix)
Definition: cmd_option.h:217
friend std::ostream & operator<<(std::ostream &os, const self_type &self)
Definition: cmd_option.h:215
size_t size() const
Definition: cmd_option.h:357
static std::vector< std::string > split_cmd(const char *begin_str)
Definition: cmd_option.h:270