-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchiver.cpp
More file actions
69 lines (64 loc) · 2.73 KB
/
archiver.cpp
File metadata and controls
69 lines (64 loc) · 2.73 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
#include <algorithm>
#include <functional>
#include <iostream>
#include <memory>
#include <random>
#include <string_view>
#include "argument_parser.h"
#include "compressor.h"
#include "decompressor.h"
#include "exceptions.h"
#include "files.h"
int main(int argc, char** argv) {
ArgumentParser parser("archiver");
parser.AddOption("-c", "compress files into archive", "-c archive_name file1 [file2 ...]");
parser.AddOption("-d", "decompress archive", "-d archive_name");
parser.AddOption("-h", "show this message", "-h");
try {
auto parsed_arguments = parser.ParseArguments(argc, argv);
if (parsed_arguments.options.size() > 1) {
throw ValidationError("Too many options");
} else if (parsed_arguments.options.empty()) {
throw ValidationError("You need to specify at least one option");
} else if (parsed_arguments.options.contains("-h")) {
parser.PrintUsage();
return 0;
} else if (parsed_arguments.options.contains("-c")) {
if (parsed_arguments.positional_arguments.size() < 2) {
throw ValidationError("You need to specify archive name and at least one input file");
}
Path archive_name = parsed_arguments.positional_arguments[0];
if (!ValidateOutput(archive_name)) {
throw ValidationError("Archive destination is not valid");
}
std::vector<Path> filenames;
for (std::size_t i = 1; i < parsed_arguments.positional_arguments.size(); ++i) {
Path filename = parsed_arguments.positional_arguments[i];
if (!ValidateInput(filename)) {
throw ValidationError("At least one of input files is not valid");
}
filenames.push_back(filename);
}
Compress(archive_name, filenames);
} else if (parsed_arguments.options.contains("-d")) {
if (parsed_arguments.positional_arguments.empty()) {
throw ValidationError("You need to specify archive name");
} else if (parsed_arguments.positional_arguments.size() > 1) {
throw ValidationError("Too many positional arguments");
}
Path archive_name = parsed_arguments.positional_arguments[0];
if (!ValidateInput(archive_name)) {
throw ValidationError("Invalid archive path");
}
Decompress(archive_name);
}
} catch (const ParsingError& exc) {
std::cerr << "ERROR: " << exc.what() << "\n\n";
parser.PrintUsage();
return 111;
} catch (const ArchiverException& exc) {
std::cerr << "ERROR: " << exc.what() << "\n\n";
return 111;
}
return 0;
}