Skip to content

Commit 14e1dc8

Browse files
authored
Commands that throw exception cause parsing to fail (#28)
* Update C# Version and nuspec * Create default parser and constructor for CommandLineParserOptions * Add test for multiple non required commands with required options * Fix #25 non required commands will no longer trigger an error result. * Fix #24 * Add ctor tests * Fixes #27
1 parent 13e976e commit 14e1dc8

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

CommandLineParser.Tests/CommandLineParserTests.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Threading;
1+
using System;
2+
using System.Linq;
3+
using System.Threading;
24

35
using MatthiWare.CommandLine;
46
using MatthiWare.CommandLine.Abstractions;
@@ -109,6 +111,26 @@ public void CommandLinerParserPassesResolverAndContainerCorreclty()
109111
Assert.Equal(options, parser.ParserOptions);
110112
}
111113

114+
[Fact]
115+
public void AutoExecuteCommandsWithExceptionDoesntCrashTheParser()
116+
{
117+
var parser = new CommandLineParser();
118+
119+
var ex = new Exception("uh-oh");
120+
121+
parser.AddCommand()
122+
.Name("test")
123+
.InvokeCommand(true)
124+
.Required(true)
125+
.OnExecuting(_ => throw ex);
126+
127+
var result = parser.Parse(new[] { "test" });
128+
129+
Assert.True(result.HasErrors);
130+
131+
Assert.Equal(ex, result.Errors.First());
132+
}
133+
112134
[Fact]
113135
public void CommandLineParserUsesArgumentFactoryCorrectly()
114136
{

CommandLineParser/CommandLineParser`TOption.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,11 @@ private void PrintErrors(IReadOnlyCollection<Exception> errors)
268268

269269
private void PrintHelp() => Printer.PrintUsage();
270270

271-
private void AutoExecuteCommands(IParserResult<TOption> result)
271+
private void AutoExecuteCommands(ParseResult<TOption> result)
272272
{
273273
if (result.HasErrors) return;
274274

275-
ExecuteCommandParserResults(result.CommandResults.Where(r => r.Command.AutoExecute));
275+
ExecuteCommandParserResults(result, result.CommandResults.Where(sub => sub.Command.AutoExecute));
276276
}
277277

278278
private bool HelpRequested(ParseResult<TOption> result, CommandLineOptionBase option, ArgumentModel model)
@@ -291,13 +291,27 @@ private bool HelpRequested(ParseResult<TOption> result, CommandLineOptionBase op
291291
return false;
292292
}
293293

294-
private void ExecuteCommandParserResults(IEnumerable<ICommandParserResult> results)
294+
private void ExecuteCommandParserResults(ParseResult<TOption> results, IEnumerable<ICommandParserResult> cmds)
295295
{
296-
foreach (var cmd in results)
297-
cmd.ExecuteCommand();
296+
var errors = new List<Exception>();
297+
298+
foreach (var cmd in cmds)
299+
{
300+
try
301+
{
302+
cmd.ExecuteCommand();
303+
}
304+
catch (Exception ex)
305+
{
306+
errors.Add(ex);
307+
}
308+
}
309+
310+
if (errors.Any())
311+
results.MergeResult(errors);
298312

299-
foreach (var cmd in results)
300-
ExecuteCommandParserResults(cmd.SubCommands.Where(sub => sub.Command.AutoExecute));
313+
foreach (var cmd in cmds)
314+
ExecuteCommandParserResults(results, cmd.SubCommands.Where(sub => sub.Command.AutoExecute));
301315
}
302316

303317
private void ParseCommands(IList<Exception> errors, ParseResult<TOption> result, IArgumentManager argumentManager)

0 commit comments

Comments
 (0)