-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
73 lines (67 loc) · 2.67 KB
/
Program.cs
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
using System.CommandLine;
namespace WhichCam;
internal static class Program
{
private static int Main(string[] args)
{
var rootCommand = new RootCommand("WhichCam - Camera Model/Maker detector");
var targetArgument = new Argument<DirectoryInfo>(
name: "target",
description: "The directory containing the images to analyze.")
{
Arity = ArgumentArity.ExactlyOne
};
var outputOption = new Option<DirectoryInfo>(
name: "--output",
description: "Where to copy the files",
getDefaultValue: () => new DirectoryInfo("./"))
{
Arity = ArgumentArity.ExactlyOne,
};
var orderByOption = new Option<string>(
name: "--orderBy",
description: "Determine if it needs to orders medias by maker or model",
getDefaultValue: () => "maker"
)
{
Arity = ArgumentArity.ExactlyOne
};
targetArgument.AddValidator(arg =>
{
var value = arg.GetValueOrDefault() as DirectoryInfo;
if (value is not null || value!.Exists is false)
{
throw new ArgumentException("Target directory {0} does not exist", value.FullName);
}
var picturesPaths = value!.GetFiles().Any(f => InfosExtractor.validImageFormats.Contains(f.Extension.ToLower()));
if (picturesPaths is false)
{
throw new ArgumentException("Target directory {0} has no valid files", value.FullName);
}
});
outputOption.AddValidator(arg =>
{
if (arg.GetValueOrDefault() is not DirectoryInfo value || value.Exists is false)
{
throw new ArgumentException($"Output directory is invalid or does not exist.");
}
});
orderByOption.AddValidator(arg =>
{
var value = arg.GetValueOrDefault();
if (value is not "maker" && value is not "model")
{
throw new ArgumentException("The value for --orderBy must be 'maker' or 'model'. (default 'maker')");
}
});
rootCommand.AddArgument(targetArgument);
rootCommand.AddOption(outputOption);
rootCommand.AddOption(orderByOption);
rootCommand.SetHandler((target, output, orderBy) =>
{
var infos = InfosExtractor.RetrieveInformation(target);
InfosExtractor.OrderPictures(infos, output, orderBy);
}, targetArgument, outputOption, orderByOption);
return rootCommand.Invoke(args);
}
}