-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
76 lines (72 loc) · 2.54 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
74
75
76
using Mono.Options;
using System;
using System.IO;
namespace Archiver
{
class Program
{
[MTAThread]
static void Main(string[] args)
{
var options = new Options();
var optionSet = new OptionSet()
{
{ "in=|input=", "input filename", v => options.Input = v },
{ "out=|output=", "output filename", v => options.Output = v },
{ "t|type:", "compression type", v => options.Type = ParseType(v) },
{ "mem:", "maximum memory for read buffers in megabytes", v => options.ReadingMemory = (v != null ? int.Parse(v) : -1) },
{ "v|verbose", "show detailed information during execution", v => options.VerboseOutput = (v != null) }
};
var commandSet = new CommandSet("archiver")
{
"usage: archiver.exe COMMAND [arguments] [options]",
new Command("compress", "Compress input file to output file")
{
Options = optionSet,
Run = argv => RunCommand(() =>
{
var factory = new ProcessorFactory(options);
var compressor = factory.CreateCompressor(options);
compressor.Run();
})
},
new Command("decompress", "Decompress input file to output file")
{
Options = optionSet,
Run = argv => RunCommand(() =>
{
var factory = new ProcessorFactory(options);
var compressor = factory.CreateDecompressor(options);
compressor.Run();
})
}
};
commandSet.Run(args);
}
static void RunCommand(Action action)
{
try
{
action();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
static CompressionType ParseType(string val)
{
var enumType = typeof(CompressionType);
try
{
var result = (CompressionType)Enum.Parse(enumType, val, true);
return result;
}
catch (Exception e)
{
Console.WriteLine("Type '" + val + "' is unknown. Using 'gzip' type instead");
return CompressionType.GZip;
}
}
}
}