Skip to content

System.CommandLine docs update #46594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions .openpublishing.redirection.fundamentals.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,30 @@
"source_path_from_root": "/docs/fundamentals/networking/httpclient.md",
"redirect_url": "/dotnet/fundamentals/networking/http/httpclient"
},
{
"source_path_from_root": "/docs/standard/commandline/customize-help.md",
"redirect_url": "/docs/standard/commandline/help"
},
{
"source_path_from_root": "/docs/standard/commandline/define-commands.md",
"redirect_url": "/docs/standard/commandline/syntax#commands"
},
{
"source_path_from_root": "/docs/standard/commandline/dependency-injection.md",
"redirect_url": "/docs/standard/commandline/beta5#invocation"
},
{
"source_path_from_root": "/docs/standard/commandline/handle-termination.md",
"redirect_url": "/docs/standard/commandline/parse-and-invoke#process-termination-timeout"
},
{
"source_path_from_root": "/docs/standard/commandline/model-binding.md",
"redirect_url": "/docs/standard/commandline/parse-and-invoke"
},
{
"source_path_from_root": "/docs/standard/commandline/use-middleware.md",
"redirect_url": "/docs/standard/commandline/beta5#configuration"
},
{
"source_path_from_root": "/docs/fundamentals/networking/httpclient-guidelines.md",
"redirect_url": "/dotnet/fundamentals/networking/http/httpclient-guidelines"
Expand Down
28 changes: 14 additions & 14 deletions docs/fundamentals/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -934,22 +934,22 @@ items:
href: ../standard/commandline/index.md
- name: Get started tutorial
href: ../standard/commandline/get-started-tutorial.md
- name: Command-line syntax
- name: Command-line syntax overview
href: ../standard/commandline/syntax.md
- name: Define commands
href: ../standard/commandline/define-commands.md
- name: Model binding
href: ../standard/commandline/model-binding.md
- name: Tab completion
- name: How to parse and invoke the result
href: ../standard/commandline/parse-and-invoke.md
- name: How to customize parsing and validation
href: ../standard/commandline/parsing-and-validation.md
- name: How to configure the parser
href: ../standard/commandline/command-line-configuration.md
- name: How to enable and customize tab completion
href: ../standard/commandline/tab-completion.md
- name: Dependency injection
href: ../standard/commandline/dependency-injection.md
- name: Customize help
href: ../standard/commandline/customize-help.md
- name: Handle termination
href: ../standard/commandline/handle-termination.md
- name: Use middleware
href: ../standard/commandline/use-middleware.md
- name: How to customize help
href: ../standard/commandline/help.md
- name: Design guidance
href: ../standard/commandline/design-guidance.md
- name: Breaking changes in beta5
href: ../standard/commandline/beta5.md
- name: File and stream I/O
items:
- name: Overview
Expand Down
330 changes: 330 additions & 0 deletions docs/standard/commandline/beta5.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps pedantic of me, but I recommend renaming this to 2.0.0-beta5 and generally referring to the release as 2.0.0-beta5 throughout.

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions docs/standard/commandline/command-line-configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
title: How to configure the parser in System.CommandLine
description: "Learn how to configure the parser in System.CommandLine."
ms.date: 16/06/2025
no-loc: [System.CommandLine]
helpviewer_keywords:
- "command line interface"
- "command line"
- "System.CommandLine"
ms.topic: how-to
---

# How to configure the parser in System.CommandLine

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

<xref:System.CommandLine.CommandLineConfiguration> 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> or <xref:System.CommandLine.Parsing.CommandLineParser.Parse>. When it is not provided, the default configuration is used.

Every <xref:System.CommandLine.ParseResult> instance has a <xref:System.CommandLine.ParseResult.Configuration> property that returns the configuration used for parsing.

## Standard output and error

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

Let's define a simple command that writes to standard output:

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

Now, let's use `CommandLineConfiguration` 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.CommandLineConfiguration.EnablePosixBundling> property to `false`.

## ProcessTerminationTimeout

[Process termination timeout](parse-and-invoke.md#process-termination-timeout) can be configured via the <xref:System.CommandLine.CommandLineConfiguration.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.CommandLineConfiguration.ResponseFileTokenReplacer> property to `null`. You can also provide a custom implementation to customize how response files are processed.

## EnableDefaultExceptionHandler

By default, all unhandled exceptions thrown during the invocation of a command are caught and reported to the user. This behavior can be disabled by setting the <xref:System.CommandLine.CommandLineConfiguration.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

<xref:System.CommandLine.CommandLineConfiguration> 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.

## See also

- [System.CommandLine overview](index.md)
140 changes: 0 additions & 140 deletions docs/standard/commandline/customize-help.md

This file was deleted.

Loading
Loading