Skip to content

Commit 41059ea

Browse files
committed
Refactored out ParserContext abstraction.
1 parent bbe30b2 commit 41059ea

File tree

7 files changed

+39
-115
lines changed

7 files changed

+39
-115
lines changed

CommandLine.sln.DotSettings.user

+1-1
Large diffs are not rendered by default.

doc/ChangeLog

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Reverted same StyleCop default (see doc/Contribution).
44
* tools/invariantstrtool.exe replaced by T4 template (project removed from GitHub).
5+
* Removed superfluous abstraction.
56

67
2013-02-23 Giacomo Stelluti Scala <[email protected]>
78

src/libcmdline/CommandLine.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
<Compile Include="ParserConfigurator.cs">
8787
<DependentUpon>Parser.cs</DependentUpon>
8888
</Compile>
89-
<Compile Include="ParserContext.cs" />
9089
<Compile Include="Helpers\Assumes.cs" />
9190
<Compile Include="Helpers\ReflectionCache.cs" />
9291
<Compile Include="Extensions\StringExtensions.cs" />

src/libcmdline/Infrastructure/SR.strings.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Changes to this file may cause incorrect behavior and will be lost if
66
// the code is regenerated.
77
// </auto-generated>
8-
//------------------------------------------------------------------------------
8+
//------------------------------------------------------------------------------
99
namespace CommandLine.Infrastructure
1010
{
1111
internal static class SR

src/libcmdline/Parser.cs

+35-39
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
using System.Diagnostics.CodeAnalysis;
2828
using System.Globalization;
2929
using System.Reflection;
30+
using System.Linq;
3031
using CommandLine.Extensions;
3132
using CommandLine.Helpers;
3233
using CommandLine.Infrastructure;
@@ -153,10 +154,9 @@ public bool ParseArguments(string[] args, object options, Action<string, object>
153154

154155
object verbInstance = null;
155156

156-
var context = new ParserContext(args, options);
157-
var result = DoParseArgumentsVerbs(context, ref verbInstance);
158-
159-
onVerbCommand(context.FirstArgument ?? string.Empty, result ? verbInstance : null);
157+
var result = DoParseArgumentsVerbs(args, options, ref verbInstance);
158+
159+
onVerbCommand(args.FirstOrDefault() ?? string.Empty, result ? verbInstance : null);
160160

161161
return result;
162162
}
@@ -220,11 +220,9 @@ public bool ParseArgumentsStrict(string[] args, object options, Action<string, o
220220

221221
object verbInstance = null;
222222

223-
var context = new ParserContext(args, options);
224-
225-
if (!DoParseArgumentsVerbs(context, ref verbInstance))
226-
{
227-
onVerbCommand(context.FirstArgument ?? string.Empty, null);
223+
if (!DoParseArgumentsVerbs(args, options, ref verbInstance))
224+
{
225+
onVerbCommand(args.FirstOrDefault() ?? string.Empty, null);
228226

229227
InvokeAutoBuildIfNeeded(options);
230228

@@ -238,9 +236,9 @@ public bool ParseArgumentsStrict(string[] args, object options, Action<string, o
238236
}
239237

240238
return false;
241-
}
242-
243-
onVerbCommand(context.FirstArgument ?? string.Empty, verbInstance);
239+
}
240+
241+
onVerbCommand(args.FirstOrDefault() ?? string.Empty, verbInstance);
244242
return true;
245243
}
246244

@@ -297,12 +295,10 @@ private bool DoParseArguments(string[] args, object options)
297295
var pair = ReflectionUtil.RetrieveMethod<HelpOptionAttribute>(options);
298296
var helpWriter = Settings.HelpWriter;
299297

300-
var context = new ParserContext(args, options);
301-
302298
if (pair != null && helpWriter != null)
303299
{
304300
// If help can be handled is displayed if is requested or if parsing fails
305-
if (ParseHelp(args, pair.Right) || !DoParseArgumentsCore(context))
301+
if (ParseHelp(args, pair.Right) || !DoParseArgumentsCore(args, options))
306302
{
307303
string helpText;
308304
HelpOptionAttribute.InvokeMethod(options, pair, out helpText);
@@ -311,19 +307,19 @@ private bool DoParseArguments(string[] args, object options)
311307
}
312308

313309
return true;
314-
}
315-
316-
return DoParseArgumentsCore(context);
310+
}
311+
312+
return DoParseArgumentsCore(args, options);
317313
}
318314

