Skip to content

Commit c29a762

Browse files
committed
Minor refactorings to Parser.
1 parent 23c1d8e commit c29a762

7 files changed

+49
-48
lines changed

CommandLine.sln.DotSettings.user

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

README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
Command Line Parser Library 1.9.7.2 stable for CLR.
1+
Command Line Parser Library 1.9.8.1 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
__This library provides _hassle free_ command line parsing with a constantly updated API since 2005.__
55

6+
News:
7+
---
8+
- First attempt to remove main side effects from Parser class (help output excluded).
9+
610
Compatibility:
711
---
812
- .NET Framework 3.5+
@@ -104,11 +108,6 @@ Resources for newcomers:
104108
- [Wiki](https://github.com/gsscoder/commandline/wiki)
105109
- [GNU getopt](http://www.gnu.org/software/libc/manual/html_node/Getopt.html)
106110

107-
Latest Changes:
108-
---
109-
- Promoted to stable.
110-
- Implicit name is now available on ``OptionAttribute`` and ``OptionListAttribute``.
111-
112111
Contacts:
113112
---
114113
Giacomo Stelluti Scala

src/SharedAssemblyInfo.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828

2929
[assembly: AssemblyProduct("Command Line Parser Library")]
3030
[assembly: AssemblyCopyright("Copyright (c) 2005 - 2013 Giacomo Stelluti Scala")]
31-
[assembly: AssemblyVersion("1.9.7.2")]
32-
[assembly: AssemblyFileVersion("1.9.7.2")]
31+
[assembly: AssemblyVersion("1.9.8.1")]
32+
[assembly: AssemblyFileVersion("1.9.8.1")]
3333

34-
[assembly: AssemblyInformationalVersion("1.9.7-stable")]
34+
[assembly: AssemblyInformationalVersion("1.9.8-beta")]
3535
[assembly: NeutralResourcesLanguage("en-US")]

src/libcmdline/Infrastructure/SR.strings

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
ArgumentNullException_ArgsStringArrayCannotBeNull = The arguments string array cannot be null.
2-
ArgumentNullException_OptionsInstanceCannotBeNull = The target options instance cannot be null.
32
ArgumentNullException_ParserSettingsInstanceCannotBeNull = The command line parser settings instance cannot be null.
43
ArgumentNullException_AttributeCannotBeNull = The attribute is mandatory.
54
ArgumentNullException_PropertyCannotBeNull = The property is mandatory.
@@ -15,4 +14,5 @@ ArgumentNullException_OnVerbDelegateCannotBeNull = Delegate executed to capture
1514
ArgumentNullException_ParserSettingsDelegateCannotBeNull = The command line parser settings delegate cannot be null.
1615
InvalidOperationException_ParserSettingsInstanceCanBeUsedOnce = The command line parserSettings instance cannnot be used more than once.
1716
InvalidOperationException_ParserStateInstanceCannotBeNotNull = ParserState instance cannot be supplied.
18-
InvalidOperationException_ParserStateInstanceBadApplied = Cannot apply ParserStateAttribute to a property that does not implement IParserState or is not accessible.
17+
InvalidOperationException_ParserStateInstanceBadApplied = Cannot apply ParserStateAttribute to a property that does not implement IParserState or is not accessible.
18+
ArgumentNullException_OnFailDelegateCannotBeNull = Delegate executed to capture failure event cannot be null.

src/libcmdline/Infrastructure/SR.strings.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ internal static class SR
1313

1414
public const string ArgumentNullException_ArgsStringArrayCannotBeNull = "The arguments string array cannot be null.";
1515

16-
public const string ArgumentNullException_OptionsInstanceCannotBeNull = "The target options instance cannot be null.";
17-
1816
public const string ArgumentNullException_ParserSettingsInstanceCannotBeNull = "The command line parser settings instance cannot be null.";
1917

2018
public const string ArgumentNullException_AttributeCannotBeNull = "The attribute is mandatory.";
@@ -47,5 +45,7 @@ internal static class SR
4745

4846
public const string InvalidOperationException_ParserStateInstanceBadApplied = "Cannot apply ParserStateAttribute to a property that does not implement IParserState or is not accessible.";
4947

48+
public const string ArgumentNullException_OnFailDelegateCannotBeNull = "Delegate executed to capture failure event cannot be null.";
49+
5050
}
5151
}

src/libcmdline/Parser.cs

+33-34
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,24 @@ public static Parser Default
117117
}
118118

