-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSwitches.cs
More file actions
43 lines (36 loc) · 1.6 KB
/
Switches.cs
File metadata and controls
43 lines (36 loc) · 1.6 KB
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace BlockAmazon {
internal class Switches {
public bool ShowHelp { get; private set; } = false;
public bool Verbose { get; private set; } = false;
public bool WriteEventLog { get; private set; } = false;
public static string HelpMessage { get {
StringBuilder sb = new StringBuilder();
sb.AppendLine(String.Format("Usage: {0}.exe [/V] [/E] [/?]", Assembly.GetExecutingAssembly().GetName().Name));
sb.AppendLine(" /V Show verbose output (including list of blocked IP's)");
sb.AppendLine(" /E Log an event in the Application event log of Windows containing the program output");
sb.AppendLine(" /? Show this help message (and don't emit a success/failure indicator)");
return sb.ToString();
} }
public static Switches Parse(string[] args) {
Switches switches = new Switches();
if (Array.IndexOf<string>(args, "/?") >= 0) throw new ShowHelpException(); // overrides everything else
if (args.Length > 2) throw new SwitchParseException("Wrong number of parameters supplied");
foreach (string arg in args) {
ParseArg(arg, switches);
}
return switches;
}
public static void ParseArg(string arg, Switches switches) {
switch (arg.ToUpperInvariant()) {
case "/V": switches.Verbose = true; break;
case "/E": switches.WriteEventLog = true; break;
default: throw new SwitchParseException(String.Format("Invalid parameter: {0}", arg));
}
}
private Switches() { }
}
}