Skip to content

Commit a87274c

Browse files
committed
fix API usage in the README
1 parent a36a957 commit a87274c

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

src/System.CommandLine/README.md

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ using System.CommandLine;
1313

1414
var rootCommand = new RootCommand("Sample command-line app");
1515

16-
var nameOption = new Option<string>(
17-
aliases: ["--name", "-n"],
18-
description: "Your name");
16+
var nameOption = new Option<string>("--name", "-n")
17+
{
18+
Description = "Your name"
19+
};
1920

2021
rootCommand.Options.Add(nameOption);
2122

@@ -25,7 +26,7 @@ rootCommand.SetAction(parseResult =>
2526
Console.WriteLine($"Hello, {name ?? "World"}!");
2627
});
2728

28-
return await rootCommand.InvokeAsync(args);
29+
return await rootCommand.Parse(args).InvokeAsync();
2930
```
3031

3132
### Commands with Arguments
@@ -54,10 +55,11 @@ rootCommand.Subcommands.Add(processCommand);
5455
Options can have default values and validation:
5556

5657
```csharp
57-
var delayOption = new Option<int>(
58-
aliases: ["--delay", "-d"],
59-
getDefaultValue: () => 1000,
60-
description: "Delay in milliseconds");
58+
var delayOption = new Option<int>("--delay", "-d")
59+
{
60+
Description = "Delay in milliseconds",
61+
DefaultValueFactory = _ => 1000
62+
};
6163

6264
delayOption.AddValidator(result =>
6365
{
@@ -81,8 +83,14 @@ var configCommand = new Command("config", "Configure the application");
8183
var configSetCommand = new Command("set", "Set a configuration value");
8284
var configGetCommand = new Command("get", "Get a configuration value");
8385

84-
var keyOption = new Option<string>("--key", "Configuration key");
85-
var valueOption = new Option<string>("--value", "Configuration value");
86+
var keyOption = new Option<string>("--key")
87+
{
88+
Description = "Configuration key"
89+
};
90+
var valueOption = new Option<string>("--value")
91+
{
92+
Description = "Configuration value"
93+
};
8694

8795
configSetCommand.Options.Add(keyOption);
8896
configSetCommand.Options.Add(valueOption);
@@ -101,9 +109,19 @@ rootCommand.Subcommands.Add(configCommand);
101109
Access option values through the ParseResult:
102110

103111
```csharp
104-
var connectionOption = new Option<string>("--connection", "Database connection string");
105-
var timeoutOption = new Option<int>("--timeout", getDefaultValue: () => 30);
106-
var verboseOption = new Option<bool>("--verbose");
112+
var connectionOption = new Option<string>("--connection")
113+
{
114+
Description = "Database connection string"
115+
};
116+
var timeoutOption = new Option<int>("--timeout")
117+
{
118+
Description = "Timeout in seconds",
119+
DefaultValueFactory = _ => 30
120+
};
121+
var verboseOption = new Option<bool>("--verbose")
122+
{
123+
Description = "Enable verbose output"
124+
};
107125

108126
rootCommand.Options.Add(connectionOption);
109127
rootCommand.Options.Add(timeoutOption);
@@ -129,27 +147,38 @@ Enable tab completion for your CLI:
129147
// Completions are automatically available for all commands, options, and arguments
130148
var rootCommand = new RootCommand("My app with completions");
131149

132-
var fileOption = new Option<FileInfo>("--file", "The file to process");
133-
fileOption.AddCompletions((ctx) =>
150+
var fileOption = new Option<FileInfo>("--file")
151+
{
152+
Description = "The file to process"
153+
};
154+
155+
// Add custom completions using CompletionSources
156+
fileOption.CompletionSources.Add(ctx =>
134157
{
135-
// Custom completion logic
158+
// Custom completion logic - return completion suggestions
136159
return new[] { "file1.txt", "file2.txt", "file3.txt" };
137160
});
138161

162+
// Or add simple string suggestions
163+
fileOption.CompletionSources.Add("option1", "option2", "option3");
164+
139165
rootCommand.Options.Add(fileOption);
140166

141167
// Users can generate completion scripts using dotnet-suggest:
142168
// dotnet tool install -g dotnet-suggest
143169
// dotnet suggest script bash > ~/.bashrc
144-
// dotnet suggest script powershell > $PROFILE
170+
// dotnet suggest script powershell >> $PROFILE
145171
```
146172

147173
### Async Command Handlers
148174

149175
Support for asynchronous operations:
150176

151177
```csharp
152-
var urlOption = new Option<string>("--url", "The URL to fetch");
178+
var urlOption = new Option<string>("--url")
179+
{
180+
Description = "The URL to fetch"
181+
};
153182
rootCommand.Options.Add(urlOption);
154183

155184
rootCommand.SetAction(async (parseResult, cancellationToken) =>

0 commit comments

Comments
 (0)