-
Notifications
You must be signed in to change notification settings - Fork 1
/
optparse.cpp
72 lines (67 loc) · 2.21 KB
/
optparse.cpp
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
//
// Created by valdemar on 17.07.16.
//
#include "optparse.h"
#include <cstdio>
#include <getopt.h>
#include <string.h>
#include <arpa/inet.h>
const char *OPTIONS_STR = "d:h:p:sw:";
int parse_args(int argc, char **argv, options &opts) {
int opt = getopt(argc, argv, OPTIONS_STR);
if (opt == -1) {
printf("You should specify options!\n");
return -1;
}
uint8_t required_opt = 0;
while (opt != -1) {
switch (opt) {
case 'd':
strcpy(opts.server_root, optarg);
required_opt |= 0x1;
break;
case 'h':
if (inet_aton(optarg, &opts.server_ip) == 0) {
fprintf(stderr, "Error: '%s' is not valid ipv4 address\n", optarg);
return -1;
}
required_opt |= 0x2;
break;
case 'p':
if (sscanf(optarg, "%hu", &opts.server_port) != 1) {
fprintf(stderr, "Error: '%s' is not valid port value\n", optarg);
return -2;
}
opts.server_port = htons(opts.server_port);
required_opt |= 0x4;
break;
case 's':
opts.daemonize = false;
break;
case 'w':
if (sscanf(optarg, "%hu", &opts.workers_count) != 1) {
fprintf(stderr, "Error: '%s' is not workers count\n", optarg);
return -5;
}
break;
case '?':
default:
return -3;
}
opt = getopt(argc, argv, OPTIONS_STR);
}
if (required_opt != 0x7) {
fprintf(stderr, "Error: missing one of required options!\n");
return -4;
}
return 0;
}
void show_usage(const char *ex_path) {
printf("%s -h <ip> -p <port> -d <directory> [-s] [-w <num>]\n", ex_path);
printf("Available options:\n"
"\t-h host ipv4 address\n"
"\t-p port in which server should run\n"
"\t-d web server root directory\n"
"\t-s stay on foreground (no daemonize). Default: no.\n"
"\t-w number of worker threads. Default: 4.\n");
}