Skip to content
Open
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
28 changes: 28 additions & 0 deletions docs/standard/commandline/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,34 @@
:::code language="csharp" source="snippets/define-symbols/csharp/Program.cs" id="defineoptions" :::

To add an option to a command and recursively to all of its subcommands, use the `System.CommandLine.Symbol.Recursive` property.
### Global options and verbosity

Check failure on line 102 in docs/standard/commandline/syntax.md

View workflow job for this annotation

GitHub Actions / lint

Headings should be surrounded by blank lines

docs/standard/commandline/syntax.md:102 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Above] [Context: "### Global options and verbosity"] https://github.com/DavidAnson/markdownlint/blob/v0.39.0/doc/md022.md

Some command-line tools define *global options* that apply to all commands and subcommands.
For example, the .NET CLI provides a global `--verbosity` (short form `-v`) option to control the level of output detail.

You can define a global option by adding it to the root command so that it’s available to all subcommands:

```csharp
using System.CommandLine;

var verbosityOption = new Option<string>(
aliases: new[] { "-v", "--verbosity" },
description: "Set the verbosity level (quiet, minimal, normal, detailed, diagnostic)")
{
Arity = ArgumentArity.ZeroOrOne
};

var rootCommand = new RootCommand("Sample app");
rootCommand.AddGlobalOption(verbosityOption);
Copy link
Contributor

Choose a reason for hiding this comment

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

@abhiramch018 Where is the AddGlobalOption method defined?


rootCommand.SetHandler((string verbosity) =>
{
verbosity ??= "diagnostic"; // Treat bare "-v" as Diagnostic
Console.WriteLine($"Verbosity set to: {verbosity}");
}, verbosityOption);

rootCommand.Invoke(args);


### Required Options

Expand Down
Loading