blob: 40558aec8bff3ba722175a59b61bd6aa6eec45fc (
plain)
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
|
/*
* (C) 2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_CHECK_GETOPT_H__
#define BOTAN_CHECK_GETOPT_H__
#include <string>
#include <vector>
#include <stdexcept>
#include <map>
#include <botan/parsing.h>
class OptionParser
{
public:
std::vector<std::string> arguments() const { return leftover; }
bool is_set(const std::string& key) const
{
return (options.find(key) != options.end());
}
std::string value(const std::string& key) const
{
std::map<std::string, std::string>::const_iterator i = options.find(key);
if(i == options.end())
throw std::runtime_error("Option " + key + " not found");
return i->second;
}
std::string value_if_set(const std::string& key) const
{
return value_or_else(key, "");
}
std::string value_or_else(const std::string& key,
const std::string& or_else) const
{
return is_set(key) ? value(key) : or_else;
}
size_t int_value_or_else(const std::string& key, size_t or_else) const
{
return is_set(key) ? Botan::to_u32bit(value(key)) : or_else;
}
void help(std::ostream& o, const char* cmd = nullptr)
{
o << "Usage: ";
if(cmd)
o << cmd << " ";
for(auto flag : flags)
{
o << "--" << flag.name();
if(flag.takes_arg())
o << "=";
o << " ";
}
o << "\n";
}
void parse(char* argv[]);
OptionParser(const std::string& opt_string)
{
std::vector<std::string> opts = Botan::split_on(opt_string, '|');
for(size_t j = 0; j != opts.size(); j++)
flags.push_back(OptionFlag(opts[j]));
}
private:
class OptionFlag
{
public:
std::string name() const { return opt_name; }
bool takes_arg() const { return opt_takes_arg; }
OptionFlag(const std::string& opt_string)
{
std::string::size_type mark = opt_string.find('=');
opt_name = opt_string.substr(0, mark);
opt_takes_arg = (mark != std::string::npos);
}
private:
std::string opt_name;
bool opt_takes_arg;
};
OptionFlag find_option(const std::string& name) const
{
for(size_t j = 0; j != flags.size(); j++)
if(flags[j].name() == name)
return flags[j];
throw std::runtime_error("Unknown option " + name);
}
std::vector<OptionFlag> flags;
std::map<std::string, std::string> options;
std::vector<std::string> leftover;
};
#endif
|