Skip to content

Commit 994e345

Browse files
committed
删除不必要的 IVerbCreator 接口,同时也能避免创建什么用都没有的对象
1 parent bedf89f commit 994e345

File tree

3 files changed

+5
-28
lines changed

3 files changed

+5
-28
lines changed

src/DotNetCampus.CommandLine.Analyzer/Generators/VerbCreatorGenerator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ namespace {{model.Namespace}};
6969
/// <summary>
7070
/// 辅助 <see cref="{{model.OptionsType.ToGlobalDisplayString()}}"/> 生成命令行选项、谓词或处理函数的创建。
7171
/// </summary>
72-
internal sealed class {{model.GetVerbCreatorTypeName()}} : global::DotNetCampus.Cli.Compiler.IVerbCreator<{{model.OptionsType.ToGlobalDisplayString()}}>
72+
internal sealed class {{model.GetVerbCreatorTypeName()}}
7373
{
74-
public {{model.OptionsType.ToGlobalDisplayString()}} CreateInstance(global::DotNetCampus.Cli.CommandLine commandLine)
74+
public static {{model.OptionsType.ToGlobalDisplayString()}} CreateInstance(global::DotNetCampus.Cli.CommandLine commandLine)
7575
{
7676
var result = new {{model.OptionsType.ToGlobalDisplayString()}}
7777
{
@@ -214,7 +214,7 @@ private string GenerateCommandRunnerRegisterCode(CommandOptionsGeneratingModel m
214214
// {{model.OptionsType.Name}} { VerbName = {{verbCode}} }
215215
global::DotNetCampus.Cli.CommandRunner.Register<{{model.OptionsType.ToGlobalDisplayString()}}>(
216216
{{verbCode}},
217-
cl => new global::{{model.Namespace}}.{{model.GetVerbCreatorTypeName()}}().CreateInstance(cl));
217+
cl => global::{{model.Namespace}}.{{model.GetVerbCreatorTypeName()}}.CreateInstance(cl));
218218
""";
219219
}
220220

@@ -248,7 +248,7 @@ private string GenerateAssemblyCommandHandlerMatchCode(IGrouping<string?, Comman
248248
if (model.IsHandler)
249249
{
250250
return $"""
251-
{(group.Key is { } vn ? $"\"{vn}\"" : "null")} => new global::{model.Namespace}.{model.GetVerbCreatorTypeName()}().CreateInstance(cl),
251+
{(group.Key is { } vn ? $"\"{vn}\"" : "null")} => global::{model.Namespace}.{model.GetVerbCreatorTypeName()}.CreateInstance(cl),
252252
""";
253253
}
254254
else

src/DotNetCampus.CommandLine/Compiler/IVerbCreator.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/DotNetCampus.CommandLine/Utils/Handlers/TaskCommandHandler.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
using DotNetCampus.Cli.Compiler;
2-
31
namespace DotNetCampus.Cli.Utils.Handlers;
42

53
internal sealed class TaskCommandHandler<TOptions>(
64
Func<TOptions> optionsCreator,
7-
Func<TOptions, Task<int>> handler) : ICommandHandler, IVerbCreator<TaskCommandHandler<TOptions>>
5+
Func<TOptions, Task<int>> handler) : ICommandHandler
86
where TOptions : class
97
{
108
private TOptions? _options;
119

12-
public TaskCommandHandler<TOptions> CreateInstance(CommandLine cl)
13-
{
14-
return this;
15-
}
16-
1710
public Task<int> RunAsync()
1811
{
1912
_options ??= optionsCreator();

0 commit comments

Comments
 (0)