-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathFluentCommandLineParser.cs
More file actions
309 lines (272 loc) · 12.1 KB
/
FluentCommandLineParser.cs
File metadata and controls
309 lines (272 loc) · 12.1 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#region License
// FluentCommandLineParser.cs
// Copyright (c) 2013, Simon Williams
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provide
// d that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this list of conditions and the
// following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
// the following disclaimer in the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Fclp.Internals;
using Fclp.Internals.Errors;
using Fclp.Internals.Extensions;
using Fclp.Internals.Parsing;
using Fclp.Internals.Validators;
namespace Fclp
{
/// <summary>
/// A command line parser which provides methods and properties
/// to easily and fluently parse command line arguments.
/// </summary>
public class FluentCommandLineParser : IFluentCommandLineParser
{
/// <summary>
/// Initialises a new instance of the <see cref="FluentCommandLineParser"/> class.
/// </summary>
public FluentCommandLineParser()
{
IsCaseSensitive = true;
}
/// <summary>
/// The <see cref="StringComparison"/> type used for case sensitive comparisons.
/// </summary>
public const StringComparison CaseSensitiveComparison = StringComparison.CurrentCulture;
/// <summary>
/// The <see cref="StringComparison"/> type used for case in-sensitive comparisons.
/// </summary>
public const StringComparison IgnoreCaseComparison = StringComparison.CurrentCultureIgnoreCase;
List<ICommandLineOption> _options;
ICommandLineOptionFactory _optionFactory;
ICommandLineParserEngine _parserEngine;
ICommandLineOptionFormatter _optionFormatter;
IHelpCommandLineOption _helpOption;
ICommandLineParserErrorFormatter _errorFormatter;
ICommandLineOptionValidator _optionValidator;
/// <summary>
/// Gets or sets whether values that differ by case are considered different.
/// </summary>
public bool IsCaseSensitive
{
get { return StringComparison == CaseSensitiveComparison; }
set { StringComparison = value ? CaseSensitiveComparison : IgnoreCaseComparison; }
}
/// <summary>
/// Gets the <see cref="StringComparison"/> to use when matching values.
/// </summary>
internal StringComparison StringComparison { get; private set; }
/// <summary>
/// Gets the list of Options
/// </summary>
public List<ICommandLineOption> Options
{
get { return _options ?? (_options = new List<ICommandLineOption>()); }
}
/// <summary>
/// Gets or sets the default option formatter.
/// </summary>
public ICommandLineOptionFormatter OptionFormatter
{
get { return _optionFormatter ?? (_optionFormatter = new CommandLineOptionFormatter()); }
set { _optionFormatter = value; }
}
/// <summary>
/// Gets or sets the default option formatter.
/// </summary>
public ICommandLineParserErrorFormatter ErrorFormatter
{
get { return _errorFormatter ?? (_errorFormatter = new CommandLineParserErrorFormatter()); }
set { _errorFormatter = value; }
}
/// <summary>
/// Gets or sets the <see cref="ICommandLineOptionFactory"/> to use for creating <see cref="ICommandLineOptionFluent{T}"/>.
/// </summary>
/// <remarks>If this property is set to <c>null</c> then the default <see cref="OptionFactory"/> is returned.</remarks>
public ICommandLineOptionFactory OptionFactory
{
get { return _optionFactory ?? (_optionFactory = new CommandLineOptionFactory()); }
set { _optionFactory = value; }
}
/// <summary>
/// Gets or sets the <see cref="ICommandLineOptionValidator"/> used to validate each setup Option.
/// </summary>
public ICommandLineOptionValidator OptionValidator
{
get { return _optionValidator ?? (_optionValidator = new CommandLineOptionValidator(this)); }
set { _optionValidator = value; }
}
/// <summary>
/// Gets or sets the <see cref="ICommandLineParserEngine"/> to use for parsing the command line args.
/// </summary>
public ICommandLineParserEngine ParserEngine
{
get { return _parserEngine ?? (_parserEngine = new CommandLineParserEngineMark2()); }
set { _parserEngine = value; }
}
internal IHelpCommandLineOption HelpOption
{
get { return _helpOption ?? (_helpOption = new EmptyHelpCommandLineOption()); }
set { _helpOption = value; }
}
/// <summary>
/// Setup a new <see cref="ICommandLineOptionFluent{T}"/> using the specified short and long Option name.
/// </summary>
/// <param name="shortOption">The short name for the Option. This must not be <c>null</c>, <c>empty</c> or only <c>whitespace</c>.</param>
/// <param name="longOption">The long name for the Option or <c>null</c> if not required.</param>
/// <returns></returns>
/// <exception cref="OptionAlreadyExistsException">
/// A Option with the same <paramref name="shortOption"/> name or <paramref name="longOption"/> name
/// already exists in the <see cref="IFluentCommandLineParser"/>.
/// </exception>
public ICommandLineOptionFluent<T> Setup<T>(char shortOption, string longOption)
{
return SetupInternal<T>(shortOption.ToString(), longOption);
}
/// <summary>
/// Setup a new <see cref="ICommandLineOptionFluent{T}"/> using the specified short and long Option name.
/// </summary>
/// <param name="shortOption">The short name for the Option. This must not be <c>whitespace</c> or a control character.</param>
/// <param name="longOption">The long name for the Option. This must not be <c>null</c>, <c>empty</c> or only <c>whitespace</c>.</param>
/// <returns></returns>
/// <exception cref="OptionAlreadyExistsException">
/// A Option with the same <paramref name="shortOption"/> name or <paramref name="longOption"/> name already exists in the <see cref="IFluentCommandLineParser"/>.
/// </exception>
/// <exception cref="InvalidOptionNameException">
/// Either <paramref name="shortOption"/> or <paramref name="longOption"/> are not valid. <paramref name="shortOption"/> must not be <c>whitespace</c>
/// or a control character. <paramref name="longOption"/> must not be <c>null</c>, <c>empty</c> or only <c>whitespace</c>.
/// </exception>
[Obsolete("Use new overload Setup<T>(char, string) to specify both a short and long option name instead.")]
public ICommandLineOptionFluent<T> Setup<T>(string shortOption, string longOption)
{
return SetupInternal<T>(shortOption, longOption);
}
private ICommandLineOptionFluent<T> SetupInternal<T>(string shortOption, string longOption)
{
var argOption = this.OptionFactory.CreateOption<T>(shortOption, longOption);
if (argOption == null)
throw new InvalidOperationException("OptionFactory is producing unexpected results.");
OptionValidator.Validate(argOption);
this.Options.Add(argOption);
return argOption;
}
/// <summary>
/// Setup a new <see cref="ICommandLineOptionFluent{T}"/> using the specified short Option name.
/// </summary>
/// <param name="shortOption">The short name for the Option. This must not be <c>whitespace</c> or a control character.</param>
/// <returns></returns>
/// <exception cref="OptionAlreadyExistsException">
/// A Option with the same <paramref name="shortOption"/> name already exists in the <see cref="IFluentCommandLineParser"/>.
/// </exception>
public ICommandLineOptionFluent<T> Setup<T>(char shortOption)
{
return SetupInternal<T>(shortOption.ToString(), null);
}
/// <summary>
/// Setup a new <see cref="ICommandLineOptionFluent{T}"/> using the specified long Option name.
/// </summary>
/// <param name="longOption">The long name for the Option. This must not be <c>null</c>, <c>empty</c> or only <c>whitespace</c>.</param>
/// <returns></returns>
/// <exception cref="OptionAlreadyExistsException">
/// A Option with the same <paramref name="longOption"/> name already exists in the <see cref="IFluentCommandLineParser"/>.
/// </exception>
public ICommandLineOptionFluent<T> Setup<T>(string longOption)
{
return SetupInternal<T>(null, longOption);
}
/// <summary>
/// Parses the specified <see><cref>T:System.String[]</cref></see> using the setup Options.
/// </summary>
/// <param name="args">The <see><cref>T:System.String[]</cref></see> to parse.</param>
/// <returns>An <see cref="ICommandLineParserResult"/> representing the results of the parse operation.</returns>
public ICommandLineParserResult Parse(string[] args)
{
var parsedRes = this.ParserEngine.Parse(args);
var parsedOptions=parsedRes.ParsedOptions.ToList();
var result = new CommandLineParserResult { EmptyArgs = parsedOptions.IsNullOrEmpty() };
result.UnMatchedArgs=parsedRes.AdditionalValues;
if (this.HelpOption.ShouldShowHelp(parsedOptions, StringComparison))
{
result.HelpCalled = true;
this.HelpOption.ShowHelp(this.Options);
return result;
}
foreach (var setupOption in this.Options)
{
/*
* Step 1. match the setup Option to one provided in the args by either long or short names
* Step 2. if the key has been matched then bind the value
* Step 3. if the key is not matched and it is required, then add a new error
* Step 4. the key is not matched and optional, bind the default value if available
*/
// Step 1
ICommandLineOption option = setupOption;
var match = parsedOptions.FirstOrDefault(pair =>
pair.Key.Equals(option.ShortName, this.StringComparison) // tries to match the short name
|| pair.Key.Equals(option.LongName, this.StringComparison)); // or else the long name
if (match != null) // Step 2
{
try
{
IEnumerable<string> unmatchedArgs=option.Bind(match);
if (unmatchedArgs != null)
{
result.UnMatchedArgs = result.UnMatchedArgs.Concat(unmatchedArgs);
}
}
catch (OptionSyntaxException)
{
result.Errors.Add(new OptionSyntaxParseError(option, match));
if (option.HasDefault)
option.BindDefault();
}
parsedOptions.Remove(match);
}
else
{
if (option.IsRequired) // Step 3
result.Errors.Add(new ExpectedOptionNotFoundParseError(option));
else if (option.HasDefault)
option.BindDefault(); // Step 4
result.UnMatchedOptions.Add(option);
}
}
parsedOptions.ForEach(item => result.AdditionalOptionsFound.Add(new KeyValuePair<string, string>(item.Key, item.Value)));
result.ErrorText = ErrorFormatter.Format(result.Errors);
return result;
}
/// <summary>
/// Setup the help args.
/// </summary>
/// <param name="helpArgs">The help arguments to register.</param>
public IHelpCommandLineOptionFluent SetupHelp(params string[] helpArgs)
{
var helpOption = this.OptionFactory.CreateHelpOption(helpArgs);
this.HelpOption = helpOption;
return helpOption;
}
/// <summary>
/// Returns the Options that have been setup for this parser.
/// </summary>
IEnumerable<ICommandLineOption> IFluentCommandLineParser.Options
{
get { return Options; }
}
}
}