libcopp  2.2.0
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Member Functions | Friends
util::cli::cmd_option_bind< TCmdStr > Class Template Reference

#include <cmd_option.h>

Inheritance diagram for util::cli::cmd_option_bind< TCmdStr >:
Inheritance graph
Collaboration diagram for util::cli::cmd_option_bind< TCmdStr >:
Collaboration graph

Public Types

typedef unsigned char uc_t
 
typedef cmd_option_bind< TCmdStr > self_type
 
typedef binder::cmd_option_bind_base::help_msg_t help_msg_t
 
typedef binder::cmd_option_bind_base::help_list_t help_list_t
 
typedef std::shared_ptr< binder::cmd_option_bind_basefunc_ptr_t
 
typedef std::map< TCmdStr, func_ptr_tfunmap_type
 
typedef std::shared_ptr< cmd_option_bindptr_type
 
typedef callback_param param_type
 

Public Member Functions

void list_help_msg (help_list_t &msg, const std::string &prefix) const
 
std::ostream & dump (std::ostream &os, const std::string &prefix)
 
std::shared_ptr< std::vector< const char * > > get_cmd_names () const
 
std::shared_ptr< binder::cmd_option_bind_baseget_binded_cmd (const char *cmd_name) const
 
size_t size () const
 
size_t empty () const
 
const funmap_typeget_all () const
 
size_t children_size () const
 
size_t children_empty () const
 
const funmap_typeget_all_children () const
 
int get_help_cmd_style () const
 
void set_help_cmd_style (int style)
 
int get_help_description_style () const
 
void set_help_description_style (int style)
 
void start (callback_param args, bool is_single_cmd=false) const
 
void start (int argv, const char *argc[], bool is_single_cmd=false, void *ext_param=nullptr) const
 
void start (int argv, char *argc[], bool is_single_cmd=false, void *ext_param=nullptr) const
 
void start (const std::vector< std::string > &cmds, bool is_single_cmd=false, void *ext_param=nullptr) const
 
void start (const char *cmd_content, bool is_single_cmd=false, void *ext_param=nullptr) const
 
void start (const std::string &cmd_content, bool is_single_cmd=false, void *ext_param=nullptr) const
 
void unbind_cmd (const std::string &cmd_content)
 
void unbind_all_cmd ()
 
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)
 
virtual void operator() (callback_param arg)
 
virtual std::string get_help_msg (const char *prefix_data="") const
 
template<typename _F , typename... _Args>
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)
 
std::shared_ptr< binder::cmd_option_bind_basebind_child_cmd (const std::string cmd_content, std::shared_ptr< binder::cmd_option_bind_base > base_node)
 
std::shared_ptr< binder::cmd_option_bind_basebind_child_cmd (const std::string cmd_content, ptr_type cmd_opt)
 
virtual std::shared_ptr< cmd_option_bind_base > set_help_msg (const char *help_msg)
 
virtual std::shared_ptr< cmd_option_bind_base > add_help_msg (const char *help_msg)
 

Static Public Member Functions

static std::ostream & dump (std::ostream &os, const self_type &self, const std::string &prefix)
 
static const char * get_segment (const char *begin_str, std::string &val)
 
static std::vector< std::string > split_cmd (const char *begin_str)
 
static ptr_type create ()
 

Protected Member Functions

void run_cmd (const TCmdStr &cmd_content, callback_param params) const
 
void on_help (callback_param)
 

Static Protected Member Functions

static bool sort_by_all_cmds (const help_msg_t &l, const help_msg_t &r)
 

Protected Attributes

funmap_type callback_funcs_
 
funmap_type callback_children_
 
int help_cmd_style_
 
int help_description_style_
 
std::string help_msg_
 

Static Protected Attributes

static short map_value_ [256] = {0}
 
static char trans_value_ [256] = {0}
 
static const char * ROOT_NODE_CMD = "@Root"
 

Private Member Functions

 cmd_option_bind ()
 

Friends

std::ostream & operator<< (std::ostream &os, const self_type &self)
 

Detailed Description

template<typename TCmdStr>
class util::cli::cmd_option_bind< TCmdStr >

命令处理函数 内定命令/变量列表(用于处理内部事件): @OnError : 出错时触发 @ErrorMsg : @OnError 函数的错误名称

@OnDefault : 默认执行函数(用于执行批量命令时的第一个指令前的参数) 注意:如果第一个参数就是指令则@OnDefault会被传入空参数执行

@OnCallFunc : 分离参数后,转入命令前(将传入所有参数,仅限于子cmd_option_bind<TCmdStr>执行时) 建议:可以在这个事件响应函数里再绑定其他命令,减少指令冗余 [注: 调用start函数不会响应这个事件]

