1 #ifndef UTIL_CLI_CMDOPTION_H
2 #define UTIL_CLI_CMDOPTION_H
50 typedef std::shared_ptr<cli::cmd_option_value>
value_type;
65 template <
typename TCmdStr>
68 typedef unsigned char uc_t;
72 typedef std::shared_ptr<binder::cmd_option_bind_base>
func_ptr_t;
90 typename funmap_type::const_iterator iter =
callback_funcs_.find(cmd_content);
99 if (cmd_content ==
"@OnDefault")
return;
104 params.
add(
"@ErrorMsg=Command Invalid");
105 params.
append_cmd(cmd_content.c_str(), iter->second);
107 (*iter->second)(params);
113 params.
append_cmd(cmd_content.c_str(), iter->second);
114 (*iter->second)(params);
121 std::cout <<
"Help:" << std::endl;
122 std::cout << (*this);
128 if (iter->first.empty() ||
'@' == iter->first[0]) {
132 help_list_t::iterator iter_m;
134 for (iter_m = msg.begin(), obj =
nullptr; iter_m != msg.end(); ++iter_m) {
135 if ((*iter_m).binded_obj == iter->second) {
143 #if defined(LIBCOPP_MACRO_ENABLE_RTTI) && LIBCOPP_MACRO_ENABLE_RTTI
149 child->
list_help_msg(msg, (prefix +
" ") + iter->first.c_str());
153 if (
nullptr == obj) {
162 std::string full_cmd;
163 full_cmd.reserve(prefix.capacity() + iter->first.capacity());
164 if (!prefix.empty()) {
168 full_cmd += iter->first.c_str();
173 static std::ostream &
dump(std::ostream &os,
const self_type &
self,
const std::string &prefix) {
175 self.list_help_msg(msgs,
"");
177 size_t cmd_padding = 4;
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();
192 (*iter).all_cmds = ss.str();
193 if ((*iter).all_cmds.size() >= cmd_padding) {
194 cmd_padding = (((*iter).all_cmds.size() >> 2) + 1) << 2;
200 for (help_list_t::iterator iter = msgs.begin(); iter != msgs.end(); ++iter) {
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;
217 std::ostream &
dump(std::ostream &os,
const std::string &prefix) {
return dump(os, *
this, prefix); }
225 static const char *
get_segment(
const char *begin_str, std::string &val) {
240 while (*begin_str && *begin_str != flag) {
241 char cur_byte = *begin_str;
243 if (*(begin_str + 1)) {
268 static std::vector<std::string>
split_cmd(
const char *begin_str) {
269 std::vector<std::string> ret;
270 for (
const char *begin_ptr = begin_str; (*begin_ptr);) {
271 std::string cmd_content;
277 cmd_content.push_back(*begin_ptr);
281 if (cmd_content.size() > 0) ret.push_back(cmd_content);
334 std::shared_ptr<std::vector<const char *> > ret_ptr =
335 std::shared_ptr<std::vector<const char *> >(
new std::vector<const char *>());
337 ret_ptr->push_back(iter->first.c_str());
348 std::shared_ptr<binder::cmd_option_bind_base>
get_binded_cmd(
const char *cmd_name)
const {
349 typename funmap_type::const_iterator iter =
callback_funcs_.find(cmd_name);
350 if (iter ==
callback_funcs_.end())
return std::shared_ptr<binder::cmd_option_bind_base>();
384 TCmdStr cmd_content = is_single_cmd ?
"@OnError" :
"@OnDefault";
385 for (
int i = -1; i < argv;) {
391 for (; i < argv; ++i) {
394 cmd_args.
add(args[i]->to_string());
398 run_cmd(cmd_content, cmd_args);
406 cmd_content = TCmdStr(args[i]->to_cpp_string().c_str(), args[i]->to_cpp_string().
size());
407 for (++i; i < argv; ++i) cmd_args.
add(args[i]->to_string());
413 run_cmd(cmd_content, cmd_args);
414 if (i >= argv)
break;
415 cmd_content = TCmdStr(args[i]->to_cpp_string().c_str(), args[i]->to_cpp_string().
size());
426 inline void start(
int argv,
const char *argc[],
bool is_single_cmd =
false,
void *ext_param =
nullptr)
const {
430 start(copt_list, is_single_cmd);
440 inline void start(
int argv,
char *argc[],
bool is_single_cmd =
false,
void *ext_param =
nullptr)
const {
441 typedef const char *conv_char_t;
443 start(argv,
const_cast<conv_char_t *
>(argc), is_single_cmd, ext_param);
452 inline void start(
const std::vector<std::string> &cmds,
bool is_single_cmd =
false,
void *ext_param =
nullptr)
const {
456 start(copt_list, is_single_cmd);
464 void start(
const char *cmd_content,
bool is_single_cmd =
false,
void *ext_param =
nullptr)
const {
469 while (*cmd_content) {
471 cmds.
add(seg.c_str());
476 start(cmds, is_single_cmd);
484 inline void start(
const std::string &cmd_content,
bool is_single_cmd =
false,
void *ext_param =
nullptr)
const {
485 start(cmd_content.c_str(), is_single_cmd, ext_param);
493 TCmdStr cmd_obj = TCmdStr(cmd_content.c_str(), cmd_content.size());
522 typename funmap_type::const_iterator iter =
callback_funcs_.find(
"@OnCallFunc");
534 std::set<typename funmap_type::mapped_type> set_obj;
535 std::string help_msg_content;
539 if (set_obj.find(iter->second) != set_obj.end())
continue;
542 if (
'@' == *iter->first.c_str())
continue;
544 set_obj.insert(iter->second);
545 std::string cmd_help = iter->second->get_help_msg((prefix_data +
help_msg_).c_str());
547 if (cmd_help.size() > 0) {
548 if (help_msg_content.size() > 0 &&
'\n' != *help_msg_content.rbegin()) help_msg_content +=
"\r\n";
549 help_msg_content += cmd_help;
552 return help_msg_content;
572 template <
typename _F,
574 std::shared_ptr<binder::cmd_option_bindt<typename binder::maybe_wrap_member_pointer<_F>::caller_type,
576 bind_cmd(
const std::string &cmd_content, _F raw_fn, _Args... args) {
579 typedef std::shared_ptr<binder::cmd_option_bindt<caller_type, list_type> > obj_type;
584 std::vector<std::string> cmds =
split_cmd(cmd_content.c_str());
585 for (std::vector<std::string>::size_type index = 0; index < cmds.size(); ++index) {
586 TCmdStr cmd_obj = TCmdStr(cmds[index].c_str(), cmds[index].
size());
602 const std::string cmd_content, std::shared_ptr<binder::cmd_option_bind_base> base_node) {
603 std::vector<std::string> cmds =
split_cmd(cmd_content.c_str());
604 for (std::vector<std::string>::size_type index = 0; index < cmds.size(); ++index) {
605 TCmdStr cmd_obj = TCmdStr(cmds[index].c_str(), cmds[index].
size());
614 #if defined(LIBATFRAME_UTILS_ENABLE_RTTI) && LIBATFRAME_UTILS_ENABLE_RTTI
615 std::shared_ptr<binder::cmd_option_bind_base> base_node =
616 std::dynamic_pointer_cast<binder::cmd_option_bind_base>(cmd_opt);
618 std::shared_ptr<binder::cmd_option_bind_base> base_node =
619 std::static_pointer_cast<binder::cmd_option_bind_base>(cmd_opt);
621 std::vector<std::string> cmds =
split_cmd(cmd_content.c_str());
622 for (std::vector<std::string>::size_type index = 0; index < cmds.size(); ++index) {
623 TCmdStr cmd_obj = TCmdStr(cmds[index].c_str(), cmds[index].
size());
632 template <
typename Ty>
635 template <
typename Ty>
static const char * ROOT_NODE_CMD
std::vector< help_msg_t > help_list_t
static bool sort_by_all_cmds(const help_msg_t &l, const help_msg_t &r)
const funmap_type & get_all_children() const
std::shared_ptr< binder::cmd_option_bind_base > bind_child_cmd(const std::string cmd_content, ptr_type cmd_opt)
static std::vector< std::string > split_cmd(const char *begin_str)
friend std::ostream & operator<<(std::ostream &os, const self_type &self)
size_t children_size() const
cmd_option_bind< TCmdStr > self_type
void start(const std::vector< std::string > &cmds, bool is_single_cmd=false, void *ext_param=nullptr) const
void set_help_cmd_style(int style)
funmap_type callback_funcs_
std::shared_ptr< cmd_option_bind > ptr_type
int help_description_style_
static std::ostream & dump(std::ostream &os, const self_type &self, const std::string &prefix)
void on_help(callback_param)
void run_cmd(const TCmdStr &cmd_content, callback_param params) const
void start(const char *cmd_content, bool is_single_cmd=false, void *ext_param=nullptr) const
void list_help_msg(help_list_t &msg, const std::string &prefix) const
size_t children_empty() const
virtual std::string get_help_msg(const char *prefix_data="") const
binder::cmd_option_bind_base::help_list_t help_list_t
std::ostream & dump(std::ostream &os, const std::string &prefix)
binder::cmd_option_bind_base::help_msg_t help_msg_t
std::shared_ptr< binder::cmd_option_bindt< typename binder::maybe_wrap_member_pointer< _F >::caller_type, binder::cmd_option_bind_param_list< _Args... > > > bind_cmd(const std::string &cmd_content, _F raw_fn, _Args... args)
static short map_value_[256]
void start(int argv, const char *argc[], bool is_single_cmd=false, void *ext_param=nullptr) const
std::shared_ptr< binder::cmd_option_bind_base > func_ptr_t
virtual void operator()(callback_param arg)
std::map< TCmdStr, func_ptr_t > funmap_type
void start(const std::string &cmd_content, bool is_single_cmd=false, void *ext_param=nullptr) const
std::shared_ptr< binder::cmd_option_bind_base > get_binded_cmd(const char *cmd_name) const
void start(callback_param args, bool is_single_cmd=false) const
static char trans_value_[256]
int get_help_description_style() const
void unbind_cmd(const std::string &cmd_content)
funmap_type callback_children_
int get_help_cmd_style() const
static const char * get_segment(const char *begin_str, std::string &val)
const funmap_type & get_all() const
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)
void set_help_description_style(int style)
void start(int argv, char *argc[], bool is_single_cmd=false, void *ext_param=nullptr) 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_list< cmd_option_bind< TCmdStr > * > > > bind_help_cmd(const char *help_cmd_content)
std::shared_ptr< std::vector< const char * > > get_cmd_names() const
void * get_ext_param() const
void append_cmd(const char *cmd_content, std::shared_ptr< binder::cmd_option_bind_base > base_node)
size_type get_params_number() const
void load_cmd_array(const cmd_array_type &cmds)
const cmd_array_type & get_cmd_array() const
void set_ext_param(void *param)
void add(const char *param)
cmd_option_bind< std::string > cmd_option
cmd_option_bind< cmd_option_ci_string > cmd_option_ci
std::shared_ptr< cli::cmd_option_value > value_type
cmd_option_list & callback_param
导入高级库,hash、引用包装等 Licensed under the MIT licenses.
std::shared_ptr< cmd_option_bind_base > binded_obj
std::vector< std::string > cmd_paths