parseopt.c
2.61 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2016 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <mailutils/mailutils.h>
#include <mailutils/opt.h>
char *file_name;
char *find_value;
char *opt_value = "initial";
int jobs = 0;
int x_option;
int a_option;
int d_option;
struct mu_option group_a[] = {
MU_OPTION_GROUP("Group A"),
{ "file", 'f', "FILE", MU_OPTION_DEFAULT,
"set file name",
mu_c_string, &file_name
},
{ "find", 'F', "VALUE", MU_OPTION_DEFAULT,
"find VALUE",
mu_c_string, &find_value },
{ "optional", 'o', "FILE", MU_OPTION_ARG_OPTIONAL,
"optional argument",
mu_c_string, &opt_value },
{ NULL, 'x', NULL, MU_OPTION_DEFAULT,
"short-only option",
mu_c_incr, &x_option },
{ "all", 'a', NULL, MU_OPTION_DEFAULT,
"no arguments to this one",
mu_c_bool, &a_option },
MU_OPTION_END
};
struct mu_option group_b[] = {
MU_OPTION_GROUP("Group B"),
{ "debug", 'd', NULL, MU_OPTION_DEFAULT,
"another option",
mu_c_incr, &d_option },
{ "verbose", 'v', NULL, MU_OPTION_ALIAS },
{ "jobs", 'j', "N", MU_OPTION_DEFAULT,
"sets numeric value",
mu_c_int, &jobs },
MU_OPTION_END
};
struct mu_option *optv[] = { group_a, group_b, NULL };
#define S(s) ((s)?(s):"(null)")
int
main (int argc, char *argv[])
{
struct mu_parseopt po;
int rc;
int i;
mu_stdstream_setup (MU_STDSTREAM_RESET_NONE);
rc = mu_parseopt (&po, argc, argv, optv, MU_PARSEOPT_DEFAULT);
printf ("rc=%d\n", rc);
mu_parseopt_apply (&po);
argc -= po.po_ind;
argv += po.po_ind;
mu_parseopt_free (&po);
printf ("file_name=%s\n", S(file_name));
printf ("find_value=%s\n", S(find_value));
printf ("opt_value=%s\n", S(opt_value));
printf ("x_option=%d\n", x_option);
printf ("a_option=%d\n", a_option);
printf ("d_option=%d\n", d_option);
printf ("jobs=%d\n", jobs);
printf ("argv:\n");
for (i = 0; i < argc; i++)
{
printf ("%d: %s\n", i, argv[i]);
}
return 0;
}