summaryrefslogtreecommitdiff
path: root/core/pkg/tbox.pkg/inc/tbox/utils/option.h
blob: f1c67cc5e89c9cf38093cc82050c15a9cbc35957 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*!The Treasure Box Library
 * 
 * TBox is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 * 
 * TBox is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with TBox; 
 * If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a>
 * 
 * Copyright (C) 2009 - 2015, ruki All rights reserved.
 *
 * @author      ruki
 * @file        option.h
 * @ingroup     utils
 *
 */
#ifndef TB_UTILS_OPTION_H
#define TB_UTILS_OPTION_H

/* //////////////////////////////////////////////////////////////////////////////////////
 * includes
 */
#include "prefix.h"

/* //////////////////////////////////////////////////////////////////////////////////////
 * extern
 */
__tb_extern_c_enter__

/* //////////////////////////////////////////////////////////////////////////////////////
 * types
 */

/// the option mode enum
typedef enum __tb_option_mode_e
{
    TB_OPTION_MODE_END      = 0     //!< end
,   TB_OPTION_MODE_VAL      = 1     //!< value
,   TB_OPTION_MODE_KEY      = 2     //!< --key or -k
,   TB_OPTION_MODE_KEY_VAL  = 3     //!< --key=value or --key value or -k=value or -k value
,   TB_OPTION_MODE_MORE     = 4     //!< more and end

}tb_option_mode_e;

/// the option type enum
typedef enum __tb_option_type_e
{
    TB_OPTION_TYPE_NONE     = 0
,   TB_OPTION_TYPE_BOOL     = 1
,   TB_OPTION_TYPE_CSTR     = 2
,   TB_OPTION_TYPE_FLOAT    = 3
,   TB_OPTION_TYPE_INTEGER  = 4

}tb_option_type_e;

/// the option item type
typedef struct __tb_option_item_t
{
    /// the short name
    tb_char_t           sname;

    /// the long name
    tb_char_t const*    lname;

    /// the mode
    tb_uint16_t         mode;

    /// the type
    tb_uint16_t         type;

    /// the help
    tb_char_t const*    help;

}tb_option_item_t;

/// the option ref type
typedef struct{}*       tb_option_ref_t;

/* //////////////////////////////////////////////////////////////////////////////////////
 * interfaces
 */

/*! init option
 *
 * @param name          the command name
 * @param help          the command help
 * @param opts          the option list
 *
 * @return              the option 
 */
tb_option_ref_t         tb_option_init(tb_char_t const* name, tb_char_t const* help, tb_option_item_t const* opts);

/*! exit option
 *
 * @param option        the option 
 */
tb_void_t               tb_option_exit(tb_option_ref_t option);

/*! find the option item 
 *
 * @param option        the option 
 * @param name          the option name, long name or short name
 *
 * @return              tb_true or tb_false
 */
tb_bool_t               tb_option_find(tb_option_ref_t option, tb_char_t const* name);

/*! done option
 *
 * @param option        the option 
 * @param argc          the arguments count
 * @param argv          the arguments value
 *
 * @return              tb_true or tb_false
 */
tb_bool_t               tb_option_done(tb_option_ref_t option, tb_size_t argc, tb_char_t** argv);

/*! dump option
 *
 * @param option        the option 
 */
tb_void_t               tb_option_dump(tb_option_ref_t option);

/*! help option
 *
 * @param option        the option 
 */
tb_void_t               tb_option_help(tb_option_ref_t option);

/*! the option item - cstr
 *
 * @param option        the option 
 * @param name          the option name, long name or short name
 *
 * @return              the c-string pointer
 */
tb_char_t const*        tb_option_item_cstr(tb_option_ref_t option, tb_char_t const* name);

/*! the option item - bool
 *
 * @param option        the option 
 * @param name          the option name, long name or short name
 *
 * @return              tb_true or tb_false
 */
tb_bool_t               tb_option_item_bool(tb_option_ref_t option, tb_char_t const* name);

/*! the option item - uint8
 *
 * @param option        the option 
 * @param name          the option name, long name or short name
 *
 * @return              the integer value
 */
tb_uint8_t              tb_option_item_uint8(tb_option_ref_t option, tb_char_t const* name);

/*! the option item - sint8
 *
 * @param option        the option 
 * @param name          the option name, long name or short name
 *
 * @return              the integer value
 */
tb_sint8_t              tb_option_item_sint8(tb_option_ref_t option, tb_char_t const* name);

/*! the option item - uint16
 *
 * @param option        the option 
 * @param name          the option name, long name or short name
 *
 * @return              the integer value
 */
tb_uint16_t             tb_option_item_uint16(tb_option_ref_t option, tb_char_t const* name);

/*! the option item - sint16
 *
 * @param option        the option 
 * @param name          the option name, long name or short name
 *
 * @return              the integer value
 */
tb_sint16_t             tb_option_item_sint16(tb_option_ref_t option, tb_char_t const* name);

/*! the option item - uint32
 *
 * @param option        the option 
 * @param name          the option name, long name or short name
 *
 * @return              the integer value
 */
tb_uint32_t             tb_option_item_uint32(tb_option_ref_t option, tb_char_t const* name);

/*! the option item - sint32
 *
 * @param option        the option 
 * @param name          the option name, long name or short name
 *
 * @return              the integer value
 */
tb_sint32_t             tb_option_item_sint32(tb_option_ref_t option, tb_char_t const* name);

/*! the option item - uint64
 *
 * @param option        the option 
 * @param name          the option name, long name or short name
 *
 * @return              the integer value
 */
tb_uint64_t             tb_option_item_uint64(tb_option_ref_t option, tb_char_t const* name);

/*! the option item - sint64
 *
 * @param option        the option 
 * @param name          the option name, long name or short name
 *
 * @return              the integer value
 */
tb_sint64_t             tb_option_item_sint64(tb_option_ref_t option, tb_char_t const* name);

#ifdef TB_CONFIG_TYPE_HAVE_FLOAT

/*! the option item - float
 *
 * @param option        the option 
 * @param name          the option name, long name or short name
 *
 * @return              the integer value
 */
tb_float_t              tb_option_item_float(tb_option_ref_t option, tb_char_t const* name);

/*! the option item - sint64
 *
 * @param option        the option 
 * @param name          the option name, long name or short name
 *
 * @return              the integer value
 */
tb_double_t             tb_option_item_double(tb_option_ref_t option, tb_char_t const* name);

#endif

/* //////////////////////////////////////////////////////////////////////////////////////
 * extern
 */
__tb_extern_c_leave__

#endif