Skip to content

Commit c4ff552

Browse files
committed
抛出专属异常
1 parent 0f5da6e commit c4ff552

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/DotNetCampus.CommandLine/CommandRunner.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Concurrent;
22
using System.ComponentModel;
33
using DotNetCampus.Cli.Compiler;
4+
using DotNetCampus.Cli.Exceptions;
45
using DotNetCampus.Cli.Utils.Handlers;
56

67
namespace DotNetCampus.Cli;
@@ -195,8 +196,9 @@ public Task<int> RunAsync()
195196

196197
if (handler is null)
197198
{
198-
throw new InvalidOperationException(
199-
$"No command handler found for verb '{_commandLine.GuessedVerbName}'. Please ensure that the command handler is registered correctly.");
199+
throw new CommandVerbNotFoundException(
200+
$"No command handler found for verb '{_commandLine.GuessedVerbName}'. Please ensure that the command handler is registered correctly.",
201+
_commandLine.GuessedVerbName);
200202
}
201203

202204
return handler.RunAsync();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace DotNetCampus.Cli.Exceptions;
2+
3+
/// <summary>
4+
/// 表示输入的命令行在匹配多个命令行参数选项、谓词或处理器时,没有任何一个匹配到的异常。
5+
/// </summary>
6+
public class CommandVerbNotFoundException : CommandLineException
7+
{
8+
/// <summary>
9+
/// 获取命令行谓词的名称。
10+
/// </summary>
11+
public string? VerbName { get; }
12+
13+
/// <summary>
14+
/// 初始化 <see cref="CommandVerbNotFoundException"/> 类的新实例。
15+
/// </summary>
16+
/// <param name="message">异常提示信息。</param>
17+
/// <param name="verbName">命令行谓词的名称。</param>
18+
public CommandVerbNotFoundException(string message, string? verbName)
19+
: base(message)
20+
{
21+
VerbName = verbName;
22+
}
23+
}

0 commit comments

Comments
 (0)