7f427d9 |
Jamozed |
2022-03-06 12:55:13 |
0
|
.TH OPTGET 3 2022-03-06 "libutil" "libutil Programmer's Manual" |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
1
|
.SH NAME |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
2
|
optget \(em parse command line options |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
3
|
.SH SYNOPSYS |
98e5939 |
Jamozed |
2021-11-27 17:53:46 |
4
|
#include "optget.h" |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
5
|
.PP |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
6
|
extern const struct opt \fIOPTGET_INIT\fR; |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
7
|
.PP |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
8
|
int \fBoptget\fR(struct opt *\fIopt\fB, char *\fIav\fB[], int \fIflags\fB); |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
9
|
.SH DESCRIPTION |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
10
|
The \fBoptget\fR() function parses command line options. The parameter \fIopt\fR |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
11
|
is a pointer to an opt struct which contains variables related to a parse run. |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
12
|
The parameter \fIav\fR is the argument vertex as passed to \fImain\fR(). The |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
13
|
paramter \fIflags\fR is a set of binary flags that may customise the parse |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
14
|
execution. |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
15
|
.PP |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
16
|
If the first character of \fIopt.str\fR is not a colon character ':', then an |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
17
|
error message will be printed to standard error in the event of an error. |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
18
|
.PP |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
19
|
The \fBoptget\fR() function will return the value of the next option, if one is |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
20
|
found that is either in \fIopt.str\fR or \fIopt.lops\fR for short and long |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
21
|
options respectively. Once all options have been parsed, \fBoptget\fR() will |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
22
|
return -1. If an invalid option is parsed, \fBoptget\fR() will return a question |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
23
|
mark character '?', and if an option is parsed that requires an argument but one |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
24
|
hasnt been specified then \fBoptget\fR() will return a colon character ':'. |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
25
|
.PP |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
26
|
If an option is parsed, \fIopt.ind\fR will contain the option index, |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
27
|
\fIopt.opt\fR will contain the option character if the option is short, or 0 |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
28
|
otherwise, \fIopt.lop\fR will contain the option string if the option is long, |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
29
|
or NULL otherwise, \fIopt.arg\fR will contain the option argument if one is |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
30
|
present, or NULL otherwise. |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
31
|
.SH EXAMPLE |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
32
|
.nf |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
33
|
static struct lop lops[] = { |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
34
|
{ "help", ARG_NUL, 256 }, |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
35
|
{ "version", ARG_NUL, 257 }, |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
36
|
{ NULL, 0, 0 } |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
37
|
}; |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
38
|
|
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
39
|
int main(int ac, char *av[]) { A0 = av[0]; |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
40
|
struct opt opt = OPTGET_INIT; opt.str = "ab"; opt.lops = lops; |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
41
|
for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
42
|
case 'a': { printf("'-a' parsed\\n"); break; } |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
43
|
case 'b': { printf("'-b' parsed\\n"); break; } |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
44
|
case 256: { hlp(); return 0; } |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
45
|
case 257: { ver(); return 0; } |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
46
|
default: { return 1; } |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
47
|
} return 0; |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
48
|
} |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
49
|
.fi |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
50
|
.SH COPYRIGHT |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
51
|
.nf |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
52
|
Copyright (C) 2020, Jakob Wakeling |
7f427d9 |
Jamozed |
2022-03-06 12:55:13 |
53
|
MIT Licence (https://opensource.org/licenses/MIT) |
3d8be0f |
Jamozed |
2020-11-01 13:43:44 |
54
|
.fi |
|
|
|
55
|
|