You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The preceding code uses a `SetHandler` overload that gets an [InvocationContext](model-binding.md#invocationcontext) instance rather than one or more `IValueDescriptor<T>` objects. The `InvocationContext` is used to get the `CancellationToken` and [ParseResult](model-binding.md#parseresult) objects. `ParseResult` can provide argument or option values.
21
+
20
22
Cancellation actions can also be added directly using the <xref:System.Threading.CancellationToken.Register%2A?displayProperty=nameWithType> method.
21
23
22
24
For information about an alternative way to set the process exit code, see [Set exit codes](model-binding.md#set-exit-codes).
The process of parsing arguments and providing them to command handler code is called *model binding*. `System.CommandLine` has the ability to bind many argument types built in. For example, integers, enums, and file system objects such as <xref:System.IO.FileInfo> and <xref:System.IO.DirectoryInfo> can be bound. Several `System.CommandLine` types can also be bound.
16
+
The process of parsing arguments and providing them to command handler code is called *parameter binding*. `System.CommandLine` has the ability to bind many argument types built in. For example, integers, enums, and file system objects such as <xref:System.IO.FileInfo> and <xref:System.IO.DirectoryInfo> can be bound. Several `System.CommandLine` types can also be bound.
17
17
18
18
## Built-in argument validation
19
19
@@ -45,7 +45,7 @@ This behavior can be overridden by setting <xref:System.CommandLine.Option.Allow
45
45
myapp --item one --item two --item three
46
46
```
47
47
48
-
## Model binding up to 16 options and arguments
48
+
## Parameter binding up to 16 options and arguments
49
49
50
50
The following example shows how to bind options to command handler parameters, by calling <xref:System.CommandLine.Handler.SetHandler%2A>:
51
51
@@ -66,11 +66,19 @@ The variables that follow the lambda represent the option and argument objects t
66
66
* If the out-of-order options or arguments are of different types, a run-time exception is thrown. For example, an `int` might appear where a `string` should be in the list of sources.
67
67
* If the out-of-order options or arguments are of the same type, the handler silently gets the wrong values in the parameters provided to it. For example, `string` option `x` might appear where `string` option `y` should be in the list of sources. In that case, the variable for the option `y` value gets the option `x` value.
68
68
69
-
There are overloads of <xref:System.CommandLine.Handler.SetHandler%2A> that support up to 16 parameters, with both synchronous and asynchronous signatures.
69
+
There are overloads of <xref:System.CommandLine.Handler.SetHandler%2A> that support up to 8 parameters, with both synchronous and asynchronous signatures.
70
70
71
-
## Model binding more than 16 options and arguments
71
+
## Parameter binding more than 8 options and arguments
72
72
73
-
To handle more than 16 options, or to construct a custom type from multiple options, create a *custom binder*. The binder lets you combine multiple option or argument values into a complex type and pass that into a single handler parameter. Suppose you have a `Person` type:
73
+
To handle more than 8 options, or to construct a custom type from multiple options, you can use `InvocationContext` or a custom binder.
74
+
75
+
### Use `InvocationContext`
76
+
77
+
A <xref:System.CommandLine.Handler.SetHandler%2A> overload provides access to the <xref:System.CommandLine.Invocation.InvocationContext> object, and you can use `InvocationContext` to get any number of option and argument values. For examples, see [Set exit codes](#set-exit-codes) and [Handle termination](handle-termination.md).
78
+
79
+
### Use a custom binder
80
+
81
+
A custom binder lets you combine multiple option or argument values into a complex type and pass that into a single handler parameter. Suppose you have a `Person` type:
However, if the lambda itself needs to be async, you can't return a `Task<int>`. In that case, use <xref:System.CommandLine.Invocation.InvocationContext.ExitCode?displayProperty=nameWithType>. You can get the `InvocationContext` instance injected into your lambda just by including it as one of the parameters, as in the following example:
103
+
However, if the lambda itself needs to be async, you can't return a `Task<int>`. In that case, use <xref:System.CommandLine.Invocation.InvocationContext.ExitCode?displayProperty=nameWithType>. You can get the `InvocationContext` instance injected into your lambda by using a SetHandler overload that specifies the `InvocationContext`as the sole parameter. This `SetHandler` overload doesn't let you specify `IValueDescriptor<T>` objects, but you can get option and argument values from the [ParseResult](#parseresult) property of `InvocationContext`, as shown in the following example:
@@ -181,32 +189,30 @@ Besides the file system types and `Uri`, the following types are supported:
181
189
*`ulong`
182
190
*`ushort`
183
191
184
-
## Inject System.CommandLine types
192
+
## Use System.CommandLine objects
185
193
186
-
`System.CommandLine` allows you to use some types in handlers by adding parameters for them to the handler signature. The available types include:
194
+
There's a `SetHandler` overload that gives you access to the <xref:System.CommandLine.Invocation.InvocationContext> object. That object can then be used to access other `System.CommandLine` objects. For example, you have access to the following objects:
Other types can be injected by using custom binders. For more information, see [Dependency injection](dependency-injection.md).
201
+
### `InvocationContext`
202
+
203
+
For examples, see [Set exit codes](#set-exit-codes) and [Handle termination](handle-termination.md).
194
204
195
205
### `CancellationToken`
196
206
197
207
For information about how to use <xref:System.Threading.CancellationToken>, see [How to handle termination](handle-termination.md).
198
208
199
209
### `IConsole`
200
210
201
-
<xref:System.CommandLine.IConsole> makes testing as well as many extensibility scenarios easier than using `System.Console`.
202
-
203
-
### `InvocationContext`
204
-
205
-
For an example, see [Set exit codes](#set-exit-codes).
211
+
<xref:System.CommandLine.IConsole> makes testing as well as many extensibility scenarios easier than using `System.Console`. It's available in the <xref:System.CommandLine.Invocation.InvocationContext.Console?displayProperty=nameWithType> property.
206
212
207
213
### `ParseResult`
208
214
209
-
<xref:System.CommandLine.Parsing.ParseResult>is a singleton structure that represents the results of parsing the command line input. You can use it to check for the presence of options or arguments on the command line or to get the <xref:System.CommandLine.Parsing.ParseResult.UnmatchedTokens?displayProperty=nameWithType> property. This property contains a list of the [tokens](syntax.md#tokens) that were parsed but didn't match any configured command, option, or argument.
215
+
The <xref:System.CommandLine.Parsing.ParseResult>object is available in the <xref:System.CommandLine.Invocation.InvocationContext.ParseResult?displayProperty=nameWithType> property. It's a singleton structure that represents the results of parsing the command line input. You can use it to check for the presence of options or arguments on the command line or to get the <xref:System.CommandLine.Parsing.ParseResult.UnmatchedTokens?displayProperty=nameWithType> property. This property contains a list of the [tokens](syntax.md#tokens) that were parsed but didn't match any configured command, option, or argument.
210
216
211
217
The list of unmatched tokens is useful in commands that behave like wrappers. A wrapper command takes a set of [tokens](syntax.md#tokens) and forwards them to another command or app. The `sudo` command in Linux is an example. It takes the name of a user to impersonate followed by a command to run. For example:
0 commit comments