aboutsummaryrefslogtreecommitdiff
path: root/src/wixcop/CommandLine/WixCopCommandLineParser.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/wixcop/CommandLine/WixCopCommandLineParser.cs')
-rw-r--r--src/wixcop/CommandLine/WixCopCommandLineParser.cs132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/wixcop/CommandLine/WixCopCommandLineParser.cs b/src/wixcop/CommandLine/WixCopCommandLineParser.cs
new file mode 100644
index 00000000..53012cfd
--- /dev/null
+++ b/src/wixcop/CommandLine/WixCopCommandLineParser.cs
@@ -0,0 +1,132 @@
1using System;
2using System.Collections.Generic;
3using WixCop.Interfaces;
4using WixToolset.Core;
5using WixToolset.Extensibility.Data;
6using WixToolset.Extensibility.Services;
7
8namespace WixCop.CommandLine
9{
10 public sealed class WixCopCommandLineParser : IWixCopCommandLineParser
11 {
12 private bool fixErrors;
13 private int indentationAmount;
14 private readonly List<string> searchPatterns;
15 private readonly IServiceProvider serviceProvider;
16 private string settingsFile1;
17 private string settingsFile2;
18 private bool showHelp;
19 private bool showLogo;
20 private bool subDirectories;
21
22 public WixCopCommandLineParser(IServiceProvider serviceProvider)
23 {
24 this.serviceProvider = serviceProvider;
25
26 this.indentationAmount = 4;
27 this.searchPatterns = new List<string>();
28 this.showLogo = true;
29 }
30
31 public ICommandLineArguments Arguments { get; set; }
32
33 public ICommandLineCommand ParseWixCopCommandLine()
34 {
35 this.Parse();
36
37 if (this.showLogo)
38 {
39 AppCommon.DisplayToolHeader();
40 Console.WriteLine();
41 }
42
43 if (this.showHelp)
44 {
45 return new HelpCommand();
46 }
47
48 return new ConvertCommand(
49 this.serviceProvider,
50 this.fixErrors,
51 this.indentationAmount,
52 this.searchPatterns,
53 this.subDirectories,
54 this.settingsFile1,
55 this.settingsFile2);
56 }
57
58 private void Parse()
59 {
60 this.showHelp = 0 == this.Arguments.Arguments.Length;
61 var parser = this.Arguments.Parse();
62
63 while (!this.showHelp &&
64 String.IsNullOrEmpty(parser.ErrorArgument) &&
65 parser.TryGetNextSwitchOrArgument(out var arg))
66 {
67 if (String.IsNullOrWhiteSpace(arg)) // skip blank arguments.
68 {
69 continue;
70 }
71
72 if (parser.IsSwitch(arg))
73 {
74 if (!this.ParseArgument(parser, arg))
75 {
76 parser.ErrorArgument = arg;
77 }
78 }
79 else
80 {
81 this.searchPatterns.Add(arg);
82 }
83 }
84 }
85
86 private bool ParseArgument(IParseCommandLine parser, string arg)
87 {
88 var parameter = arg.Substring(1);
89
90 switch (parameter.ToLowerInvariant())
91 {
92 case "?":
93 this.showHelp = true;
94 return true;
95 case "f":
96 this.fixErrors = true;
97 return true;
98 case "nologo":
99 this.showLogo = false;
100 return true;
101 case "s":
102 this.subDirectories = true;
103 return true;
104 default: // other parameters
105 if (parameter.StartsWith("set1", StringComparison.Ordinal))
106 {
107 this.settingsFile1 = parameter.Substring(4);
108 }
109 else if (parameter.StartsWith("set2", StringComparison.Ordinal))
110 {
111 this.settingsFile2 = parameter.Substring(4);
112 }
113 else if (parameter.StartsWith("indent:", StringComparison.Ordinal))
114 {
115 try
116 {
117 this.indentationAmount = Convert.ToInt32(parameter.Substring(7));
118 }
119 catch
120 {
121 throw new ArgumentException("Invalid numeric argument.", parameter);
122 }
123 }
124 else
125 {
126 throw new ArgumentException("Invalid argument.", parameter);
127 }
128 return true;
129 }
130 }
131 }
132}