forked from Lcressot/cpp_option_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptionParser.hpp
More file actions
125 lines (76 loc) · 3.05 KB
/
Copy pathoptionParser.hpp
File metadata and controls
125 lines (76 loc) · 3.05 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
#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);
/* 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;
public:
Option(string short_name, string long_name, string description, string default_value);
/* create a boolean option */
Option(string short_name, string long_name, string description, bool default_value);
~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);}
bool is_boolean(){return _is_boolean;}
};
/* 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);
/* to add a boolean option */
void add_option(string short_name, string long_name, string description);
/* 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