119119
/// <summary>
120-
/// Parses a <see cref="System.String"/> array of command line arguments, setting values in <paramref name="options"/>
121-
/// parameter instance's public fields decorated with appropriate attributes. If parsing fails, the method invokes
122-
/// the <paramref name="onFail"/> delegate, if null exits with <see cref="Parser.DefaultExitCodeFail"/>.
120+
/// Parses a <see cref="System.String"/> array of command line arguments returning values as an instance
121+
/// of type <typeparamref name="T"/>. Type public fields should be decorated with appropriate attributes.
122+
/// If parsing fails, the method invokes the <paramref name="onFail"/> delegate.
123123
/// </summary>
124124
/// <param name="args">A <see cref="System.String"/> array of command line arguments.</param>
125-
/// <param name="options">An object's instance used to receive values.
126-
/// Parsing rules are defined using <see cref="CommandLine.BaseOptionAttribute"/> derived types.</param>
127125
/// <param name="onFail">The <see cref="Action"/> delegate executed when parsing fails.</param>
128-
/// <returns>True if parsing process succeed.</returns>
126+
/// <typeparam name="T">An object's type used to receive values.
127+
/// Parsing rules are defined using <see cref="CommandLine.BaseOptionAttribute"/> derived types.</typeparam>
128+
/// <returns>An instance of type <typeparamref name="T"/> with parsed values.</returns>
129129
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="args"/> is null.</exception>
130-
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="options"/> is null.</exception>
130+
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="onFail"/> is null.</exception>
131131
public T ParseArguments<T>(string[] args, Action onFail)
132132
where T : new()
133133
{
134134
Assumes.NotNull(args, "args", SR.ArgumentNullException_ArgsStringArrayCannotBeNull);
135-
//Assumes.NotNull(options, "options", SR.ArgumentNullException_OptionsInstanceCannotBeNull);
135+
Assumes.NotNull(onFail, "onFail", SR.ArgumentNullException_OnFailDelegateCannotBeNull);
136136

137-
var resultAndOptions = DoParseArguments<T>(args);
137+
var resultAndOptions = this.ParseArguments<T>(args);
138138
var result = resultAndOptions.Item1;
139139
var options = resultAndOptions.Item2;
140140

@@ -149,35 +149,34 @@ public T ParseArguments<T>(string[] args, Action onFail)
149149
}
150150

151151
/// <summary>
152-
/// Parses a <see cref="System.String"/> array of command line arguments with verb commands, setting values in <paramref name="options"/>
153-
/// parameter instance's public fields decorated with appropriate attributes. If parsing fails, the method invokes
154-
/// the <paramref name="onFail"/> delegate, if null exits with <see cref="Parser.DefaultExitCodeFail"/>.
152+
/// Parses a <see cref="System.String"/> array of command line arguments returning values as an instance
153+
/// of type <typeparamref name="T"/>. Type public fields should be decorated with appropriate attributes.
154+
/// If parsing fails, the method invokes the <paramref name="onFail"/> delegate.
155155
/// This overload supports verb commands.
156156
/// </summary>
157157
/// <param name="args">A <see cref="System.String"/> array of command line arguments.</param>
158-
/// <param name="options">An instance used to receive values.
159-
/// Parsing rules are defined using <see cref="CommandLine.BaseOptionAttribute"/> derived types.</param>
160158
/// <param name="onVerbCommand">Delegate executed to capture verb command name and instance.</param>
161159
/// <param name="onFail">The <see cref="Action"/> delegate executed when parsing fails.</param>
162-
/// <returns>True if parsing process succeed.</returns>
160+
/// <typeparam name="T">An object's type used to receive values.
161+
/// Parsing rules are defined using <see cref="CommandLine.BaseOptionAttribute"/> derived types.</typeparam>
162+
/// <returns>An instance of type <typeparamref name="T"/> with parsed values.</returns>
163163
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="args"/> is null.</exception>
164-
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="options"/> is null.</exception>
165164
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="onVerbCommand"/> is null.</exception>
165+
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="onFail"/> is null.</exception>
166166
public T ParseArguments<T>(string[] args, Action<string, object> onVerbCommand, Action onFail)
167167
where T : new()
168168
{
169169
Assumes.NotNull(args, "args", SR.ArgumentNullException_ArgsStringArrayCannotBeNull);
170-
//Assumes.NotNull(options, "options", SR.ArgumentNullException_OptionsInstanceCannotBeNull);
171170
Assumes.NotNull(onVerbCommand, "onVerbCommand", SR.ArgumentNullException_OnVerbDelegateCannotBeNull);
171+
Assumes.NotNull(onFail, "onFail", SR.ArgumentNullException_OnFailDelegateCannotBeNull);
172172

173-
var resultAndOptionsAndVerbInstance = DoParseArgumentsVerbs<T>(args);
173+
var resultAndOptionsAndVerbInstance = this.ParseArgumentsVerbs<T>(args);
174174

175175
var result = resultAndOptionsAndVerbInstance.Item1;
176176
var options = resultAndOptionsAndVerbInstance.Item2;
177177
var verbInstance = resultAndOptionsAndVerbInstance.Item3;
178178

179-
//TODO: evaluate mutually activation of delegates
180-
179+
// TODO: mutually activate delegates?
181180
onVerbCommand(args.FirstOrDefault() ?? string.Empty, result ? verbInstance : null);
182181

183182
if (!result)
@@ -259,12 +258,20 @@ private static T SetParserStateIfNeeded<T>(T options, IEnumerable<ParsingError>
259258
return options;
260259
}
261260

261+
private static void DisplayHelpText<T>(T options, Pair<MethodInfo, HelpOptionAttribute> pair, TextWriter helpWriter)
262+
where T : new()
263+
{
264+
string helpText;
265+
HelpOptionAttribute.InvokeMethod(options, pair, out helpText); // TODO: refactor this
266+
helpWriter.Write(helpText);
267+
}
268+
262269
private static StringComparison GetStringComparison(ParserSettings settings)
263270
{
264271
return settings.CaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
265272
}
266273

267-
private Tuple<bool, T> DoParseArguments<T>(string[] args)
274+
private Tuple<bool, T> ParseArguments<T>(string[] args)
268275
where T : new()
269276
{
270277
var options = new T();
@@ -280,7 +287,7 @@ private Tuple<bool, T> DoParseArguments<T>(string[] args)
280287
return new Tuple<bool, T>(false, options);
281288
}
282289

283-
var optionsAndResult= this.DoParseArgumentsCore(args, options);
290+
var optionsAndResult = this.ParseArgumentsCore(args, options);
284291
var result = optionsAndResult.Item1;
285292
options = optionsAndResult.Item2;
286293

@@ -291,18 +298,10 @@ private Tuple<bool, T> DoParseArguments<T>(string[] args)
291298
}
292299
}
293300

