-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandLineParsing.cs
145 lines (130 loc) · 5.79 KB
/
CommandLineParsing.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.IO;
namespace Dithery_cli
{
public static class CmdParsing
{
public static (bool valid, string possibleError, DitheringMethod dithering, ColorReductionMethod colorReduction, OutputFormat outputFormat, string inputFile, string outputFile) ParseParameters(string[] args)
{
if (!File.Exists(args[0]))
{
return (false, "Cannot find input file!", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
string input = args[0];
int ditheringMethodValueIndex = FindParameterIndex(args, "-m");
if (ditheringMethodValueIndex < 0)
{
return (false, "Missing parameter -m !", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
ditheringMethodValueIndex++;
if (ditheringMethodValueIndex == args.Length)
{
return (false, "Missing parameter -m value !", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
int colorReductionMethodValueIndex = FindParameterIndex(args, "-c");
if (colorReductionMethodValueIndex < 0)
{
return (false, "Missing parameter -c !", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
colorReductionMethodValueIndex++;
if (colorReductionMethodValueIndex == args.Length)
{
return (false, "Missing parameter -c value !", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
int formatValueIndex = FindParameterIndex(args, "-f");
if (formatValueIndex < 0)
{
return (false, "Missing parameter -f !", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
formatValueIndex++;
if (formatValueIndex == args.Length)
{
return (false, "Missing parameter -f value !", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
int outputFileValueIndex = FindParameterIndex(args, "-o");
if (outputFileValueIndex < 0)
{
return (false, "Missing parameter -o !", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
outputFileValueIndex++;
if (outputFileValueIndex == args.Length)
{
return (false, "Missing parameter -o value !", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
DitheringMethod ditheringMethod = DitheringMethod.None;
string ditheringMethodString = args[ditheringMethodValueIndex];
try
{
ditheringMethod = (DitheringMethod) Enum.Parse(typeof(DitheringMethod), ditheringMethodString, ignoreCase: true);
if (!Enum.IsDefined(typeof(DitheringMethod), ditheringMethod))
{
return (false, $"Cannot parse dithering method from {ditheringMethodString} !", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
}
catch
{
return (false, $"Cannot parse dithering method from {ditheringMethodString} !", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
if (ditheringMethod == DitheringMethod.None)
{
return (false, $"Cannot use None as dithering!", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
ColorReductionMethod colorReductionMethod = ColorReductionMethod.None;
string colorReductionMethodString = args[colorReductionMethodValueIndex];
try
{
colorReductionMethod = (ColorReductionMethod) Enum.Parse(typeof(ColorReductionMethod), colorReductionMethodString, ignoreCase: true);
if (!Enum.IsDefined(typeof(ColorReductionMethod), colorReductionMethod))
{
return (false, $"Cannot parse color reduction method from {colorReductionMethodString} !", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
}
catch
{
return (false, $"Cannot parse color reduction method from {colorReductionMethodString} !", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
if (colorReductionMethod == ColorReductionMethod.None)
{
return (false, $"Cannot use None as color reduce method!", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
OutputFormat outputFormat = OutputFormat.None;
string outputFormatString = args[formatValueIndex];
try
{
outputFormat = (OutputFormat) Enum.Parse(typeof(OutputFormat), outputFormatString, ignoreCase: true);
if (!Enum.IsDefined(typeof(OutputFormat), outputFormat))
{
return (false, $"Cannot parse output format from {outputFormatString} !", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
}
catch
{
return (false, $"Cannot parse output format from {outputFormatString} !", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
string outputFileName = args[outputFileValueIndex];
if (File.Exists(outputFileName))
{
return (false, $"Cannot overwrite existing file {outputFileName} !", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
if (outputFormat == OutputFormat.HTMLBasic)
{
if (!outputFileName.EndsWith(".html") && !outputFileName.EndsWith(".htm"))
{
return (false, $"HTML output must use .html or .htm file extension!", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
}
else if (outputFormat == OutputFormat.SingleImage)
{
if (!outputFileName.EndsWith(".png"))
{
return (false, $"Image output must use .png file extension!", DitheringMethod.None, ColorReductionMethod.None, OutputFormat.None, "", "");
}
}
return (true, "", ditheringMethod, colorReductionMethod, outputFormat, input, outputFileName);
}
private static int FindParameterIndex(string[] args, string toSeek)
{
return Array.FindIndex(args, item => item == toSeek);
}
}
}