Skip to content

Commit fef6558

Browse files
committed
Added ParserConfigurator to build the parser with lambda expressions (see isse #42).
1 parent e9be6fa commit fef6558

13 files changed

+235
-30
lines changed

CommandLine.sln.DotSettings.user

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

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Command Line Parser Library 1.9.4.207 beta for CLR.
1+
Command Line Parser Library 1.9.4.209 beta for CLR.
22
===
33
The Command Line Parser Library offers to CLR applications a clean and concise API for manipulating command line arguments and related tasks defining switches, options and verb commands. It allows you to display an help screen with an high degree of customization and a simple way to report syntax errors to the end user. Everything that is boring and repetitive to be programmed stands up on library shoulders, letting developers concentrate on core logic.
44
__The search for the command line parser for your application is over, with this library you got a solid parsing API constantly updated since 2005.__

Rakefile.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PRODUCT = "Command Line Parser Library"
22
DESCRIPTION = "Command Line Parser Library allows CLR applications to define a syntax for parsing command line arguments."
33
INF_VERSION = "1.9"
4-
VERSION = INF_VERSION + ".4.205"
4+
VERSION = INF_VERSION + ".4.209"
55
COPYRIGHT = "Copyright (c) 2005 - 2013 Giacomo Stelluti Scala"
66
LICENSE_URL = "https://raw.github.com/gsscoder/commandline/master/doc/LICENSE"
77
PROJECT_URL = "https://github.com/gsscoder/commandline"

doc/ChangeLog

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2013-01-31 Giacomo Stelluti Scala <[email protected]>
2+
3+
* Added readonly property Settings of type ParserSettings to IParser, Parser.
4+
* Added ParserConfigurator to configure the parser with lambda expressions (see isse #42).
5+
* Version incremented to 1.9.4.209 beta.
6+
17
2013-01-31 Giacomo Stelluti Scala <[email protected]>
28

39
* Namespace '.Internal' renamed to '.Core'.
@@ -9,7 +15,7 @@
915
- CommandLineParserSettings -> ParserSettings
1016
- CommandLineParserException -> ParserException
1117
* FxCop can scan Debug assembly (because now Debug == Release assembly).
12-
* Version incremented to 1.9.4.207 beta.
18+
* Version incremented to 1.9.4.207 beta.
1319

1420
2013-01-29 Giacomo Stelluti Scala <[email protected]>
1521

doc/PublicAPI.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ Public API Changes:
99
- Version 1.9.4.123: Breaking, ``HandleParsingErrorsDelegate`` renamed to ``ParsingErrorsHandler``, ``MultiLineTextAttribute`` renamed to ``MultilineTextAttribute``. Non breaking, refactoring (see ChangeLog).
1010
- Version 1.9.4.127: 'Partially' non breaking, ``OptionAttribute`` is now sealed. ``OptionArrayAttribute`` and ``OptionListAttribute`` derives from ``BaseOptionAttribute`` (update your custom types too).
1111
- Version 1.9.4.201: Non breaking, introduced ``ValueOptionAttribute`` enhancement of issue #33.
12-
- Version 1.9.4.207: Breaking: CommandLineParser, ICommandLineParser, CommandLineParserSettings, CommandLineParserException renamed to Parser, IParser, ParserSettings, ParserException as explained [here](https://github.com/gsscoder/commandline/issues/48).
12+
- Version 1.9.4.207: Breaking: ``CommandLineParser``, ``ICommandLineParser``, ``CommandLineParserSettings``, ``CommandLineParserException`` renamed to ``Parser``, ``IParser``, ``ParserSettings``, ``ParserException`` as explained [here](https://github.com/gsscoder/commandline/issues/48).
13+
- Version 1.9.4.209: Non breaking, added fluent builder (``ParserConfigurator``, see issue #42).

src/CommonAssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
using System.Runtime.InteropServices;
55
[assembly: AssemblyProduct("Command Line Parser Library")]
66
[assembly: AssemblyCopyright("Copyright (c) 2005 - 2013 Giacomo Stelluti Scala")]
7-
[assembly: AssemblyVersion("1.9.4.205")]
8-
[assembly: AssemblyFileVersion("1.9.4.205")]
7+
[assembly: AssemblyVersion("1.9.4.209")]
8+
[assembly: AssemblyFileVersion("1.9.4.209")]
99

1010
[assembly: AssemblyInformationalVersion("1.9")]
1111
[assembly: NeutralResourcesLanguage("en-US")]

src/libcmdline/CommandLine.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@
8181
<Compile Include="Attributes\ValueListAttribute.cs" />
8282
<Compile Include="Attributes\ValueOptionAttribute.cs" />
8383
<Compile Include="Core\PropertyWriter.cs" />
84+
<Compile Include="IHideObjectMembers.cs" />
85+
<Compile Include="ParserConfigurator.cs" />
8486
<Compile Include="ParserContext.cs" />
8587
<Compile Include="Helpers\Assumes.cs" />
8688
<Compile Include="Core\SR.strings.cs">

src/libcmdline/IHideObjectMembers.cs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.ComponentModel;
3+
4+
namespace CommandLine
5+
{
6+
/// <summary>
7+
/// Helper interface used to hide the base <see cref="Object"/> members from the fluent API to make it much cleaner
8+
/// in Visual Studio intellisense.
9+
/// </summary>
10+
/// <remarks>Created by Daniel Cazzulino http://www.clariusconsulting.net/blogs/kzu/archive/2008/03/10/58301.aspx</remarks>
11+
[EditorBrowsable(EditorBrowsableState.Never)]
12+
public interface IHideObjectMembers
13+
{
14+
/// <summary>
15+
/// Hides the <see cref="Equals"/> method.
16+
/// </summary>
17+
[EditorBrowsable(EditorBrowsableState.Never)]
18+
bool Equals(object obj);
19+
20+
/// <summary>
21+
/// Hides the <see cref="GetHashCode"/> method.
22+
/// </summary>
23+
[EditorBrowsable(EditorBrowsableState.Never)]
24+
int GetHashCode();
25+
26+
/// <summary>
27+
/// Hides the <see cref="GetType"/> method.
28+
/// </summary>
29+
[EditorBrowsable(EditorBrowsableState.Never)]
30+
Type GetType();
31+
32+
/// <summary>
33+
/// Hides the <see cref="ToString"/> method.
34+
/// </summary>
35+
[EditorBrowsable(EditorBrowsableState.Never)]
36+
string ToString();
37+
}
38+
}

src/libcmdline/IParser.cs

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ namespace CommandLine
3737
/// </summary>
3838
public interface IParser
3939
{
40+
/// <summary>
41+
/// Gets the instance of <see cref="CommandLine.ParserSettings"/> in use.
42+
/// </summary>
43+
ParserSettings Settings { get; }
44+
4045
/// <summary>
4146
/// Parses a <see cref="System.String"/> array of command line arguments, setting values in <paramref name="options"/>
4247
/// parameter instance's public fields decorated with appropriate attributes.

src/libcmdline/Parser.cs

+42-23
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,30 @@ namespace CommandLine
4747
public class Parser : IParser, IDisposable
4848
{
4949
/// <summary>
50-
/// Initializes a new instance of the <see cref="Parser"/> class.
50+
/// Initializes a new instance of the <see cref="CommandLine.Parser"/> class.
5151
/// </summary>
5252
public Parser()
5353
{
54-
_settings = new ParserSettings();
54+
Settings = new ParserSettings();
55+
}
56+
57+
/// <summary>
58+
/// Initializes a new instance of the <see cref="CommandLine.Parser"/> class.
59+
/// </summary>
60+
/// <param name="configuration">The configuration that should by used by the parser.</param>
61+
public Parser(Action<ParserConfigurator> configuration)
62+
: this()
63+
{
64+
var configurator = new ParserConfigurator(this);
65+
configuration.Invoke(configurator);
5566
}
5667

5768
// ReSharper disable UnusedParameter.Local
5869
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "singleton", Justification = "The constructor that accepts a boolean is designed to support default singleton, the parameter is ignored.")]
5970
private Parser(bool singleton)
6071
// ReSharper restore UnusedParameter.Local
6172
{
62-
_settings = new ParserSettings(false, false, Console.Error);
73+
Settings = new ParserSettings(false, false, Console.Error);
6374
}
6475

6576
/// <summary>
@@ -71,7 +82,7 @@ private Parser(bool singleton)
7182
public Parser(ParserSettings settings)
7283
{
7384
Assumes.NotNull(settings, "settings", SR.ArgumentNullException_CommandLineParserSettingsInstanceCannotBeNull);
74-
_settings = settings;
85+
Settings = settings;
7586
}
7687

7788
/// <summary>
@@ -82,6 +93,15 @@ public static IParser Default
8293
get { return DefaultParser; }
8394
}
8495

96+
/// <summary>
97+
/// Gets the instance of <see cref="CommandLine.ParserSettings"/> in use.
98+
/// </summary>
99+
public ParserSettings Settings
100+
{
101+
get;
102+
private set;
103+
}
104+
85105
/// <summary>
86106
/// Parses a <see cref="System.String"/> array of command line arguments, setting values in <paramref name="options"/>
87107
/// parameter instance's public fields decorated with appropriate attributes.
@@ -118,14 +138,14 @@ public virtual bool ParseArguments(string[] args, object options, TextWriter hel
118138
Assumes.NotNull(args, "args", SR.ArgumentNullException_ArgsStringArrayCannotBeNull);
119139
Assumes.NotNull(options, "options", SR.ArgumentNullException_OptionsInstanceCannotBeNull);
120140

121-
_settings.HelpWriter = helpWriter;
141+
Settings.HelpWriter = helpWriter;
122142
return DoParseArguments(args, options);
123143
}
124144

125145
private bool DoParseArguments(string[] args, object options)
126146
{
127147
var pair = ReflectionUtil.RetrieveMethod<HelpOptionAttribute>(options);
128-
var helpWriter = _settings.HelpWriter;
148+
var helpWriter = Settings.HelpWriter;
129149

130150
_context = new ParserContext(args, options);
131151

@@ -155,7 +175,7 @@ private bool DoParseArgumentsDispatcher(ParserContext context)
155175
private bool DoParseArgumentsCore(ParserContext context)
156176
{
157177
var hadError = false;
158-
var optionMap = OptionMap.Create(context.Target, _settings);
178+
var optionMap = OptionMap.Create(context.Target, Settings);
159179
optionMap.SetDefaults();
160180
var valueMapper = context.Target.CreateValueMapper();
161181

@@ -167,7 +187,7 @@ private bool DoParseArgumentsCore(ParserContext context)
167187
{
168188
continue;
169189
}
170-
var parser = ArgumentParser.Create(argument, _settings.IgnoreUnknownArguments);
190+
var parser = ArgumentParser.Create(argument, Settings.IgnoreUnknownArguments);
171191
if (parser != null)
172192
{
173193
var result = parser.Parse(arguments, optionMap, context.Target);
@@ -199,7 +219,7 @@ private bool DoParseArgumentsCore(ParserContext context)
199219

200220
private bool ParseHelp(string[] args, HelpOptionAttribute helpOption)
201221
{
202-
var caseSensitive = _settings.CaseSensitive;
222+
var caseSensitive = Settings.CaseSensitive;
203223
foreach (var arg in args)
204224
{
205225
if (helpOption.ShortName != null)
@@ -261,7 +281,7 @@ public bool WasVerbOptionInvoked(string verb)
261281
{
262282
return false;
263283
}
264-
return string.Compare(_context.FirstArgument, verb, _settings.StringComparison) == 0;
284+
return string.Compare(_context.FirstArgument, verb, Settings.StringComparison) == 0;
265285
}
266286

267287
private bool DoParseArgumentsVerbs(ParserContext context)
@@ -270,13 +290,13 @@ private bool DoParseArgumentsVerbs(ParserContext context)
270290
var helpInfo = ReflectionUtil.RetrieveMethod<HelpVerbOptionAttribute>(context.Target);
271291
if (context.HasNoArguments())
272292
{
273-
if (helpInfo != null || _settings.HelpWriter != null)
293+
if (helpInfo != null || Settings.HelpWriter != null)
274294
{
275295
DisplayHelpVerbText(context.Target, helpInfo, null);
276296
}
277297
return false;
278298
}
279-
var optionMap = OptionMap.Create(context.Target, verbs, _settings);
299+
var optionMap = OptionMap.Create(context.Target, verbs, Settings);
280300
// Read the verb from command line arguments
281301
if (TryParseHelpVerb(context.Arguments, context.Target, helpInfo, optionMap))
282302
{
@@ -309,10 +329,10 @@ private bool DoParseArgumentsVerbs(ParserContext context)
309329

310330
private bool TryParseHelpVerb(string[] args, object options, Pair<MethodInfo, HelpVerbOptionAttribute> helpInfo, OptionMap optionMap)
311331
{
312-
var helpWriter = _settings.HelpWriter;
332+
var helpWriter = Settings.HelpWriter;
313333
if (helpInfo != null && helpWriter != null)
314334
{
315-
if (string.Compare(args[0], helpInfo.Right.LongName, _settings.StringComparison) == 0)
335+
if (string.Compare(args[0], helpInfo.Right.LongName, Settings.StringComparison) == 0)
316336
{
317337
// User explicitly requested help
318338
var verb = args.Length > 1 ? args[1] : null;
@@ -346,9 +366,9 @@ private void DisplayHelpVerbText(object options, Pair<MethodInfo, HelpVerbOption
346366
{
347367
HelpVerbOptionAttribute.InvokeMethod(options, helpInfo, verb, out helpText);
348368
}
349-
if (_settings.HelpWriter != null)
369+
if (Settings.HelpWriter != null)
350370
{
351-
_settings.HelpWriter.Write(helpText);
371+
Settings.HelpWriter.Write(helpText);
352372
}
353373
}
354374
#endregion
@@ -418,7 +438,7 @@ public virtual bool ParseArgumentsStrict(string[] args, object options, TextWrit
418438
Assumes.NotNull(args, "args", SR.ArgumentNullException_ArgsStringArrayCannotBeNull);
419439
Assumes.NotNull(options, "options", SR.ArgumentNullException_OptionsInstanceCannotBeNull);
420440

421-
_settings.HelpWriter = helpWriter;
441+
Settings.HelpWriter = helpWriter;
422442

423443
return DoParseArgumentsStrict(args, options, DefaultExitCodeFail);
424444
}
@@ -443,7 +463,7 @@ public virtual bool ParseArguments(string[] args, object options, TextWriter hel
443463
Assumes.NotNull(args, "args", SR.ArgumentNullException_ArgsStringArrayCannotBeNull);
444464
Assumes.NotNull(options, "options", SR.ArgumentNullException_OptionsInstanceCannotBeNull);
445465

446-
_settings.HelpWriter = helpWriter;
466+
Settings.HelpWriter = helpWriter;
447467

448468
return DoParseArgumentsStrict(args, options, exitCode);
449469
}
@@ -468,15 +488,15 @@ private bool DoParseArgumentsStrict(string[] args, object options, int exitCode)
468488

469489
private void InvokeAutoBuildIfNeeded(object options)
470490
{
471-
if (_settings.HelpWriter == null ||
491+
if (Settings.HelpWriter == null ||
472492
options.HasHelp() ||
473493
options.HasVerbHelp())
474494
{
475495
return;
476496
}
477497

478498
// We print help text for the user
479-
_settings.HelpWriter.Write(HelpText.AutoBuild(options,
499+
Settings.HelpWriter.Write(HelpText.AutoBuild(options,
480500
current => HelpText.DefaultParsingErrorsHandler(options, current), options.HasVerbs()));
481501
}
482502
#endregion
@@ -519,9 +539,9 @@ private void Dispose(bool disposing)
519539
}
520540
if (disposing)
521541
{
522-
if (_settings != null)
542+
if (Settings != null)
523543
{
524-
_settings.Dispose();
544+
Settings.Dispose();
525545
}
526546
_disposed = true;
527547
}
@@ -538,6 +558,5 @@ private void Dispose(bool disposing)
538558
private ParserContext _context;
539559
private bool _disposed;
540560
private static readonly IParser DefaultParser = new Parser(true);
541-
private readonly ParserSettings _settings;
542561
}
543562
}

0 commit comments

Comments
 (0)