-
Notifications
You must be signed in to change notification settings - Fork 2
/
getpath.d
72 lines (66 loc) · 1.99 KB
/
getpath.d
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
/+dub.sdl:
name "printdirs"
dependency "standardpaths" path="../"
+/
import std.stdio;
import std.getopt;
import standardpaths;
void main(string[] args)
{
import std.conv : to;
import std.range : iota;
StandardPath[string] stringToType;
foreach(StandardPath i; StandardPath.min..StandardPath.max) {
stringToType[to!string(i)] = i;
}
bool verify;
bool create;
string subfolder;
auto helpInformation = getopt(args,
"verify", "Verify if path exists", &verify,
"create", "Create if does not exist", &create,
"subfolder", "Subfolder path", &subfolder
);
if (helpInformation.helpWanted)
{
defaultGetoptPrinter("Usage: getpath [options...] <pathType>", helpInformation.options);
return;
}
if (args.length < 2) {
stderr.writeln("Path type must be specified");
return;
}
FolderFlag flags;
if (verify) {
flags |= FolderFlag.verify;
}
if (create) {
flags |= FolderFlag.create;
}
foreach(pathType; args[1..$]) {
StandardPath* typePtr = pathType in stringToType;
if (typePtr) {
StandardPath type = *typePtr;
string path;
if (subfolder.length) {
path = writablePath(type, subfolder, flags);
} else {
path = writablePath(type, flags);
}
if (path.length) {
writefln("%s: %s", pathType, path);
} else {
if (create) {
stderr.writefln("%s: could not create path", pathType);
} else if (verify) {
writefln("%s: path does not exist", pathType);
} else {
writefln("%s: could not determine path", pathType);
}
}
} else {
stderr.writefln("Unknown type: %s", pathType);
writefln("Available types: %s", iota(StandardPath.min, StandardPath.max));
}
}
}