-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathoptionParser.hpp
More file actions
130 lines (83 loc) · 3.38 KB
/
Copy pathoptionParser.hpp
File metadata and controls
130 lines (83 loc) · 3.38 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
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
#ifndef OPTION_PARSER_H
#define OPTION_PARSER_H
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <vector>
#include <typeinfo>
using namespace std;
namespace op{
/* convert a boolean to a string */
string bool2str(bool value);
/* convert a string to a boolean */
bool str2bool(string value);
/* convert a string to an int */
int str2int(string value);
/* convert a string to an int */
float str2float(string value);
/* convert a string to an int */
double str2double(string value);
/* strips a string from prefix dashes - or -- */
std::string stripPrefixDashes(const std::string& str);
/* CLASS OPTION ===================================
An option is defined by a short and a long name
The - and -- in the option names are added automatically
*/
class Option{
protected:
string var, default_value;
string short_name, long_name, description;
bool _is_boolean;
bool _is_required;
bool _is_parsed;
public:
Option(string short_name, string long_name, string description, string default_value, bool is_required);
/* create a boolean option */
Option(string short_name, string long_name, string description, bool default_value, bool is_required);
~Option(){}
string get_short_name()const{return short_name;}
string get_long_name()const{return long_name;}
string get_description()const{return description;}
string get_default() const{ return default_value;}
string get_var() const{ return var;}
void set_var(string& new_var){ var = new_var;}
void set_var(bool new_val){ var = bool2str(new_val);}
void toggle_is_parsed(){ _is_parsed = true;}
bool is_boolean(){return _is_boolean;}
bool is_required(){return _is_required;}
bool is_parsed(){return _is_parsed;}
};
/* CLASS OPTIONPARSER ===================================
This class implements a very easy to use c++ option parser
Options can be added and then their value accessed with [] operator
All options values are string, the user must convert them himself
*/
class OptionParser{
protected:
vector<Option> options;
int max_size_short; // max size of short names of options, usefull for a beautifull help print
int max_size_long; // max size of long names of options, usefull for a beautifull help print
int max_size_desc; // max size of desciption of options, usefull for a beautifull help print
int description_max_line_size; // maximum size of a descritption line, if reached, a line is skipped
bool has_option(string name);
int get_description_max_line_size() const{ return description_max_line_size;}
void set_description_max_line_size(int size){ description_max_line_size = size;}
// returns a string of n spaces
std::string repeat_str( const std::string &word, int times );
// returns max
int max(int a, int b);
public:
OptionParser(int max_size_desc_line = 100);
~OptionParser();
/* Shows help with aligned columns */
void show_help();
void add_option(string short_name, string long_name, string description, string default_value, bool required);
/* to add a boolean option */
void add_option(string short_name, string long_name, string description, bool required);
/* This function changes default option values to those which are read on the command line */
bool parse_options(int& argc, char**& argv);
/* Get the option values as string */
string operator[](string name) const;
};
} // end namespace op
#endif