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
|
=encoding UTF-8
=head1 NAME
BH_Args - command line argument processing
=head1 SYNTAX
#include <BH/Args.h>
static BH_ArgsOption options[] =
{
{'h', "help", 0, "Display this help"},
{1000, "list", 0, "List files"},
{'i', "input", BH_ARGS_VALUE, "Input file"},
{'o', "output", BH_ARGS_VALUE | BH_ARGS_OPTIONAL, "Output file"},
{0, NULL, 0, NULL}
};
static int OptionsCallback(int key,
char *arg,
void *data)
{
switch (key)
{
case BH_ARGS_UNKNOWN:
/* Unknown options */
break;
case BH_ARGS_ARGUMENT:
/* Regular argument in arg */
break;
case 'h':
/* Named option without argument */
break;
case 1000:
/* Long-only option without argument */
break;
case 'i':
/* Option with argument in arg */
break;
case 'o':
/* Option with optional argument in arg */
break;
}
return BH_OK;
}
BH_ArgsParse(argc, argv, options, OptionsCallback, NULL);
BH_ArgsHelp(options, 0);
cc prog.c -o prog -lbh
=head1 DESCRIPTION
The BH_Args library is designed for convenient handling of command line
arguments in programs. It allows you to define a set of options that can be
specified when starting a program and provides mechanisms for their analysis and
processing.
Argument parsing follows these rules:
=over
=item *
If the "--" argument is specified, subsequent argument parsing is stopped.
=item *
If the "-" argument is specified, it is processed as is.
=item *
For long argument values, the string after the equal sign or the next argument
("--define=value" or "--define value") will be used.
=item *
For short argument values, the remainder of the string after the parameter
character or the next argument ("-dvalue" or "-d value") will be used.
=back
=head1 API CALLS
=head2 BH_ArgsParse
int BH_ArgsParse(int argc,
char **argv,
BH_ArgsOption *options,
BH_ArgsCallback callback,
void *data);
Parses the command line arguments I<options> (specified in I<argc> and I<argv>)
and calls the specified handler I<callback> (with user data I<data>).
The I<options> parameter refers to a null-terminated array.
If successful, this function returns 0, otherwise it returns an error code.
=head2 BH_ArgsHelp
void BH_ArgsHelp(BH_ArgsOption *options,
int padding);
Prints help information for the arguments I<options> to F<stdout> (with the
specified alignment I<padding>).
If the I<padding> parameter is 0, the default value (30) will be used.
=head1 STRUCTURES
=head2 BH_ArgsOption
typedef struct BH_ArgsOption
{
int key;
const char *name;
int flags;
const char *description;
} BH_ArgsOption;
The I<key> field contains the option identifier. If the I<key> identifier is a
printable ASCII character, it will be used as a short argument.
The optional I<name> field contains a string that will be used as a long
argument.
The I<flags> field contains argument flags. A combination of the following flags
is possible:
=over
=item B<BH_ARGS_VALUE>
The argument accepts a value.
=item B<BH_ARGS_OPTIONAL>
The argument value is optional.
=back
The I<description> field contains a string that will be used when displaying
help information using the L</BH_ArgsHelp> function.
=head1 SEE ALSO
L<BH>
|