-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
71 lines (64 loc) · 1.87 KB
/
program.cpp
File metadata and controls
71 lines (64 loc) · 1.87 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
#include "program.h"
#include <algorithm>
#include <iterator>
#include <sstream>
#include <stdexcept>
using namespace program;
std::string arguments::get(const std::string& name) const
{
const auto i(optional.find(name));
if (i != optional.end())
return i->second;
const auto j(formal.find(name));
if (j != formal.end())
return std::get<1>(j->second);
throw std::invalid_argument(help());
}
std::string arguments::help() const
{
std::ostringstream r;
r << "Usage: " << program << " [options] ..." << std::endl;
r << "Options:" << std::endl;
std::for_each(formal.begin(), formal.end(), [&r] (const auto& o) {
r << '\t' << o.first << '\t' << std::get<0>(o.second);
const auto& dflt(std::get<1>(o.second));
if (!dflt.empty())
r << " (" << dflt << " by default)";
r << std::endl;
});
return r.str();
}
void arguments::parse(int argc, char* argv[])
{
decltype(optional) opt;
decltype(positional) pos;
program = 0 < argc ? argv[0] : "";
for (int i = 1; i < argc; ++i)
{
std::cmatch m;
if (std::regex_match(argv[i], m, re))
{
const auto j(formal.find(argv[i]));
if (j == formal.end())
throw std::invalid_argument(help());
const auto& cnst(std::get<2>(j->second));
if (cnst.empty())
{
if (++i == argc)
throw std::invalid_argument(help());
opt[j->first] = argv[i];
}
else
{
opt[j->first] = cnst;
}
}
else
{
std::copy(argv + i, argv + argc, std::back_inserter(pos));
break;
}
}
std::swap(opt, optional);
std::swap(pos, positional);
}