-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptions.cs
140 lines (127 loc) · 4.84 KB
/
Options.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
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace SupSubtitleParser
{
public class Options
{
public string inputFile { get; set; }
public string outputPath { get; set; }
public ImageFormat format { get; set; } = ImageFormat.Jpeg;
public Color bgColor { get; set; } = Color.Transparent;
public static Options TryParse(string[] args)
{
if (args.Length == 0)
{
PrintUsage();
return null;
}
if (args[0].StartsWith("-"))
{
string arg = args[0].ToLower();
switch (arg)
{
case "-v":
case "--version":
PrintVer();
return null;
case "-h":
case "--help":
PrintUsage();
return null;
default:
PrintUsage();
return null;
}
}
Options opts = new Options();
opts.inputFile = args[0];
for (int i = 1; i < args.Length; i += 2)
{
if (args.Length < i + 1)
{
PrintError($"no value of arg[{args[i]}].");
return null;
}
string arg = args[i].ToLower();
switch (arg)
{
case "-o":
case "--out":
opts.outputPath = args[i + 1].TrimEnd('\\');
break;
case "-f":
case "--format":
var fmt = args[i + 1].ToLower();
switch (fmt)
{
case "bmp":
opts.format = ImageFormat.Bmp;
break;
case "jpeg":
opts.format = ImageFormat.Jpeg;
break;
case "gif":
opts.format = ImageFormat.Gif;
break;
case "tiff":
opts.format = ImageFormat.Tiff;
break;
case "png":
opts.format = ImageFormat.Png;
break;
default:
PrintError($"image format is not valid.");
return null;
}
break;
case "-b":
case "--bgcolor":
var color = Utils.TryParseColor(args[i + 1]);
if (color == null || !color.HasValue)
{
PrintError($"The bgcolor value[{args[i + 1]}] is not valid.");
return null;
}
else
{
opts.bgColor = color.Value;
}
break;
default:
PrintUsage();
return null;
}
}
return opts;
}
public string GetImgExt()
{
return format.ToString().ToLower().Replace("jpeg", "jpg");
}
private static void PrintError(string message)
{
ColorConsole.WriteLine(message, 3);
Console.WriteLine();
PrintUsage();
}
private static void PrintUsage()
{
PrintVer();
Console.WriteLine($"Usage: {Utils.GetAppName()} input_file -o|-out <path> -f|-format {{bmp|jpeg|gif|tiff|png}} -b|-bgcolor <color>");
Console.WriteLine(" input_file\t: subtitle file for parse (*.sup) ");
Console.WriteLine(" -o,--out\t: folder path for time line and images file to save.");
Console.WriteLine(" -f,--output\t: timage format. (default:jpeg)");
Console.WriteLine(" -b,--bgcolor\t: image backgroud color.");
Console.WriteLine(" \t: named color (like:red lightyellow) or hex rgb value (like:#ff0000 ).");
Console.WriteLine(" \t: (default:transparent)");
Console.WriteLine();
}
private static void PrintVer()
{
Version ver = Utils.GetAppVer();
DateTime buildDate = new DateTime(2000, 1, 1).AddDays(ver.Build);
Console.WriteLine($"{Utils.GetAppName()} {ver.Major}.{ver.Minor}-{buildDate.ToString("yyyyMMdd")}\r\n");
}
}
}