-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt_parser.h
48 lines (36 loc) · 1012 Bytes
/
opt_parser.h
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
#pragma once
/* This is a template not a library, so you have to modify it to meet your
* requirements.
*
* This is designed for g++/clang++ on linux, and only for c++11 or later. */
#ifndef _OPTIONS_PARSER_H_
#define _OPTIONS_PARSER_H_
#include <getopt.h>
enum ARG_TYPE { NO_ARGUMENT, REQUIRE_ARGUMENT, OPTIONAL_ARGUMENT };
struct opt {
char short_opt;
const char *long_opt;
enum ARG_TYPE type;
int count;
const char *arg;
// TODO: add more
const char *arg_name;
const char *description;
opt();
};
class opt_parser {
public:
opt_parser();
~opt_parser();
int parse(opt *opts, int n, int argc, char *const *argv);
private:
void make_short_opt(const opt *opts, int n);
void make_long_opt(const opt *opts, int n);
opt **make_ref(opt *opts, int n, int max_val) const;
void handle_missing_arg_opt();
void handle_unrecognized_opt();
private:
char *short_opts;
struct option *long_opts;
};
#endif //_OPTIONS_PARSER_H_