Definition at line 66 of file cmd_option.h.

Member Typedef Documentation

◆ func_ptr_t

template<typename TCmdStr >
typedef std::shared_ptr<binder::cmd_option_bind_base> util::cli::cmd_option_bind< TCmdStr >::func_ptr_t

Definition at line 72 of file cmd_option.h.

◆ funmap_type

template<typename TCmdStr >
typedef std::map<TCmdStr, func_ptr_t> util::cli::cmd_option_bind< TCmdStr >::funmap_type

Definition at line 73 of file cmd_option.h.

◆ help_list_t

template<typename TCmdStr >
typedef binder::cmd_option_bind_base::help_list_t util::cli::cmd_option_bind< TCmdStr >::help_list_t

Definition at line 71 of file cmd_option.h.

◆ help_msg_t

template<typename TCmdStr >
typedef binder::cmd_option_bind_base::help_msg_t util::cli::cmd_option_bind< TCmdStr >::help_msg_t

Definition at line 70 of file cmd_option.h.

◆ param_type

Definition at line 50 of file cmd_option_list.h.

◆ ptr_type

template<typename TCmdStr >
typedef std::shared_ptr<cmd_option_bind> util::cli::cmd_option_bind< TCmdStr >::ptr_type

Definition at line 325 of file cmd_option.h.

◆ self_type

template<typename TCmdStr >
typedef cmd_option_bind<TCmdStr> util::cli::cmd_option_bind< TCmdStr >::self_type

Definition at line 69 of file cmd_option.h.

◆ uc_t

template<typename TCmdStr >
typedef unsigned char util::cli::cmd_option_bind< TCmdStr >::uc_t

Definition at line 68 of file cmd_option.h.

Constructor & Destructor Documentation

◆ cmd_option_bind()

template<typename TCmdStr >
util::cli::cmd_option_bind< TCmdStr >::cmd_option_bind ( )
inlineprivate

Member Function Documentation

◆ add_help_msg()

virtual std::shared_ptr<cmd_option_bind_base> util::cli::binder::cmd_option_bind_base::add_help_msg ( const char *  help_msg)
inlinevirtualinherited

◆ bind_child_cmd() [1/2]

template<typename TCmdStr >
std::shared_ptr<binder::cmd_option_bind_base> util::cli::cmd_option_bind< TCmdStr >::bind_child_cmd ( const std::string  cmd_content,
ptr_type  cmd_opt 
)
inline

◆ bind_child_cmd() [2/2]

template<typename TCmdStr >
std::shared_ptr<binder::cmd_option_bind_base> util::cli::cmd_option_bind< TCmdStr >::bind_child_cmd ( const std::string  cmd_content,
std::shared_ptr< binder::cmd_option_bind_base base_node 
)
inline

绑定指令(通用) bind_cmd: 绑定参数 example: *.bind_cmd(命令名称, binder::cmd_option_bind_base 结构智能指针) *.bind_cmd(命令名称, cmd_option_bind<TCmdStr> 结构引用) 推荐使用上一种,可以减少一次结构复制

Definition at line 601 of file cmd_option.h.

References util::cli::cmd_option_bind< TCmdStr >::callback_children_, util::cli::cmd_option_bind< TCmdStr >::callback_funcs_, util::cli::cmd_option_bind< TCmdStr >::size(), and util::cli::cmd_option_bind< TCmdStr >::split_cmd().

◆ bind_cmd()

template<typename TCmdStr >
template<typename _F , typename... _Args>
std::shared_ptr<binder::cmd_option_bindt<typename binder::maybe_wrap_member_pointer<_F>::caller_type, binder::cmd_option_bind_param_list<_Args...> > > util::cli::cmd_option_bind< TCmdStr >::bind_cmd ( const std::string &  cmd_content,
_F  raw_fn,
_Args...  args 
)
inline

增加指令处理函数 (相同命令会被覆盖) 支持普通函数和类成员函数 注意:所有传入的类为引用,请确保在执行start时类对象未被释放(特别注意指针和局部变量) 注意:参数的复制发生在执行bind_cmd函数时 绑定函数对象/函数/成员函数(自适应) 注意:默认会复制函数对象和传入参数

bind_cmd: 绑定参数[注意值的复制发生在本函数执行时] example: *.bind_cmd(命令名称, 函数对象/函数/成员函数, 参数) // 默认类型推断是传值而非引用 *.bind_cmd<传入类型>(命令名称, 函数对象/函数/成员函数, 参数) *.bind_cmd<传入类型, 参数类型>(命令名称, 函数对象/函数/成员函数, 参数)

