@@ -22,7 +22,7 @@ rootCommand.Options.Add(nameOption);
2222
2323rootCommand .SetAction (parseResult =>
2424{
25- string name = parseResult .GetValueForOption (nameOption );
25+ string name = parseResult .GetValue (nameOption );
2626 Console .WriteLine ($" Hello, {name ?? " World" }!" );
2727});
2828
@@ -34,19 +34,22 @@ return await rootCommand.Parse(args).InvokeAsync();
3434Arguments are values passed directly to commands without option names:
3535
3636``` csharp
37- var fileArgument = new Argument <FileInfo >(
38- name : " file" ,
39- description : " The file to process" );
37+ var fileArgument = new Argument <FileInfo >(" file" )
38+ {
39+ Description = " The file to process"
40+ };
4041
4142var processCommand = new Command (" process" , " Process a file" );
4243processCommand .Arguments .Add (fileArgument );
4344
4445processCommand .SetAction (parseResult =>
4546{
46- FileInfo file = parseResult .GetValueForArgument (fileArgument );
47+ FileInfo file = parseResult .GetValue (fileArgument );
4748 Console .WriteLine ($" Processing {file .FullName }" );
4849});
4950
51+ var rootCommand = new RootCommand ();
52+
5053rootCommand .Subcommands .Add (processCommand );
5154```
5255
@@ -55,17 +58,19 @@ rootCommand.Subcommands.Add(processCommand);
5558Options can have default values and validation:
5659
5760``` csharp
61+ var rootCommand = new RootCommand ();
62+
5863var delayOption = new Option <int >(" --delay" , " -d" )
5964{
6065 Description = " Delay in milliseconds" ,
6166 DefaultValueFactory = _ => 1000
6267};
6368
64- delayOption .AddValidator (result =>
69+ delayOption .Validators . Add (result =>
6570{
6671 if (result .GetValueOrDefault <int >() < 0 )
6772 {
68- result .ErrorMessage = " Delay must be non-negative" ;
73+ result .AddError ( " Delay must be non-negative" ) ;
6974 }
7075});
7176
@@ -129,9 +134,9 @@ rootCommand.Options.Add(verboseOption);
129134
130135rootCommand .SetAction (parseResult =>
131136{
132- var connection = parseResult .GetValueForOption (connectionOption );
133- var timeout = parseResult .GetValueForOption (timeoutOption );
134- var verbose = parseResult .GetValueForOption (verboseOption );
137+ var connection = parseResult .GetValue (connectionOption );
138+ var timeout = parseResult .GetValue (timeoutOption );
139+ var verbose = parseResult .GetValue (verboseOption );
135140
136141 Console .WriteLine ($" Connection: {connection }" );
137142 Console .WriteLine ($" Timeout: {timeout }" );
@@ -154,22 +159,36 @@ var fileOption = new Option<FileInfo>("--file")
154159
155160// Add custom completions using CompletionSources
156161fileOption .CompletionSources .Add (ctx =>
157- {
158- // Custom completion logic - return completion suggestions
159- return new [] { " file1.txt" , " file2.txt" , " file3.txt" };
160- });
162+ // hard-coded list of files
163+ [" file1.txt" , " file2.txt" , " file3.txt" ]
164+ );
161165
162166// Or add simple string suggestions
163167fileOption .CompletionSources .Add (" option1" , " option2" , " option3" );
164168
165169rootCommand .Options .Add (fileOption );
170+ ```
166171
167- // Users can generate completion scripts using dotnet-suggest:
168- // dotnet tool install -g dotnet-suggest
169- // dotnet suggest script bash > ~/.bashrc
170- // dotnet suggest script powershell >> $PROFILE
172+ Users can then easily trigger your completions using ` dotnet-suggest ` :
173+
174+ ``` shell
175+ > dotnet tool install -g dotnet-suggest
176+ > dotnet suggest script bash > ~ /.bashrc
177+ ```
178+
179+ ``` powershell
180+ > dotnet tool install -g dotnet-suggest
181+ > dotnet suggest script powershell >> $PROFILE
171182```
172183
184+ Once ` dotnet-suggest ` is installed, you can register your app with it for completions support:
185+
186+ ``` shell
187+ > dotnet-suggest register --command-path /path/to/myapp
188+ ```
189+
190+ Alternatively, you can create your own commands for completion generation and instruct users on how to set them up.
191+
173192### Async Command Handlers
174193
175194Support for asynchronous operations:
@@ -183,7 +202,7 @@ rootCommand.Options.Add(urlOption);
183202
184203rootCommand .SetAction (async (parseResult , cancellationToken ) =>
185204{
186- var url = parseResult .GetValueForOption (urlOption );
205+ var url = parseResult .GetValue (urlOption );
187206 if (url != null )
188207 {
189208 using var client = new HttpClient ();
@@ -204,9 +223,9 @@ rootCommand.SetAction(async (parseResult, cancellationToken) =>
204223
205224### New Features
206225- ** Finnish Localization** : Added Finnish language translations for help text and error messages ([ #2605 ] ( https://github.com/dotnet/command-line-api/pull/2605 ) )
207- - ** Improved Help System** : Enhanced ` HelpAction ` to allow users to provide custom ` MaxWidth ` for help text formatting ([ #2635 ] ( https://github.com/dotnet/command-line-api/pull/2635 ) )
208- - ** Task<int > Support** : Added ` SetAction ` overload for ` Task<int> ` return types ([ #2634 ] ( https://github.com/dotnet/command-line-api/issues/2634 ) )
209- - ** Implicit Arguments** : Added ` ArgumentResult.Implicit ` property for better argument handling ([ #2622 ] ( https://github.com/dotnet/command-line-api/issues/2622 ) , [ #2625 ] ( https://github.com/dotnet/command-line-api/pull/2625 ) )
226+ - ** Improved Help System** : Enhanced ` HelpAction ` to allow users to provide custom ` MaxWidth ` for help text formatting ([ #2635 ] ( https://github.com/dotnet/command-line-api/pull/2635 ) ). Note that if you create custom Help or Version actions, you'll want to set ` ClearsParseErrors ` to ` true ` to ensure that invoking those features isn't treated like an error by the parser.
227+ - ** ` Task<int> ` Support** : Added ` SetAction ` overload for ` Task<int> ` return types ([ #2634 ] ( https://github.com/dotnet/command-line-api/issues/2634 ) )
228+ - ** Detect Implicit Arguments** : Added the ` ArgumentResult.Implicit ` property for better argument handling ([ #2622 ] ( https://github.com/dotnet/command-line-api/issues/2622 ) , [ #2625 ] ( https://github.com/dotnet/command-line-api/pull/2625 ) )
210229- ** Performance Improvements** : Reduced reflection usage throughout the library for better performance ([ #2662 ] ( https://github.com/dotnet/command-line-api/pull/2662 ) )
211230
212231### Bug Fixes
@@ -225,7 +244,6 @@ rootCommand.SetAction(async (parseResult, cancellationToken) =>
225244
226245### Other Improvements
227246- Updated to .NET 10.0 RC1 compatibility
228- - Enhanced parsing logic for better POSIX and Windows convention support
229247- Improved memory usage and performance optimizations
230248- Better handling of complex command hierarchies
231249
0 commit comments