294-
return DoParseArgumentsCore(args, options);
295-
}
296-
297-
private static void DisplayHelpText<T>(T options, Pair<MethodInfo, HelpOptionAttribute> pair, TextWriter helpWriter)
298-
where T : new()
299-
{
300-
string helpText;
301-
HelpOptionAttribute.InvokeMethod(options, pair, out helpText); // TODO: refactor this
302-
helpWriter.Write(helpText);
301+
return this.ParseArgumentsCore(args, options);
303302
}
304303

305-
private Tuple<bool,T> DoParseArgumentsCore<T>(string[] args, T options)
304+
private Tuple<bool, T> ParseArgumentsCore<T>(string[] args, T options)
306305
where T : new()
307306
{
308307
var hadError = false;
@@ -349,7 +348,7 @@ private Tuple<bool,T> DoParseArgumentsCore<T>(string[] args, T options)
349348
return new Tuple<bool, T>(!hadError, options);
350349
}
351350

352-
private Tuple<bool, T, object> DoParseArgumentsVerbs<T>(string[] args)
351+
private Tuple<bool, T, object> ParseArgumentsVerbs<T>(string[] args)
353352
where T : new()
354353
{
355354
var options = new T();
@@ -394,7 +393,7 @@ private Tuple<bool, T, object> DoParseArgumentsVerbs<T>(string[] args)
394393
verbInstance = verbOption.CreateInstance(options);
395394
}
396395

397-
var resultAndVerbInstance = DoParseArgumentsCore(args.Skip(1).ToArray(), verbInstance);
396+
var resultAndVerbInstance = this.ParseArgumentsCore(args.Skip(1).ToArray(), verbInstance);
398397
var result = resultAndVerbInstance.Item1;
399398
verbInstance = resultAndVerbInstance.Item2;
400399

src/libcmdline/ParserSettings.cs

+3
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ public CultureInfo ParsingCulture
229229
}
230230
}
231231

232+
/// <summary>
233+
/// Gets or sets a value indicating whether to invoke <see cref="CommandLine.Text.HelpText.AutoBuild(object,Action{CommandLine.Text.HelpText},bool)"/> if help method is not defined.
234+
/// </summary>
232235
public bool DynamicAutoBuild
233236
{
234237
get

0 commit comments

Comments
 (0)