Skip to content

Commit 55dbf39

Browse files
authored
SetHandler improvements (#1729)
* reduce SetHandler overloads to max 8 generic type parameters * remove params from SetHandler, make parameter list match generic type param arity * cleanup, update approvals * update trimming and NativeAOT test code
1 parent 372d408 commit 55dbf39

File tree

21 files changed

+460
-1216
lines changed

21 files changed

+460
-1216
lines changed

src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -103,40 +103,26 @@
103103
public System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<System.String,System.Collections.Generic.IEnumerable<System.String>>> GetEnumerator()
104104
public System.Boolean TryGetValues(System.String name, ref System.Collections.Generic.IReadOnlyList<System.String> values)
105105
public static class Handler
106+
public static System.Void SetHandler(this Command command, System.Action<System.CommandLine.Invocation.InvocationContext> handle)
106107
public static System.Void SetHandler(this Command command, System.Action handle)
107108
public static System.Void SetHandler(this Command command, System.Func<System.Threading.Tasks.Task> handle)
108-
public static System.Void SetHandler<T>(this Command command, Action<T> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
109-
public static System.Void SetHandler<T1, T2>(this Command command, Action<T1,T2> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
110-
public static System.Void SetHandler<T1, T2, T3>(this Command command, Action<T1,T2,T3> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
111-
public static System.Void SetHandler<T1, T2, T3, T4>(this Command command, Action<T1,T2,T3,T4> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
112-
public static System.Void SetHandler<T1, T2, T3, T4, T5>(this Command command, Action<T1,T2,T3,T4,T5> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
113-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6>(this Command command, Action<T1,T2,T3,T4,T5,T6> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
114-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7>(this Command command, Action<T1,T2,T3,T4,T5,T6,T7> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
115-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8>(this Command command, Action<T1,T2,T3,T4,T5,T6,T7,T8> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
116-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8, T9>(this Command command, Action<T1,T2,T3,T4,T5,T6,T7,T8,T9> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
117-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(this Command command, Action<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
118-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(this Command command, Action<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
119-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(this Command command, Action<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
120-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13>(this Command command, Action<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
121-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14>(this Command command, Action<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
122-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15>(this Command command, Action<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
123-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>(this Command command, Action<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
124-
public static System.Void SetHandler<T>(this Command command, Func<T,System.Threading.Tasks.Task> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
125-
public static System.Void SetHandler<T1, T2>(this Command command, Func<T1,T2,System.Threading.Tasks.Task> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
126-
public static System.Void SetHandler<T1, T2, T3>(this Command command, Func<T1,T2,T3,System.Threading.Tasks.Task> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
127-
public static System.Void SetHandler<T1, T2, T3, T4>(this Command command, Func<T1,T2,T3,T4,System.Threading.Tasks.Task> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
128-
public static System.Void SetHandler<T1, T2, T3, T4, T5>(this Command command, Func<T1,T2,T3,T4,T5,System.Threading.Tasks.Task> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
129-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6>(this Command command, Func<T1,T2,T3,T4,T5,T6,System.Threading.Tasks.Task> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
130-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7>(this Command command, Func<T1,T2,T3,T4,T5,T6,T7,System.Threading.Tasks.Task> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
131-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8>(this Command command, Func<T1,T2,T3,T4,T5,T6,T7,T8,System.Threading.Tasks.Task> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
132-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8, T9>(this Command command, Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,System.Threading.Tasks.Task> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
133-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(this Command command, Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,System.Threading.Tasks.Task> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
134-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(this Command command, Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,System.Threading.Tasks.Task> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
135-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(this Command command, Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,System.Threading.Tasks.Task> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
136-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13>(this Command command, Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,System.Threading.Tasks.Task> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
137-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14>(this Command command, Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,System.Threading.Tasks.Task> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
138-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15>(this Command command, Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,System.Threading.Tasks.Task> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
139-
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>(this Command command, Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,System.Threading.Tasks.Task> handle, System.CommandLine.Binding.IValueDescriptor[] symbols)
109+
public static System.Void SetHandler(this Command command, System.Func<System.CommandLine.Invocation.InvocationContext,System.Threading.Tasks.Task> handle)
110+
public static System.Void SetHandler<T>(this Command command, Action<T> handle, IValueDescriptor<T> symbol)
111+
public static System.Void SetHandler<T>(this Command command, Func<T,System.Threading.Tasks.Task> handle, IValueDescriptor<T> symbol)
112+
public static System.Void SetHandler<T1, T2>(this Command command, Action<T1,T2> handle, IValueDescriptor<T1> symbol1, IValueDescriptor<T2> symbol2)
113+
public static System.Void SetHandler<T1, T2>(this Command command, Func<T1,T2,System.Threading.Tasks.Task> handle, IValueDescriptor<T1> symbol1, IValueDescriptor<T2> symbol2)
114+
public static System.Void SetHandler<T1, T2, T3>(this Command command, Action<T1,T2,T3> handle, IValueDescriptor<T1> symbol1, IValueDescriptor<T2> symbol2, IValueDescriptor<T3> symbol3)
115+
public static System.Void SetHandler<T1, T2, T3>(this Command command, Func<T1,T2,T3,System.Threading.Tasks.Task> handle, IValueDescriptor<T1> symbol1, IValueDescriptor<T2> symbol2, IValueDescriptor<T3> symbol3)
116+
public static System.Void SetHandler<T1, T2, T3, T4>(this Command command, Action<T1,T2,T3,T4> handle, IValueDescriptor<T1> symbol1, IValueDescriptor<T2> symbol2, IValueDescriptor<T3> symbol3, IValueDescriptor<T4> symbol4)
117+
public static System.Void SetHandler<T1, T2, T3, T4>(this Command command, Func<T1,T2,T3,T4,System.Threading.Tasks.Task> handle, IValueDescriptor<T1> symbol1, IValueDescriptor<T2> symbol2, IValueDescriptor<T3> symbol3, IValueDescriptor<T4> symbol4)
118+
public static System.Void SetHandler<T1, T2, T3, T4, T5>(this Command command, Action<T1,T2,T3,T4,T5> handle, IValueDescriptor<T1> symbol1, IValueDescriptor<T2> symbol2, IValueDescriptor<T3> symbol3, IValueDescriptor<T4> symbol4, IValueDescriptor<T5> symbol5)
119+
public static System.Void SetHandler<T1, T2, T3, T4, T5>(this Command command, Func<T1,T2,T3,T4,T5,System.Threading.Tasks.Task> handle, IValueDescriptor<T1> symbol1, IValueDescriptor<T2> symbol2, IValueDescriptor<T3> symbol3, IValueDescriptor<T4> symbol4, IValueDescriptor<T5> symbol5)
120+
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6>(this Command command, Action<T1,T2,T3,T4,T5,T6> handle, IValueDescriptor<T1> symbol1, IValueDescriptor<T2> symbol2, IValueDescriptor<T3> symbol3, IValueDescriptor<T4> symbol4, IValueDescriptor<T5> symbol5, IValueDescriptor<T6> symbol6)
121+
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6>(this Command command, Func<T1,T2,T3,T4,T5,T6,System.Threading.Tasks.Task> handle, IValueDescriptor<T1> symbol1, IValueDescriptor<T2> symbol2, IValueDescriptor<T3> symbol3, IValueDescriptor<T4> symbol4, IValueDescriptor<T5> symbol5, IValueDescriptor<T6> symbol6)
122+
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7>(this Command command, Action<T1,T2,T3,T4,T5,T6,T7> handle, IValueDescriptor<T1> symbol1, IValueDescriptor<T2> symbol2, IValueDescriptor<T3> symbol3, IValueDescriptor<T4> symbol4, IValueDescriptor<T5> symbol5, IValueDescriptor<T6> symbol6, IValueDescriptor<T7> symbol7)
123+
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7>(this Command command, Func<T1,T2,T3,T4,T5,T6,T7,System.Threading.Tasks.Task> handle, IValueDescriptor<T1> symbol1, IValueDescriptor<T2> symbol2, IValueDescriptor<T3> symbol3, IValueDescriptor<T4> symbol4, IValueDescriptor<T5> symbol5, IValueDescriptor<T6> symbol6, IValueDescriptor<T7> symbol7)
124+
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8>(this Command command, Action<T1,T2,T3,T4,T5,T6,T7,T8> handle, IValueDescriptor<T1> symbol1, IValueDescriptor<T2> symbol2, IValueDescriptor<T3> symbol3, IValueDescriptor<T4> symbol4, IValueDescriptor<T5> symbol5, IValueDescriptor<T6> symbol6, IValueDescriptor<T7> symbol7, IValueDescriptor<T8> symbol8)
125+
public static System.Void SetHandler<T1, T2, T3, T4, T5, T6, T7, T8>(this Command command, Func<T1,T2,T3,T4,T5,T6,T7,T8,System.Threading.Tasks.Task> handle, IValueDescriptor<T1> symbol1, IValueDescriptor<T2> symbol2, IValueDescriptor<T3> symbol3, IValueDescriptor<T4> symbol4, IValueDescriptor<T5> symbol5, IValueDescriptor<T6> symbol6, IValueDescriptor<T7> symbol7, IValueDescriptor<T8> symbol8)
140126
public interface IConsole : System.CommandLine.IO.IStandardError, System.CommandLine.IO.IStandardIn, System.CommandLine.IO.IStandardOut
141127
public abstract class IdentifierSymbol : Symbol, System.CommandLine.Completions.ICompletionSource
142128
public System.Collections.Generic.IReadOnlyCollection<System.String> Aliases { get; }
@@ -234,7 +220,6 @@ System.CommandLine.Binding
234220
public System.Void AddService<T>(Func<System.IServiceProvider,T> factory)
235221
public System.Object GetService(System.Type serviceType)
236222
public struct BoundValue : System.ValueType
237-
public static BoundValue DefaultForValueDescriptor(IValueDescriptor valueDescriptor)
238223
public System.Object Value { get; }
239224
public IValueDescriptor ValueDescriptor { get; }
240225
public IValueSource ValueSource { get; }

src/System.CommandLine.NamingConventionBinder/ModelBinder.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ internal static (BoundValue? boundValue, bool usedNonDefault) GetBoundValue(
287287
{
288288
if (valueDescriptor.HasDefaultValue)
289289
{
290-
return (BoundValue.DefaultForValueDescriptor(valueDescriptor), false);
290+
return (DefaultForValueDescriptor(valueDescriptor, bindingContext), false);
291291
}
292292

293293
if (valueDescriptor.ValueType != parentType) // Recursive models aren't allowed
@@ -312,12 +312,26 @@ internal static (BoundValue? boundValue, bool usedNonDefault) GetBoundValue(
312312
return (new BoundValue(parameterDescriptor.GetDefaultValue(), valueDescriptor, valueSource), false);
313313
}
314314

315-
return (BoundValue.DefaultForValueDescriptor(valueDescriptor), false);
315+
return (DefaultForValueDescriptor(valueDescriptor, bindingContext), false);
316316
}
317317

318318
return (null, false);
319319
}
320320

321+
private static BoundValue DefaultForValueDescriptor(
322+
IValueDescriptor valueDescriptor,
323+
BindingContext context)
324+
{
325+
var valueSource = ValueDescriptorDefaultValueSource.Instance;
326+
327+
valueSource.TryGetValue(valueDescriptor, context, out var value);
328+
329+
return new BoundValue(
330+
value,
331+
valueDescriptor,
332+
valueSource);
333+
}
334+
321335
private protected IValueDescriptor FindModelPropertyDescriptor(Type propertyType, string propertyName)
322336
{
323337
return ModelDescriptor.PropertyDescriptors

src/System.CommandLine.Rendering.Tests/TerminalModeTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public async Task Sets_output_mode_to_Ansi_when_specified_by_output_directive(Ou
4444
OutputMode detectedOutputMode = OutputMode.Auto;
4545

4646
var command = new Command("hello");
47-
command.SetHandler((InvocationContext ctx) =>
47+
command.SetHandler(ctx =>
4848
{
4949
detectedOutputMode = ctx.Console.DetectOutputMode();
5050
return Task.FromResult(0);

src/System.CommandLine.Suggest/SuggestionDispatcher.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,16 @@ public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISug
3232
{
3333
shellTypeArgument
3434
};
35-
CompleteScriptCommand.SetHandler((InvocationContext context) =>
35+
CompleteScriptCommand.SetHandler(context =>
3636
{
3737
SuggestionShellScriptHandler.Handle(context.Console, context.ParseResult.GetValueForArgument(shellTypeArgument));
38-
return Task.FromResult(0);
3938
});
4039

4140
ListCommand = new Command("list")
4241
{
4342
Description = "Lists apps registered for suggestions",
4443
};
45-
ListCommand.SetHandler((InvocationContext ctx) =>
44+
ListCommand.SetHandler(ctx =>
4645
{
4746
ctx.Console.Out.WriteLine(ShellPrefixesToMatch(_suggestionRegistration));
4847
return Task.FromResult(0);
@@ -53,7 +52,7 @@ public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISug
5352
ExecutableOption,
5453
PositionOption
5554
};
56-
GetCommand.SetHandler((InvocationContext context) => Get(context));
55+
GetCommand.SetHandler(context => Get(context));
5756

5857
var commandPathOption = new Option<string>("--command-path", "The path to the command for which to register suggestions");
5958

@@ -63,7 +62,7 @@ public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISug
6362
new Option<string>("--suggestion-command", "The command to invoke to retrieve suggestions")
6463
};
6564

66-
RegisterCommand.SetHandler((InvocationContext context) =>
65+
RegisterCommand.SetHandler(context =>
6766
{
6867
Register(context.ParseResult.GetValueForOption(commandPathOption), context.Console);
6968
return Task.FromResult(0);

0 commit comments

Comments
 (0)