libcopp 2.3.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cmd_option_list.h
Go to the documentation of this file.
1#ifndef CMDOPTIONLIST_H
2#define CMDOPTIONLIST_H
3
4#pragma once
5
6/*
7 * cmd_option_list.h
8 *
9 * Created on: 2011-12-29
10 * Author: OWenT
11 *
12 * 应用程序命令处理
13 *
14 */
15
16#include <map>
17#include <memory>
18#include <string>
19#include <vector>
20
22
23namespace util {
24namespace cli {
25class cmd_option_list;
27
28namespace binder {
29struct unspecified {};
30
31// 绑定器接口
32class cmd_option_bind_base : public std::enable_shared_from_this<cmd_option_bind_base> {
33 protected:
34 static const char *ROOT_NODE_CMD;
35 struct help_msg_t {
36 std::vector<std::string> cmd_paths;
37 std::string all_cmds;
38 std::string description;
39 std::shared_ptr<cmd_option_bind_base> binded_obj;
40 };
41 typedef std::vector<help_msg_t> help_list_t;
42
43 std::string help_msg_;
45
46 static bool sort_by_all_cmds(const help_msg_t &l, const help_msg_t &r) { return l.all_cmds < r.all_cmds; }
47
48 public:
49 // 定义参数类型
51
52 virtual void operator()(callback_param arg) = 0;
53
54 // 获取绑定器的帮助信息
55 virtual std::string get_help_msg(const char *prefix_data = "") const { return prefix_data + help_msg_; }
56
57 // 设置绑定器的帮助信息
58 virtual std::shared_ptr<cmd_option_bind_base> set_help_msg(const char *help_msg) {
59 help_msg_ = help_msg;
60 return shared_from_this();
61 }
62
63 // 增加绑定器的帮助信息
64 virtual std::shared_ptr<cmd_option_bind_base> add_help_msg(const char *help_msg) {
65 help_msg_ += help_msg;
66 return shared_from_this();
67 }
68};
69} // namespace binder
70
72 public:
73 // 类型定义
74 typedef std::vector<std::pair<std::string, std::shared_ptr<binder::cmd_option_bind_base> > >
75 cmd_array_type; // 大小类型
76 typedef std::shared_ptr<cmd_option_value> value_type; // 值类型
77 typedef std::vector<value_type>::size_type size_type; // 大小类型
78
79 protected:
80 std::shared_ptr<std::map<std::string, std::shared_ptr<cmd_option_value> > > key_value_;
81 std::vector<std::shared_ptr<cmd_option_value> > keys_;
84
85 // 初始化Key-Value映射(用于第一次调用get(key)时调用)
86 void init_key_value_map();
87
88 public:
89 // 构造函数
91 cmd_option_list(int argv, const char *argc[]);
92 cmd_option_list(const std::vector<std::string> &cmds);
93
94 // 增加选项
95 void add(const char *param);
96
97 // 删除全部选项
98 void clear();
99
100 // 读取指令集
101 void load_cmd_array(const cmd_array_type &cmds);
102
103 // 添加指令
104 void append_cmd(const char *cmd_content, std::shared_ptr<binder::cmd_option_bind_base> base_node);
105
106 // 移除末尾指令
107 void pop_cmd();
108
109 const cmd_array_type &get_cmd_array() const;
110
111 // 根据键值获取选项指针,如果不存在返回默认值
112 value_type get(std::string key, const char *default_val);
113
114 // 根据键值获取选项指针,如果不存在返回空指针
115 value_type get(std::string key);
116
117 // 根据下标获取选项指针,如果不存在会出现运行时错误
118 value_type get(size_type index) const;
119
120 // 操作符重载,功能和上面一样
121 value_type operator[](size_type index) const;
122
123 // 获取参数数量
125
126 // 重置Key-Value映射表
127 // # 在第一次调用get(字符串[, 默认值])后会建立映射表
128 // # 如果这之后add了参数而没有调用此函数重置映射表
129 // # 新的变量将不会进入映射表
130 void reset_key_value_map();
131
132 // 设置透传参数列表
133 void set_ext_param(void *param);
134
135 // 获取透传参数列表
136 void *get_ext_param() const;
137};
138} // namespace cli
139} // namespace util
140
141#endif /* _CMDOPTIONLIST_H_ */
virtual std::shared_ptr< cmd_option_bind_base > add_help_msg(const char *help_msg)
std::vector< help_msg_t > help_list_t
virtual void operator()(callback_param arg)=0
virtual std::string get_help_msg(const char *prefix_data="") const
static bool sort_by_all_cmds(const help_msg_t &l, const help_msg_t &r)
virtual std::shared_ptr< cmd_option_bind_base > set_help_msg(const char *help_msg)
value_type get(std::string key, const char *default_val)
value_type operator[](size_type index) const
std::shared_ptr< cmd_option_value > value_type
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)
std::shared_ptr< std::map< std::string, std::shared_ptr< cmd_option_value > > > key_value_
const cmd_array_type & get_cmd_array() const
std::vector< std::shared_ptr< cmd_option_value > > keys_
std::vector< value_type >::size_type size_type
std::vector< std::pair< std::string, std::shared_ptr< binder::cmd_option_bind_base > > > cmd_array_type
void add(const char *param)
cmd_option_list & callback_param
std::shared_ptr< cmd_option_bind_base > binded_obj