Newer
Older
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
#include "../copts.h"
static const char* logfile = NULL;
static int use_daemon = 1;
static unsigned short port = 0;
static unsigned long addr = 0;
static const char* cfgfile = "not set";
static char* strs[20];
static const char * strenum[] = {
"strenum0",
"strenum1",
"strenum2",
"strenum3",
"strenum4",
NULL
};
static int str_callback(const copt_t * opt, const char * option, copt_value_t * value)
{
printf("STR_CALLBACK: %s: %s\n", option, value->v_str);
return 0;
}
static int long_callback(const copt_t * opt, const char * option, copt_value_t * value)
{
printf("LONG_CALLBACK: %s %ld\n", option, value->v_long);
return 0;
}
static int short_callback(const copt_t * opt, const char * option, copt_value_t * value)
{
printf("SHORT_CALLBACK: %s %d\n", option, value->v_short);
return 0;
}
static int arg_callback(const copt_t * opt, const char * option, copt_value_t * value)
{
printf("ARGUMENT: %s \n", value->v_str);
return 0;
}
static copt_t options [] = {
{"h?", "help", COPT_HELP, NULL , "Print this help page"},
{"C", "config", COPT_CFGFILE, &cfgfile , "Config file" },
{"b", "boolvalue", COPT_BOOL |COPT_CONFIG, NULL , "Bool test value" },
{"d", "nodaemon", COPT_BOOLI |COPT_CONFIG, &use_daemon , "Start as daemon" },
{"l", "logfile", COPT_STR |COPT_CONFIG, &logfile , "Use this log file" },
{"e", "strenum", COPT_STRENUM|COPT_CONFIG, &strenum[0] , "String enum values" },
{"pP", "port", COPT_USHORT |COPT_CONFIG, &port , "Bind to port" },
{"aA", "addr", COPT_HOST |COPT_CONFIG, &addr , "Bind to ip-address" },
{"s", "strings", COPT_STRLIST|COPT_CONFIG, &strs[0] , "String list" },
{"S", "str_cb", COPT_STR |COPT_CALLBACK,&str_callback,"String throw callback" },
{"L", "l_cb", COPT_LONG |COPT_CALLBACK,&long_callback,"Long throw callback" },
{"H", "s_cb", COPT_SHORT |COPT_CALLBACK,&short_callback,"Short throw callback" },
{NULL, NULL, COPT_CALLBACK, &arg_callback,NULL },
{NULL, NULL, COPT_END, NULL , NULL }
};
int main(int argc, char** argv)
{
int i, j;
int flags = COPT_DEFAULT|COPT_NOERR_UNKNOWN|COPT_NOAUTOHELP;
i = coptions(argc, argv, flags, options);
if(i<0){
if(COPT_ERC(i)){
if(i == COPT_EHELP){
coptions_help(stdout, argv[0], options, "nonarg params");
}
}else{
printf("Unknown option %s\n", argv[0-i]);
}
}
printf("help = %s\n", options[0].vptr?"yes":"no");
printf("bool = %s\n", options[1].vptr?"yes":"no");
printf("use_daemon = %s\n", use_daemon?"yes":"no");
printf("logfile = %s\n", logfile);
printf("port = %d\n", port);
printf("addr = %08lX\n", addr);
printf("strenum=%d(%s)\n",
(int)(((const char **)options[5].vptr) - &strenum[0]),
*(const char **)options[5].vptr );
{
char ** b = &strs[0];
char ** e = options[8].vptr;
printf("strlist: count=%d\n", (int)(e - b));
while(b < e){
printf("\t%s\n", *b);
b++;
}
}
if(i>0){
printf("\nNonoptions:\n");
for(j=1; j<i; j++){
printf("\targv[%d]=%s\n", j, argv[j]);
}
printf("\nOptions in order:\n");
for(; j<argc; j++){
printf("\targv[%d]=%s\n", j, argv[j]);
}
}
return 0;
}