Skip to content

Commit e12d5d6

Browse files
committed
refactored blazer.exe to be more useful. added help. version bumped to main archiver version
1 parent aacf192 commit e12d5d6

File tree

10 files changed

+525
-190
lines changed

10 files changed

+525
-190
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ packages
66
.tools
77
private.snk
88
sign.cmd
9+
_releases
910

1011
# todo: check this folders
1112
ipch

Blazer.Exe/Blazer.Exe.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@
5252
<Reference Include="System.Xml" />
5353
</ItemGroup>
5454
<ItemGroup>
55-
<Compile Include="CommandLineOptions.cs" />
55+
<Compile Include="CommandLine\BlazerCommandLineOptions.cs" />
56+
<Compile Include="CommandLine\CommandLineParser.cs" />
57+
<Compile Include="CommandLine\CommandLineOptionAttribute.cs" />
58+
<Compile Include="NullStream.cs" />
5659
<Compile Include="Program.cs" />
5760
<Compile Include="Properties\AssemblyInfo.cs" />
5861
<Compile Include="StatStream.cs" />
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace Force.Blazer.Exe.CommandLine
2+
{
3+
public class BlazerCommandLineOptions
4+
{
5+
[CommandLineOption('h', "help", "Display this help")]
6+
public bool Help { get; set; }
7+
8+
[CommandLineOption('d', "decompress", "Decompress archive")]
9+
public bool Decompress { get; set; }
10+
11+
[CommandLineOption('f', "force", "Overwrite target files without confirmation")]
12+
public bool Force { get; set; }
13+
14+
[CommandLineOption("stdin", "Read data from stdin")]
15+
public bool Stdin { get; set; }
16+
17+
[CommandLineOption("stdout", "Write data to stdout")]
18+
public bool Stdout { get; set; }
19+
20+
[CommandLineOption('p', "password", "Archive password")]
21+
public string Password { get; set; }
22+
23+
[CommandLineOption("encyptfull", "Encrypt archive fully (this key required on decompress)")]
24+
public bool EncryptFull { get; set; }
25+
26+
[CommandLineOption("nofilename", "Do not (re)store file name")]
27+
public bool NoFileName { get; set; }
28+
29+
[CommandLineOption("mode", "Compression mode: none, block (default), stream, streamhigh")]
30+
public string Mode { get; set; }
31+
32+
[CommandLineOption("blobonly", "Compress to blob (no header and footer)")]
33+
public bool BlobOnly { get; set; }
34+
35+
[CommandLineOption('t', "test", "Test archive")]
36+
public bool Test { get; set; }
37+
}
38+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
3+
namespace Force.Blazer.Exe.CommandLine
4+
{
5+
[AttributeUsage(AttributeTargets.Property)]
6+
public class CommandLineOptionAttribute : Attribute
7+
{
8+
public char ShortKey { get; set; }
9+
10+
public string LongKey { get; set; }
11+
12+
public string Description { get; set; }
13+
14+
public CommandLineOptionAttribute(char shortKey, string longKey, string description)
15+
{
16+
ShortKey = shortKey;
17+
LongKey = longKey;
18+
Description = description;
19+
}
20+
21+
public CommandLineOptionAttribute(string longKey, string description)
22+
{
23+
LongKey = longKey;
24+
Description = description;
25+
}
26+
}
27+
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Globalization;
5+
using System.Linq;
6+
using System.Reflection;
7+
using System.Text;
8+
9+
namespace Force.Blazer.Exe.CommandLine
10+
{
11+
public class CommandLineParser<T> where T : new()
12+
{
13+
private T _options;
14+
15+
private List<string> _nonKeyOptions;
16+
17+
public CommandLineParser(string[] args)
18+
{
19+
ParseArgumentsInternal(args);
20+
}
21+
22+
private void ParseArgumentsInternal(string[] args)
23+
{
24+
var t = new T();
25+
26+
var knownOptions =
27+
typeof(T).GetProperties()
28+
.Select(x => new Tuple<PropertyInfo, CommandLineOptionAttribute>(x, (CommandLineOptionAttribute)x.GetCustomAttributes(typeof(CommandLineOptionAttribute), true).FirstOrDefault()))
29+
.Where(x => x.Item2 != null)
30+
.ToArray();
31+
32+
var boolOptions =
33+
knownOptions.Where(x => x.Item1.PropertyType == typeof(bool) || x.Item1.PropertyType == typeof(bool?)).ToArray();
34+
35+
Action<string> throwError = e => { throw new InvalidOperationException("Invalid commandline argument " + e); };
36+
var dict = new Dictionary<string, string>();
37+
var list = new List<string>();
38+
string key = null;
39+
foreach (var arg in args)
40+
{
41+
if (arg.Length > 0)
42+
{
43+
if (arg[0] == '-')
44+
{
45+
if (arg.Length == 1) throwError(arg);
46+
if (arg[1] == '-')
47+
{
48+
if (arg[1] == '-' && arg.Length <= 3) throwError(arg);
49+
if (key != null) dict[key] = string.Empty;
50+
key = arg.Remove(0, 2);
51+
}
52+
else
53+
{
54+
key = arg.Remove(0, 1);
55+
}
56+
57+
dict[key] = string.Empty;
58+
}
59+
else
60+
{
61+
if (boolOptions.Any(x => x.Item2.ShortKey.ToString(CultureInfo.InvariantCulture) == key || x.Item2.LongKey == key)) key = null;
62+
if (key == null)
63+
{
64+
list.Add(arg);
65+
}
66+
else
67+
{
68+
dict[key] = arg;
69+
key = null;
70+
}
71+
}
72+
}
73+
}
74+
75+
_nonKeyOptions = list;
76+
77+
foreach (var v in dict)
78+
{
79+
var option = knownOptions.FirstOrDefault(
80+
x => x.Item2.ShortKey.ToString(CultureInfo.InvariantCulture) == v.Key || x.Item2.LongKey == v.Key);
81+
if (option != null)
82+
{
83+
if (boolOptions.Contains(option))
84+
{
85+
option.Item1.SetValue(t, true, null);
86+
}
87+
else
88+
{
89+
var converted = new TypeConverter().ConvertTo(v.Value, option.Item1.PropertyType);
90+
option.Item1.SetValue(t, converted, null);
91+
}
92+
}
93+
}
94+
95+
_options = t;
96+
}
97+
98+
public T Get()
99+
{
100+
return _options;
101+
}
102+
103+
public string[] GetNonParamOptions()
104+
{
105+
return _nonKeyOptions.ToArray();
106+
}
107+
108+
public string GetNonParamOptions(int idx)
109+
{
110+
if (_nonKeyOptions.Count <= idx) return null;
111+
return _nonKeyOptions[idx];
112+
}
113+
114+
public string GenerateHelp()
115+
{
116+
var options = typeof(T).GetProperties()
117+
.Select(x => (CommandLineOptionAttribute)x.GetCustomAttributes(typeof(CommandLineOptionAttribute), true).FirstOrDefault())
118+
.Where(x => x != null)
119+
.ToArray();
120+
121+
var maxParamLen = 0;
122+
123+
foreach (var option in options)
124+
{
125+
var cnt = 0;
126+
if (option.ShortKey != default(char) && option.LongKey != null) cnt = 6 + option.LongKey.Length;
127+
else if (option.ShortKey != default(char)) cnt = 2;
128+
else cnt = 2 + option.LongKey.Length;
129+
maxParamLen = Math.Max(maxParamLen, cnt);
130+
}
131+
132+
var b = new StringBuilder();
133+
foreach (var option in options)
134+
{
135+
b.Append("\t");
136+
string str;
137+
if (option.ShortKey != default(char) && option.LongKey != null)
138+
{
139+
str = string.Format("-{0}, --{1}", option.ShortKey, option.LongKey);
140+
}
141+
else if (option.ShortKey != default(char))
142+
{
143+
str = string.Format("-{0}", option.ShortKey);
144+
}
145+
else
146+
{
147+
str = string.Format("--{0}", option.LongKey);
148+
}
149+
150+
b.Append(str);
151+
b.Append(new string(' ', maxParamLen - str.Length));
152+
b.Append("\t");
153+
b.Append(option.Description);
154+
b.AppendLine();
155+
}
156+
157+
return b.ToString();
158+
}
159+
160+
public string GenerateHeader()
161+
{
162+
var title = (AssemblyTitleAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), true).First();
163+
var copy = (AssemblyCopyrightAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), true).First();
164+
var version = (AssemblyFileVersionAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).First();
165+
166+
return string.Format("{0} {2} {1}", title.Title, copy.Copyright, version.Version);
167+
}
168+
}
169+
}

Blazer.Exe/CommandLineOptions.cs

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)