Definition at line 576 of file cmd_option.h.

References util::cli::cmd_option_bind< TCmdStr >::callback_funcs_, util::cli::cmd_option_bind< TCmdStr >::size(), and util::cli::cmd_option_bind< TCmdStr >::split_cmd().

Referenced by util::cli::cmd_option_bind< TCmdStr >::bind_help_cmd().

◆ bind_help_cmd()

template<typename TCmdStr >
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> *> > > util::cli::cmd_option_bind< TCmdStr >::bind_help_cmd ( const char *  help_cmd_content)
inline

绑定默认帮助函数

Parameters
help_cmd_content帮助命令名称

Definition at line 513 of file cmd_option.h.

References util::cli::cmd_option_bind< TCmdStr >::bind_cmd().

◆ children_empty()

template<typename TCmdStr >
size_t util::cli::cmd_option_bind< TCmdStr >::children_empty ( ) const
inline

◆ children_size()

template<typename TCmdStr >
size_t util::cli::cmd_option_bind< TCmdStr >::children_size ( ) const
inline

◆ create()

template<typename TCmdStr >
static ptr_type util::cli::cmd_option_bind< TCmdStr >::create ( )
inlinestatic

Definition at line 326 of file cmd_option.h.

References util::cli::cmd_option_bind< TCmdStr >::cmd_option_bind().

Referenced by run_tests().

◆ dump() [1/2]

template<typename TCmdStr >
static std::ostream& util::cli::cmd_option_bind< TCmdStr >::dump ( std::ostream &  os,
const self_type self,
const std::string &  prefix 
)
inlinestatic

◆ dump() [2/2]

template<typename TCmdStr >
std::ostream& util::cli::cmd_option_bind< TCmdStr >::dump ( std::ostream &  os,
const std::string &  prefix 
)
inline

◆ empty()

template<typename TCmdStr >
size_t util::cli::cmd_option_bind< TCmdStr >::empty ( ) const
inline

◆ get_all()

template<typename TCmdStr >
const funmap_type& util::cli::cmd_option_bind< TCmdStr >::get_all ( ) const
inline

◆ get_all_children()

template<typename TCmdStr >
const funmap_type& util::cli::cmd_option_bind< TCmdStr >::get_all_children ( ) const
inline

◆ get_binded_cmd()

template<typename TCmdStr >
std::shared_ptr<binder::cmd_option_bind_base> util::cli::cmd_option_bind< TCmdStr >::get_binded_cmd ( const char *  cmd_name) const
inline

获取已绑定的指令对象

Parameters
cmd_name指令名称
Returns
绑定的指令对象指针, 未找到返回空指针指针

Definition at line 348 of file cmd_option.h.

References util::cli::cmd_option_bind< TCmdStr >::callback_funcs_.

◆ get_cmd_names()

template<typename TCmdStr >
std::shared_ptr<std::vector<const char *> > util::cli::cmd_option_bind< TCmdStr >::get_cmd_names ( ) const
inline

获取已绑定的指令列表

Returns
指令列表指针

Definition at line 332 of file cmd_option.h.

References util::cli::cmd_option_bind< TCmdStr >::callback_funcs_.

◆ get_help_cmd_style()

template<typename TCmdStr >
int util::cli::cmd_option_bind< TCmdStr >::get_help_cmd_style ( ) const
inline

◆ get_help_description_style()

template<typename TCmdStr >
int util::cli::cmd_option_bind< TCmdStr >::get_help_description_style ( ) const
inline

◆ get_help_msg()

template<typename TCmdStr >
virtual std::string util::cli::cmd_option_bind< TCmdStr >::get_help_msg ( const char *  prefix_data = "") const
inlinevirtual

获取命令集合的帮助信息

Parameters
prefix_data前缀

Reimplemented from util::cli::binder::cmd_option_bind_base.

Definition at line 533 of file cmd_option.h.

References util::cli::cmd_option_bind< TCmdStr >::callback_funcs_, and util::cli::binder::cmd_option_bind_base::help_msg_.

◆ get_segment()

template<typename TCmdStr >
static const char* util::cli::cmd_option_bind< TCmdStr >::get_segment ( const char *  begin_str,
std::string &  val 
)
inlinestatic

从字符串获取一个字段(返回未处理的字符串结尾)

Parameters
begin_str要解析的字符串的起始位置
val解析结果
Returns
未解析部分的开始位置

Definition at line 225 of file cmd_option.h.

References util::cli::cmd_option_bind< TCmdStr >::map_value_, util::cli::SPLITCHAR, util::cli::STRINGSYM, util::cli::cmd_option_bind< TCmdStr >::trans_value_, and util::cli::TRANSLATE.