319-
private bool DoParseArgumentsCore(ParserContext context)
315+
private bool DoParseArgumentsCore(string[] args, object options)
320316
{
321317
var hadError = false;
322-
var optionMap = OptionMap.Create(context.Target, Settings);
318+
var optionMap = OptionMap.Create(options, Settings);
323319
optionMap.SetDefaults();
324-
var valueMapper = new ValueMapper(context.Target, Settings.ParsingCulture);
320+
var valueMapper = new ValueMapper(options, Settings.ParsingCulture);
325321

326-
var arguments = new StringArrayEnumerator(context.Arguments);
322+
var arguments = new StringArrayEnumerator(args);
327323
while (arguments.MoveNext())
328324
{
329325
var argument = arguments.Current;
@@ -335,10 +331,10 @@ private bool DoParseArgumentsCore(ParserContext context)
335331
var parser = ArgumentParser.Create(argument, Settings.IgnoreUnknownArguments);
336332
if (parser != null)
337333
{
338-
var result = parser.Parse(arguments, optionMap, context.Target);
334+
var result = parser.Parse(arguments, optionMap, options);
339335
if ((result & PresentParserState.Failure) == PresentParserState.Failure)
340336
{
341-
SetParserStateIfNeeded(context.Target, parser.PostParsingState);
337+
SetParserStateIfNeeded(options, parser.PostParsingState);
342338
hadError = true;
343339
continue;
344340
}
@@ -362,54 +358,54 @@ private bool DoParseArgumentsCore(ParserContext context)
362358
return !hadError;
363359
}
364360

365-
private bool DoParseArgumentsVerbs(ParserContext context, ref object verbInstance)
361+
private bool DoParseArgumentsVerbs(string[] args, object options, ref object verbInstance)
366362
{
367-
var verbs = ReflectionUtil.RetrievePropertyList<VerbOptionAttribute>(context.Target);
368-
var helpInfo = ReflectionUtil.RetrieveMethod<HelpVerbOptionAttribute>(context.Target);
369-
if (context.HasNoArguments())
363+
var verbs = ReflectionUtil.RetrievePropertyList<VerbOptionAttribute>(options);
364+
var helpInfo = ReflectionUtil.RetrieveMethod<HelpVerbOptionAttribute>(options);
365+
if (args.Length == 0)
370366
{
371367
if (helpInfo != null || Settings.HelpWriter != null)
372368
{
373-
DisplayHelpVerbText(context.Target, helpInfo, null);
369+
DisplayHelpVerbText(options, helpInfo, null);
374370
}
375371

376372
return false;
377373
}
378374

379-
var optionMap = OptionMap.Create(context.Target, verbs, Settings);
375+
var optionMap = OptionMap.Create(options, verbs, Settings);
380376

381377
// Read the verb from command line arguments
382-
if (TryParseHelpVerb(context.Arguments, context.Target, helpInfo, optionMap))
378+
if (TryParseHelpVerb(args, options, helpInfo, optionMap))
383379
{
384380
// Since user requested help, parsing is considered a fail
385381
return false;
386382
}
387383

388-
var verbOption = optionMap[context.FirstArgument];
384+
var verbOption = optionMap[args.First()];
389385

390386
// User invoked a bad verb name
391387
if (verbOption == null)
392388
{
393389
if (helpInfo != null)
394390
{
395-
DisplayHelpVerbText(context.Target, helpInfo, null);
391+
DisplayHelpVerbText(options, helpInfo, null);
396392
}
397393

398394
return false;
399395
}
400396

401-
verbInstance = verbOption.GetValue(context.Target);
397+
verbInstance = verbOption.GetValue(options);
402398
if (verbInstance == null)
403399
{
404400
// Developer has not provided a default value and did not assign an instance
405-
verbInstance = verbOption.CreateInstance(context.Target);
406-
}
407-
408-
var verbResult = DoParseArgumentsCore(context.ToCoreInstance(verbOption));
401+
verbInstance = verbOption.CreateInstance(options);
402+
}
403+
404+
var verbResult = DoParseArgumentsCore(args.Skip(1).ToArray(), verbInstance);
409405
if (!verbResult && helpInfo != null)
410406
{
411407
// Particular verb parsing failed, we try to print its help
412-
DisplayHelpVerbText(context.Target, helpInfo, context.FirstArgument);
408+
DisplayHelpVerbText(options, helpInfo, args.First());
413409
}
414410

415411
return verbResult;

src/libcmdline/ParserContext.cs

-72
This file was deleted.

src/tests/Unit/Text/VerbsHelpTextFixture.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private void DoCoreTestForIndex(string[] args)
126126
ReflectionUtil.AssemblyFromWhichToPullInformation = Assembly.GetExecutingAssembly();
127127
var parser = new Parser(with => with.UseHelpWriter(testWriter));
128128
var result = parser.ParseArguments(args, options,
129-
(verb, _) =>
129+
(_, __) =>
130130
{
131131
});
132132

0 commit comments

Comments
 (0)