-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs.cpp
More file actions
74 lines (61 loc) · 1.32 KB
/
Copy pathargs.cpp
File metadata and controls
74 lines (61 loc) · 1.32 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
/*
* Created on: Feb 20, 2013
* Author: ssobczak
*/
#include "args.h"
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
Args::Args(int argc, char* argv[]) : daemon(false), config(""), program_name("") {
parse_args(argc, argv);
}
Args::~Args() {
}
void Args::print_usage(FILE* stream) const {
fprintf(stream, "Usage: %s options.\n"
" -c --config config_file Load config from config_file.\n"
" -d --daemon Don't run as daemon.\n"
" -h --help Display this information.\n",
program_name.c_str());
}
void Args::parse_args(int argc, char* argv[]) {
if (argc < 1) {
abort();
}
program_name = argv[0];
const char* short_options = "hc:d";
const struct option long_options[] = {
{"help", 0, NULL, 'h'},
{"config", 1, NULL, 'c'},
{"daemon", 0, NULL, 'd'},
{NULL, 0, NULL, 0}
};
int next_option = -1;
do {
next_option = getopt_long(argc, argv, short_options, long_options, NULL);
switch (next_option) {
case 'h':
print_usage(stdout);
exit(EXIT_SUCCESS);
case 'd':
daemon = true;
break;
case 'c':
config = optarg;
break;
case '?':
print_usage(stderr);
exit(EXIT_FAILURE);
case -1:
break;
default:
abort();
/* no break */
}
} while (next_option != -1);
if (config == "") {
print_usage(stderr);
exit(EXIT_FAILURE);
}
}