Referenced by util::cli::cmd_option_list::init_key_value_map(), and util::cli::cmd_option_bind< TCmdStr >::start().

◆ list_help_msg()

template<typename TCmdStr >
void util::cli::cmd_option_bind< TCmdStr >::list_help_msg ( help_list_t msg,
const std::string &  prefix 
) const
inline

◆ on_help()

template<typename TCmdStr >
void util::cli::cmd_option_bind< TCmdStr >::on_help ( callback_param  )
inlineprotected

默认帮助函数

Definition at line 120 of file cmd_option.h.

◆ operator()()

template<typename TCmdStr >
virtual void util::cli::cmd_option_bind< TCmdStr >::operator() ( callback_param  arg)
inlinevirtual

◆ run_cmd()

template<typename TCmdStr >
void util::cli::cmd_option_bind< TCmdStr >::run_cmd ( const TCmdStr &  cmd_content,
callback_param  params 
) const
inlineprotected

◆ set_help_cmd_style()

template<typename TCmdStr >
void util::cli::cmd_option_bind< TCmdStr >::set_help_cmd_style ( int  style)
inline

◆ set_help_description_style()

template<typename TCmdStr >
void util::cli::cmd_option_bind< TCmdStr >::set_help_description_style ( int  style)
inline

◆ set_help_msg()

virtual std::shared_ptr<cmd_option_bind_base> util::cli::binder::cmd_option_bind_base::set_help_msg ( const char *  help_msg)
inlinevirtualinherited

◆ size()

template<typename TCmdStr >
size_t util::cli::cmd_option_bind< TCmdStr >::size ( ) const
inline

◆ sort_by_all_cmds()

static bool util::cli::binder::cmd_option_bind_base::sort_by_all_cmds ( const help_msg_t l,
const help_msg_t r 
)
inlinestaticprotectedinherited

◆ split_cmd()

template<typename TCmdStr >
static std::vector<std::string> util::cli::cmd_option_bind< TCmdStr >::split_cmd ( const char *  begin_str)
inlinestatic

多指令分离

Parameters
begin_str源字符串
Returns
分离结果

Definition at line 268 of file cmd_option.h.

References util::cli::CMDSPLIT, and util::cli::cmd_option_bind< TCmdStr >::map_value_.

Referenced by util::cli::cmd_option_bind< TCmdStr >::bind_child_cmd(), and util::cli::cmd_option_bind< TCmdStr >::bind_cmd().

◆ start() [1/6]

template<typename TCmdStr >
void util::cli::cmd_option_bind< TCmdStr >::start ( callback_param  args,
bool  is_single_cmd = false 
) const
inline

处理指令 说明: 在第一个指令前的参数都将传入@OnDefault事件 参数可通过get[数组下标]获取 第一次使用get[字符串]时将构建参数映射表,get(0)为key,get(1)为value,get(2)为key,get(3)为value,依此类推 如果最后一组key没有value,执行get[key]将返回空指针 注意:get[偶数下标]对应的所有value值和get[字符串]返回的指针共享数据(即改了一个另一个也随之更改) 处理已分离指令(使用cmd_option_list存储参数集)

Parameters
args数据集合
is_single_cmd是否强制单指令, 如果不强制, 则指令名称不能重复

Definition at line 381 of file cmd_option.h.

References util::cli::cmd_option_list::add(), util::cli::cmd_option_bind< TCmdStr >::callback_funcs_, util::cli::cmd_option_list::clear(), util::cli::cmd_option_list::get_cmd_array(), util::cli::cmd_option_list::get_ext_param(), util::cli::cmd_option_list::get_params_number(), util::cli::cmd_option_list::load_cmd_array(), util::cli::cmd_option_bind< TCmdStr >::run_cmd(), util::cli::cmd_option_list::set_ext_param(), and util::cli::cmd_option_bind< TCmdStr >::size().

Referenced by util::cli::cmd_option_bind< TCmdStr >::operator()(), and util::cli::cmd_option_bind< TCmdStr >::start().

◆ start() [2/6]

template<typename TCmdStr >
void util::cli::cmd_option_bind< TCmdStr >::start ( const char *  cmd_content,
bool  is_single_cmd = false,
void *  ext_param = nullptr 
) const
inline

处理未分离指令(使用const char*存储参数集字符串)

Parameters
cmd_content指令
is_single_cmd是否强制单指令, 如果不强制, 则指令名称不能重复

Definition at line 464 of file cmd_option.h.

