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
|
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
module;
#include <variant>
#include <string>
#include <stdexcept>
#include <algorithm>
#include <cstring>
#include <boost/program_options.hpp>
export module katja.command_line;
namespace katja
{
export enum class command
{
updates,
search,
help
};
export struct command_line
{
const command type;
const std::vector<std::string> arguments;
};
export class invalid_command_error : public std::runtime_error
{
const std::string command;
public:
invalid_command_error()
: std::runtime_error("Expecting the command line to begin with a command.")
{
}
invalid_command_error(const std::string& invalid_command)
: command{ invalid_command },
std::runtime_error("Unknown command given on the command line.")
{
}
};
export command_line parse_command_line(int argc, const char **argv)
{
if (argc == 1)
{
throw invalid_command_error();
}
std::vector<std::string> arguments;
std::copy(argv + 2, argv + argc, std::back_inserter(arguments));
if (strcmp("updates", argv[1]) == 0)
{
return command_line{ command::updates, std::move(arguments) };
}
if (strcmp("search", argv[1]) == 0)
{
return command_line{ command::search, std::move(arguments) };
}
if (strcmp("help", argv[1]) == 0)
{
return command_line{ command::help, std::move(arguments) };
}
throw invalid_command_error(argv[1]);
}
}
|