-
Notifications
You must be signed in to change notification settings - Fork 5
/
OptionParser.hpp
151 lines (126 loc) · 5.39 KB
/
OptionParser.hpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* Part of SMITHLAB software
*
* Copyright (C) 2018 Cold Spring Harbor Laboratory,
* University of Southern California and
* Andrew D. Smith
* Authors: Andrew D. Smith
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#ifndef OPTION_PARSER_HPP
#define OPTION_PARSER_HPP
#include <limits>
#include <string>
#include <vector>
class Option {
public:
Option(const std::string l_name, const char s_name, const std::string descr,
const bool reqd, int &val);
Option(const std::string l_name, const char s_name, const std::string descr,
const bool reqd, unsigned &val);
Option(const std::string l_name, const char s_name, const std::string descr,
const bool reqd, long &val);
Option(const std::string l_name, const char s_name, const std::string descr,
const bool reqd, unsigned long &val);
Option(const std::string l_name, const char s_name, const std::string descr,
const bool reqd, float &val);
Option(const std::string l_name, const char s_name, const std::string descr,
const bool reqd, double &val);
Option(const std::string l_name, const char s_name, const std::string descr,
const bool reqd, std::string &val);
Option(const std::string l_name, const char s_name, const std::string descr,
const bool reqd, bool &val);
Option(const std::string l_name, const char s_name, const std::string descr,
const bool reqd, char &val);
bool parse(std::vector<std::string> &command_line);
void parse_config_file(std::vector<std::string> &options);
std::string format_option_name() const;
std::string format_option_description(const size_t offset,
const bool show_default = false) const;
std::string format_default_value() const;
private:
unsigned arg_type;
std::string long_name;
char short_name;
std::string description;
bool required;
bool specified;
// the values of the options: ugly but clean
int *int_value;
unsigned int *uint_value;
long *long_value;
unsigned long *ulong_value;
float *float_value;
double *double_value;
std::string *string_value;
bool *bool_value;
char *char_value;
void format_option(const std::string &argument);
static void set_max_length(size_t num);
static size_t get_max_length();
bool option_match(const std::string &other);
};
class OptionParser {
public:
OptionParser(const std::string nm, const std::string descr,
std::string noflag_msg = "",
const size_t n_left = std::numeric_limits<size_t>::max());
void set_show_defaults() { show_defaults = true; }
void add_opt(const std::string l_name, const char s_name,
const std::string descr, const bool reqd, int &val);
void add_opt(const std::string l_name, const char s_name,
const std::string descr, const bool reqd, unsigned &val);
void add_opt(const std::string l_name, const char s_name,
const std::string descr, const bool reqd, long &val);
void add_opt(const std::string l_name, const char s_name,
const std::string descr, const bool reqd, unsigned long &val);
void add_opt(const std::string l_name, const char s_name,
const std::string descr, const bool reqd, float &val);
void add_opt(const std::string l_name, const char s_name,
const std::string descr, const bool reqd, double &val);
void add_opt(const std::string l_name, const char s_name,
const std::string descr, const bool reqd, std::string &val);
void add_opt(const std::string l_name, const char s_name,
const std::string descr, const bool reqd, bool &val);
void add_opt(const std::string l_name, const char s_name,
const std::string descr, const bool reqd, char &val);
void parse(const int argc, const char **argv,
std::vector<std::string> &arguments);
std::vector<std::string> parse(const int argc, const char **argv);
void parse(const int argc, const char **argv,
std::vector<std::string> &arguments, std::string config_filename);
bool help_requested() const { return help_request; }
std::string help_message() const;
bool about_requested() const { return about_request; }
std::string about_message() const;
std::string about_message_raw() const;
std::string invalid_leftover() const;
bool option_missing() const { return !first_missing_option_name.empty(); }
bool wrong_number_leftover() const {
return n_leftover != std::numeric_limits<size_t>::max() &&
leftover_args.size() != n_leftover;
}
std::string option_missing_message() const;
static const bool OPTIONAL = false;
static const bool REQUIRED = true;
private:
std::string prog_name;
std::string prog_descr;
std::string noflag_message;
std::vector<Option> options;
bool help_request;
bool about_request;
bool show_defaults;
std::string first_missing_option_name;
std::vector<std::string> leftover_args;
size_t n_leftover;
};
#endif