-
Notifications
You must be signed in to change notification settings - Fork 0
/
cargs.c
75 lines (67 loc) · 1.62 KB
/
cargs.c
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
#include "cargs.h"
static char CARGS_NICK_DEFAULT[] = "knetch";
void cargs_help(struct cargs* args, struct flub* flub) {
FILE* out;
// Print error.
if (flub) {
out = stderr;
fprintf(out, "ERROR: '%s'\n\n", flub->message);
} else {
out = stdout;
}
// Print usage.
fprintf(out, "gls [ARGS]\n\nARGS:\n");
fprintf(out, "\t-h --help Print this usage message\n");
fprintf(out, "\t-n --nick Connect using specified nickname (default: "
"'%s', cur: '%s')\n", CARGS_NICK_DEFAULT, args->nick);
// Exit program.
if (flub) {
exit(EXIT_FAILURE);
} else {
exit(EXIT_SUCCESS);
}
}
struct flub* cargs_parse(struct cargs* args, int argc, char* argv[]) {
struct flub* flub;
struct option longopts[] = {
{"help", 0, NULL, 'h'},
{"nick", 1, NULL, 'n'},
{0, 0, 0, 0}
};
int ret;
// Set defaults.
memset(args, 0, sizeof(struct cargs));
strcpy(args->nick, CARGS_NICK_DEFAULT);
// Parse arguments.
while((ret = getopt_long(argc, argv, ":n:", longopts, NULL)) != -1) {
switch(ret) {
case 'h':
cargs_help(args, NULL);
case 'n':
strlcpy(args->nick, optarg, GLS_NICK_LENGTH);
if ((flub = gls_nick_validate(optarg, 0))) {
cargs_help(args, flub);
}
break;
case ':':
flub = g_flub_toss("Missing argument after '%c'",
optopt);
cargs_help(args, flub);
case '?':
// Unknown option.
if (optopt) {
flub = g_flub_toss("Unknown argument '%c'",
optopt);
} else {
flub = g_flub_toss("Unknown longopt at index "
"'%i'", optind);
}
cargs_help(args, flub);
default:
// Error!
flub = g_flub_toss("Unable to parse arguments");
cargs_help(args, flub);
}
}
return NULL;
}