References util::cli::cmd_option_list::add(), util::cli::cmd_option_bind< TCmdStr >::get_segment(), util::cli::cmd_option_list::set_ext_param(), and util::cli::cmd_option_bind< TCmdStr >::start().

◆ start() [3/6]

template<typename TCmdStr >
void util::cli::cmd_option_bind< TCmdStr >::start ( const std::string &  cmd_content,
bool  is_single_cmd = false,
void *  ext_param = nullptr 
) const
inline

处理未分离指令(使用const std::string&存储参数集字符串)

Parameters
cmd_content指令
is_single_cmd是否强制单指令, 如果不强制, 则指令名称不能重复

Definition at line 484 of file cmd_option.h.

References util::cli::cmd_option_bind< TCmdStr >::start().

◆ start() [4/6]

template<typename TCmdStr >
void util::cli::cmd_option_bind< TCmdStr >::start ( const std::vector< std::string > &  cmds,
bool  is_single_cmd = false,
void *  ext_param = nullptr 
) const
inline

处理已分离指令(使用std::vector<std::string>存储参数集)

Parameters
cmds数据集合
is_single_cmd是否强制单指令, 如果不强制, 则指令名称不能重复
ext_param透传参数

Definition at line 452 of file cmd_option.h.

References util::cli::cmd_option_list::set_ext_param(), and util::cli::cmd_option_bind< TCmdStr >::start().

◆ start() [5/6]

template<typename TCmdStr >
void util::cli::cmd_option_bind< TCmdStr >::start ( int  argv,
char *  argc[],
bool  is_single_cmd = false,
void *  ext_param = nullptr 
) const
inline

处理已分离指令(使用char**存储参数集)

Parameters
argv参数个数
argc参数列表
is_single_cmd是否强制单指令, 如果不强制, 则指令名称不能重复
ext_param透传参数

Definition at line 440 of file cmd_option.h.

References util::cli::cmd_option_bind< TCmdStr >::start().

◆ start() [6/6]

template<typename TCmdStr >
void util::cli::cmd_option_bind< TCmdStr >::start ( int  argv,
const char *  argc[],
bool  is_single_cmd = false,
void *  ext_param = nullptr 
) const
inline

处理已分离指令(使用char**存储参数集)

Parameters
argv参数个数
argc参数列表
is_single_cmd是否强制单指令, 如果不强制, 则指令名称不能重复
ext_param透传参数

Definition at line 426 of file cmd_option.h.

References util::cli::cmd_option_list::set_ext_param(), and util::cli::cmd_option_bind< TCmdStr >::start().

◆ unbind_all_cmd()

template<typename TCmdStr >
void util::cli::cmd_option_bind< TCmdStr >::unbind_all_cmd ( )
inline

◆ unbind_cmd()

template<typename TCmdStr >
void util::cli::cmd_option_bind< TCmdStr >::unbind_cmd ( const std::string &  cmd_content)
inline

解绑指令

Parameters
cmd_content指令名称

Definition at line 492 of file cmd_option.h.

References util::cli::cmd_option_bind< TCmdStr >::callback_children_, and util::cli::cmd_option_bind< TCmdStr >::callback_funcs_.

Friends And Related Function Documentation

◆ operator<<

template<typename TCmdStr >
std::ostream& operator<< ( std::ostream &  os,
const self_type self 
)
friend

Definition at line 215 of file cmd_option.h.

Field Documentation

◆ callback_children_

template<typename TCmdStr >
funmap_type util::cli::cmd_option_bind< TCmdStr >::callback_children_
protected

◆ callback_funcs_

template<typename TCmdStr >
funmap_type util::cli::cmd_option_bind< TCmdStr >::callback_funcs_
protected

◆ help_cmd_style_

template<typename TCmdStr >
int util::cli::cmd_option_bind< TCmdStr >::help_cmd_style_
protected

◆ help_description_style_

template<typename TCmdStr >
int util::cli::cmd_option_bind< TCmdStr >::help_description_style_
protected

◆ help_msg_

std::string util::cli::binder::cmd_option_bind_base::help_msg_
protectedinherited

◆ map_value_

template<typename Ty >
short util::cli::cmd_option_bind< Ty >::map_value_ = {0}
staticprotected

◆ ROOT_NODE_CMD

const char * util::cli::binder::cmd_option_bind_base::ROOT_NODE_CMD = "@Root"
staticprotectedinherited

Definition at line 34 of file cmd_option_list.h.

Referenced by util::cli::cmd_option_bind< TCmdStr >::run_cmd().

◆ trans_value_

template<typename Ty >
char util::cli::cmd_option_bind< Ty >::trans_value_ = {0}
staticprotected

The documentation for this class was generated from the following file: