Skip to content

Commit cd7a0ce

Browse files
Keboojonsequitur
authored andcommitted
Cleaning up MethodInfoHandler
1 parent 9315483 commit cd7a0ce

File tree

3 files changed

+13
-25
lines changed

3 files changed

+13
-25
lines changed

src/System.CommandLine/Binding/MethodInfoHandlerDescriptor.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,10 @@ public MethodInfoHandlerDescriptor(
2424

2525
public override ICommandHandler GetCommandHandler()
2626
{
27-
if (_invocationTarget is null)
28-
{
29-
return new ModelBindingCommandHandler(
30-
_handlerMethodInfo,
31-
this);
32-
}
33-
else
34-
{
35-
return new ModelBindingCommandHandler(
36-
_handlerMethodInfo,
37-
this,
38-
_invocationTarget);
39-
}
27+
return new ModelBindingCommandHandler(
28+
_handlerMethodInfo,
29+
this,
30+
_invocationTarget);
4031
}
4132

4233
public override ModelDescriptor Parent => ModelDescriptor.FromType(_handlerMethodInfo.DeclaringType);

src/System.CommandLine/Invocation/ModelBindingCommandHandler.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,22 @@ public class ModelBindingCommandHandler : ICommandHandler
2222

2323
public ModelBindingCommandHandler(
2424
MethodInfo handlerMethodInfo,
25-
IMethodDescriptor methodDescriptor)
25+
IMethodDescriptor methodDescriptor,
26+
object? invocationTarget)
2627
{
2728
_handlerMethodInfo = handlerMethodInfo ?? throw new ArgumentNullException(nameof(handlerMethodInfo));
2829
_invocationTargetBinder = _handlerMethodInfo.IsStatic
2930
? null
3031
: new ModelBinder(_handlerMethodInfo.ReflectedType);
3132
_methodDescriptor = methodDescriptor ?? throw new ArgumentNullException(nameof(methodDescriptor));
33+
_invocationTarget = invocationTarget;
3234
}
3335

3436
public ModelBindingCommandHandler(
3537
MethodInfo handlerMethodInfo,
36-
IMethodDescriptor methodDescriptor,
37-
object? invocationTarget)
38-
:this(handlerMethodInfo, methodDescriptor )
39-
{
40-
_invocationTarget = invocationTarget;
41-
}
38+
IMethodDescriptor methodDescriptor)
39+
: this(handlerMethodInfo, methodDescriptor, null)
40+
{ }
4241

4342
public ModelBindingCommandHandler(
4443
Delegate handlerDelegate,

src/System.CommandLine/Parsing/ParseResult.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,10 @@ public T ValueForOption<T>(Option<T> option)
166166
[return: MaybeNull]
167167
private T ValueForOption<T>(Option option)
168168
{
169-
if (FindResultFor(option) is { } result)
169+
if (FindResultFor(option) is { } result &&
170+
result.GetValueOrDefault<T>() is { } t)
170171
{
171-
if (result.GetValueOrDefault<T>() is { } t)
172-
{
173-
return t;
174-
}
172+
return t;
175173
}
176174

177175
return (T)Binder.GetDefaultValue(option.Argument.ArgumentType)!;

0 commit comments

Comments
 (0)