Skip to content

Commit f2c41a4

Browse files
committed
init runnable version
1 parent 9725104 commit f2c41a4

File tree

6 files changed

+566
-5
lines changed

6 files changed

+566
-5
lines changed

Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
all: objcli
3+
4+
CXXFLAGS := -g
5+
6+
objcli: obj_cli.o test_obj_cli.o
7+
${CXX} ${CXXFLAGS} -o $@ obj_cli.o test_obj_cli.o
8+
9+
clean:
10+
rm -fr *.o
11+
rm -fr objcli

README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ Object Oriented Command Line Interface Library of C++
55

66
Introduce
77
--------
8-
This is
8+
This is used to handle command line input.
9+
Programers can write a struct to describe the input and accord function,
10+
Despite the getopt process and error handler
911

1012
Usage
1113
--------
1214
First, define a Request struct, include all possible parameters
15+
1316
Second, define a Context class, include the
17+
1418
Thirdly, define the class and actions of the cli
15-
Finally, call cli_invoke to parse request and execute actions.
19+
20+
Finally, call CliInvoke to parse request and execute actions.
1621

1722
Here are some example:
1823

@@ -54,24 +59,32 @@ Here are some example:
5459
// ./a.out class1 action1 --param1=a --param2=b
5560
// ./a.out class1 action2 --param2=a
5661
int main(int argc, const char *argv[]) {
57-
return cli_invoke<Request, Context>(argc, argv, meta, 3);
62+
return CliInvoke<Request, Context>(argc, argv, meta, 3);
5863
}
5964
### Advanced
6065
int main(int argc, const char* argv[]) {
6166
Request request;
6267
Context context;
6368
InvokeContext<Request, Context> env(argv[0], g_meta, meta_num, g_opt, opt_num);
64-
int ret = cli_parse(argc, argv, request, env);
69+
int ret = CliParse(argc, argv, request, env);
6570
if(0 != ret){
6671
return ret;
6772
}
6873
if(0 != strcmp(request.password, "secret")){
6974
return -1;
7075
}
7176
context.password = "holy pot";
72-
return cli_invoke(request, context, env);
77+
return CliInvoke(request, context, env);
7378
}
7479

80+
AutoComplete
81+
-------
82+
Besides, this program supplies auto-complete functions, users can use <Tab> to call the program give Tips
83+
eg:
84+
./objcli node <Tag>
85+
set get
86+
87+
7588
About
7689
--------
7790
For more information, please refer to the code

cli_complete.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
#Cli program name to use auto complete function
3+
cli_paths=(
4+
*objcli
5+
)
6+
7+
function _mycomp()
8+
{
9+
local word=${COMP_WORDS[COMP_CWORD]}
10+
COMPREPLY=()
11+
if [[ ${word} == '--' ]]; then
12+
actions=($(${COMP_WORDS[@]}autocompletion 2>&1))
13+
else
14+
if [[ ${word:0:2} == '--' ]]; then
15+
unset COMP_WORDS[COMP_CWORD]
16+
fi
17+
actions=($(${COMP_WORDS[@]} --autocompletion 2>&1))
18+
fi
19+
COMPREPLY=($(compgen -W "$(echo ${actions[@]})" -- "${word}"))
20+
}
21+
for cli_path in ${cli_paths[@]}; do
22+
complete -F _mycomp ${cli_path}
23+
done

obj_cli.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//@author hankjin([email protected])
2+
//@date 2013/10/19 09:30:31
3+
//
4+
#include "obj_cli.h"
5+
6+
/** map for option convert */
7+
std::map<std::string, size_t> g_option_map;
8+
9+
/**
10+
* @brief
11+
*/
12+
bool is_completion(int argc, char *argv[])
13+
{
14+
const int optValue = 1;
15+
const char *short_options=":";
16+
option long_options[]={
17+
{
18+
"autocompletion",
19+
0,
20+
0,
21+
optValue
22+
},{
23+
NULL,
24+
0,
25+
0,
26+
0
27+
}
28+
};
29+
int option_index = 0;
30+
while(1){
31+
int c = getopt_long(argc, argv, short_options, long_options, &option_index);
32+
if(-1 == c){
33+
break;
34+
}
35+
else if(c == optValue){
36+
return true;
37+
}
38+
}
39+
return false;
40+
};
41+
42+
43+
/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */

0 commit comments

Comments
 (0)