scot_test.c
5.12 KB
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
/*
* san_test.c: A small app to test the functionality of san, my
* Swiss Army Nife tool-lib and to show how to use it.
* Actually only focused on cmdla which parses commandline
* arguments.
*
* Copyright (C) 2006 Georg Steffers. All rights reserved.
*
* This software is licensed under the terms of the GNU Genral Public
* License (GPL). Please see gpl.txt for details or refer to
* http://www.gnu.org/licenses/gpl.html
*
* Author: Georg Steffers <georg@steffers.org>
*
* 01/14/2006: Georg Steffers - First implemented
* 01/15/2006: Georg Steffers - V0.1 ready. Modified much and many
* now long arguments are supported as
* well as arguments with am optional
* parameter.
* 01/23/2006: Georg Steffers - add support for gettext.
* 01/24/2006: Georg Steffers - renamed to san_test.c as it should reflect
* the whole lib as it grows.
*/
#include <stdio.h>
#include <scot/cmdla.h>
#include <scot_common.h>
//#include "../config.h"
int
switch_cb (void *arg)
{
int *sw = (int *) arg;
* sw = 1;
}
int
main (int argc, char *argv[])
{
char *cval = NULL;
char *string = NULL;
char *base = NULL;
char *home = NULL;
int ival = 0;
float fval = 0.0;
int sw1 = 0, sw2 = 0;
char *locale;
char domain[1024];
char about[] =
N_("CMDLA_TEST is used to demonstrate the usage of CMDLA.\n"
"CMDLA stands for CoMmanD Line Argument and it is some functions that,\n"
"as the name suggested, makes it convinient to parse command line\n"
"parameters.\n\n"
"To every parameter there has to be assigned a callback funktion.If\n"
"one only wants to map options into string, int or float variables,\n"
"then one can use the predefind get_cmdlap_cb callback.\n"
"There are two kinds of options that CMDLA processes: switches, which\n"
"Don't have an additional value, and parameters, which may or may not\n"
"have an additional value added.\n"
"Parameters can be set up, so that they need to have an addition value.\n"
"In addition to parameter parsing and calling appropriate callbacks,\n"
"CMDLA creates a help output by some hints given to it.\n"
"All one has to do is fill a structure for the switches you have and\n"
"one for the parameters, feed them to process_cmd_line and you're "
"ready.");
char copyright[] =
N_("Copyright (C) 2006 by Georg Steffers <georg@steffers.org>\n"
"This software is licensed under the terms of the GNU General Public\n"
"Licence (GPL). Please refer http://www.gnu.org/licenses/gpl.html\n"
"for details.");
char usage_add[] = N_("infile outfile");
/* an entry with the pointer to the cb-funktion as NULL is end sign */
struct cmdlap_cbt cmdlap_cbs[] = {
{
'c', N_("cval"), CMDLA_REQ_ARG,
get_cmdlap_cb, CMDLA_TYPE_STRING, &cval,
N_("takes a required string argument")
},
{
'i', N_("ival"), CMDLA_OPT_ARG,
get_cmdlap_cb, CMDLA_TYPE_INT, &ival,
N_("takes an optional integer argument")
},
{
'f', N_("fval"), CMDLA_REQ_ARG,
get_cmdlap_cb, CMDLA_TYPE_FLOAT, &fval,
N_("takes a required float argument")
},
{
0, N_("string"), CMDLA_REQ_ARG,
get_cmdlap_cb, CMDLA_TYPE_STRING, &string,
N_("another required string argument")
},
{
0, N_("base"), CMDLA_REQ_ARG,
get_cmdlap_cb, CMDLA_TYPE_STRING, &base,
N_("and again a req. string argument")
},
{
0, N_("home"), CMDLA_REQ_ARG,
get_cmdlap_cb, CMDLA_TYPE_STRING, &home,
N_("oh, a last required string argument")
},
CMDLAP_CBT_END
};
/* an entry with the pointer to the cb-funktion as NULL is end sign */
struct cmdlas_cbt cmdlas_cbs[] = {
{
's', N_("switch"),
switch_cb, &sw1,
N_("a switch takes nor argument")
},
{
'S', NULL,
switch_cb, &sw2,
N_("another switch")
},
CMDLAS_CBT_END
};
locale = setlocale (LC_ALL, "");
if (locale == NULL)
printf ("uable to set locale\n");
else
printf ("use current locale: %s\n", locale);
printf ("LOCALEDIR: %s\n", LOCALEDIR);
printf ("PACKAGE: %s\n", PACKAGE);
snprintf (domain, 1024, "%s_test", PACKAGE);
bindtextdomain (domain, LOCALEDIR);
textdomain ("scot_test");
process_cmd_line (
argc, argv, about, copyright, usage_add, cmdlap_cbs, cmdlas_cbs);
printf("CVAL: %s\n", cval);
printf("STRING: %s\n", string);
printf("BASE: %s\n", base);
printf("HOME: %s\n", home);
printf("IVAL: %d\n", ival);
printf("FVAL: %f\n", fval);
printf(
"SWITCH1 %scalled\n",
sw1 == 0 ?
"not " :
"");
printf(
"SWITCH2 %scalled\n",
sw2 == 0 ?
"not " :
"");
if (optind < argc)
{
printf ("non-option ARGV-elements: ");
while (optind < argc)
printf ("%s ", argv[optind++]);
printf ("\n");
}
return 0;
}