Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions docs/standard/commandline/how-to-configure-the-parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,26 @@ ms.topic: how-to

[!INCLUDE [scl-preview](./includes/preview.md)]

<xref:System.CommandLine.ParserConfiguration> is a class that provides properties to configure the parser. It is an optional argument for every `Parse` method, such as <xref:System.CommandLine.Command.Parse*?displayProperty=nameWithType> and <xref:System.CommandLine.Parsing.CommandLineParser.Parse*?displayProperty=nameWithType>. When it isn't provided, the default configuration is used.
Parsing and invocation are two separate steps, so each of them has their own configuration:

<xref:System.CommandLine.ParseResult> has a <xref:System.CommandLine.ParseResult.Configuration> property that returns the configuration used for parsing.
- <xref:System.CommandLine.ParserConfiguration> is a class that provides properties to configure the parsing. It is an optional argument for every `Parse` method, such as <xref:System.CommandLine.Command.Parse*?displayProperty=nameWithType> and <xref:System.CommandLine.Parsing.CommandLineParser.Parse*?displayProperty=nameWithType>.
- <xref:System.CommandLine.InvocationConfiguration> is a class that provides properties to configure the invocation. It is an optional argument of the <xref:System.CommandLine.ParseResult.Invoke*?displayProperty=nameWithType> and <xref:System.CommandLine.ParseResult.InvokeAsync*?displayProperty=nameWithType> methods.

## Standard output and error
They are exposed by the <xref:System.CommandLine.ParseResult.Configuration?displayProperty=nameWithType> and <xref:System.CommandLine.ParseResult.InvocationConfiguration?displayProperty=nameWithType> properties. When they aren't provided, the default configurations are used.

## ParserConfiguration

### EnablePosixBundling

[Bundling](syntax.md#option-bundling) of single-character options is enabled by default, but you can disable it by setting the <xref:System.CommandLine.ParserConfiguration.EnablePosixBundling?displayProperty=nameWithType> property to `false`.

### ResponseFileTokenReplacer

[Response files](syntax.md#response-files) are enabled by default, but you can disable them by setting the <xref:System.CommandLine.ParserConfiguration.ResponseFileTokenReplacer> property to `null`. You can also provide a custom implementation to customize how response files are processed.

## InvocationConfiguration

### Standard output and error

<xref:System.CommandLine.InvocationConfiguration> makes testing, as well as many extensibility scenarios, easier than using `System.Console`. It exposes two `TextWriter` properties: <xref:System.CommandLine.InvocationConfiguration.Output> and <xref:System.CommandLine.InvocationConfiguration.Error>. You can set these properties to any `TextWriter` instance, such as a `StringWriter`, which you can use to capture output for testing.

Expand All @@ -30,23 +45,15 @@ Now, use <xref:System.CommandLine.InvocationConfiguration> to capture the output

:::code language="csharp" source="snippets/configuration/csharp/Program.cs" id="captureoutput":::

## EnablePosixBundling

[Bundling](syntax.md#option-bundling) of single-character options is enabled by default, but you can disable it by setting the <xref:System.CommandLine.ParserConfiguration.EnablePosixBundling?displayProperty=nameWithType> property to `false`.

## ProcessTerminationTimeout
### ProcessTerminationTimeout

[Process termination timeout](how-to-parse-and-invoke.md#process-termination-timeout) can be configured via the <xref:System.CommandLine.InvocationConfiguration.ProcessTerminationTimeout> property. The default value is 2 seconds.

## ResponseFileTokenReplacer

[Response files](syntax.md#response-files) are enabled by default, but you can disable them by setting the <xref:System.CommandLine.ParserConfiguration.ResponseFileTokenReplacer> property to `null`. You can also provide a custom implementation to customize how response files are processed.

## EnableDefaultExceptionHandler
### EnableDefaultExceptionHandler

By default, all unhandled exceptions thrown during the invocation of a command are caught and reported to the user. You can disable this behavior by setting the <xref:System.CommandLine.InvocationConfiguration.EnableDefaultExceptionHandler> property to `false`. This is useful when you want to handle exceptions in a custom way, such as logging them or providing a different user experience.

## Derived classes
### Derived classes

<xref:System.CommandLine.InvocationConfiguration> is not sealed, so you can derive from it to add custom properties or methods. This is useful when you want to provide additional configuration options specific to your application.

Expand Down
2 changes: 1 addition & 1 deletion docs/standard/commandline/includes/preview.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ ms.date: 07/31/2025
ms.topic: include
---
> [!IMPORTANT]
> `System.CommandLine` is currently in preview. This documentation is for version 2.0 beta 5.
> `System.CommandLine` is currently in preview. This documentation is for version 2.0 beta 7.
> Some information relates to prerelease product that might be substantially modified before it's released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Spectre.Console" Version="0.48.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta5.25277.114" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta7.25380.108" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta5.25277.114" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta7.25380.108" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static int Main(string[] args)

// <parse>
ParseResult parseResult = rootCommand.Parse(args);
if (parseResult.GetValue(fileOption) is FileInfo parsedFile)
if (parseResult.Errors.Count == 0 && parseResult.GetValue(fileOption) is FileInfo parsedFile)
{
ReadFile(parsedFile);
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta5.25277.114" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta7.25380.108" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta5.25277.114" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta7.25380.108" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta5.25277.114" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta7.25380.108" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta5.25277.114" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta7.25380.108" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta5.25277.114" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta7.25380.108" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta5.25277.114" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta7.25380.108" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta5.25277.114" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta7.25380.108" />
</ItemGroup>

</Project>