forked from andrewn/webcapture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptparse.h
82 lines (70 loc) · 2.67 KB
/
optparse.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
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
/*
* optparse.h
*
* An option parser for C++ modeled after the optparse Python
* module. Create an instance of OptionParser in your program
* and you can add options to it then call the method
* parse_args with argc and argv and it will parse them. The
* '-h' and '--help' options are built in, they print the
* usage message.
*
* W. Evan Sheehan <[email protected]>
*
*/
using namespace std;
#include <map>
#include <vector>
#include <string>
/* Use this type to determine what we're storing, naming
* convention is the same as the optparse python module.
* Even with STORE_FALSE and STORE_TRUE the values are
* going to be stored as strings, false is "0" true is "1"
* so you can just atoi() the value and use that for tests.
*/
typedef enum {STORE_FALSE=0, STORE_TRUE, STORE} action_t;
class Option
{
public:
Option (std::string shrt, std::string lng, std::string dest,
std::string hlp="", action_t act=STORE);
~Option ();
/* Override the == operator so that when you compare an Option
* to a string it will check the string against shrt_flag and lng_flag
* and return true if either match.
*/
bool operator == (const std::string & option);
action_t action; // define what we store
std::string shrt_flag; // short flag, something like -o
std::string lng_flag; // long flag, something like --outfile
std::string help; // help message about the option
std::string destination; // the key used to store this option in the
// parser.
};
class OptionParser
{
public:
OptionParser (std::string usage="", bool show_opts=true);
~OptionParser ();
/* Add an option to the parser. */
void add_option (std::string shrt_flag, std::string lng_flag,
std::string destination, std::string help="",
std::string dfault="", action_t act=STORE);
/* Parse the commandline args. */
void parse_args (int argc, char **argv);
/* Print an error message: this->use_msg followed by msg. Exit. */
void error (std::string msg);
/* Print the help message (use_msg) and exit. */
void help ();
map<std::string, std::string> options; // Dictionary of the options
vector<std::string> arguments; // List of the arguments with no flag
private:
unsigned int lng_flag_max_len,
shrt_flag_max_len; // max length of the largest flags
bool help_msg_show_opts; // If true we'll display the help messages for each
// option when help() is called.
std::string use_msg; // usage message.
vector<Option> opts; // List of the options the parser knows about.
/* Helper for finding which Option (if any) argv[index] matches. */
void find_opt (char **argv, int &index);
};
/*** The End ***/