-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom2.cpp
More file actions
61 lines (54 loc) · 1.51 KB
/
custom2.cpp
File metadata and controls
61 lines (54 loc) · 1.51 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
#include "argparse.hpp"
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
namespace geometry
{
struct Point
{
Point() = default;
Point(int xx, int yy) : x(xx), y(yy)
{
}
int x = 0;
int y = 0;
};
}
namespace argparse
{
template<>
class Converter<geometry::Point>
{
public:
auto from_string(std::string const & s) const -> std::optional<geometry::Point>
{
std::istringstream iss(s);
auto p = geometry::Point();
char comma;
iss >> p.x >> comma >> p.y;
return !iss.fail()
? std::optional<geometry::Point>(p)
: std::nullopt;
}
auto to_string(geometry::Point const & p) const -> std::string
{
return std::to_string(p.x) + "," + std::to_string(p.y);
}
auto are_equal(geometry::Point const & l, geometry::Point const & r) const -> bool
{
return l.x == r.x && l.y == r.y;
}
};
}
auto main(int argc, char * argv[]) -> int
{
auto parser = argparse::ArgumentParser();
parser.add_argument("start").type<geometry::Point>();
parser.add_argument("end").type<geometry::Point>();
auto parsed = parser.parse_args(argc, argv);
auto start = parsed.get_value<geometry::Point>("start");
auto end = parsed.get_value<geometry::Point>("end");
auto distance = std::hypot(end.x - start.x, end.y - start.y);
std::cout << "The distance is " << distance << '\n';
}