@@ -4,7 +4,7 @@ This walkthrough will show you how to get started using System.CommandLine to bu
4
4
5
5
## Create a new console app
6
6
7
- Open a new console and run the following commands:
7
+ Open a new console and run the following commands in an empty directory :
8
8
9
9
``` console
10
10
> dotnet new console -o myApp
@@ -13,15 +13,12 @@ Open a new console and run the following commands:
13
13
14
14
## Install the System.CommandLine package
15
15
16
- Use ` dotnet ` to add the package to your project. From the project directory, run:
16
+ Use ` dotnet ` to add the package to your project. In the project directory, run:
17
17
18
18
``` console
19
19
> dotnet add package System.CommandLine --prerelease
20
20
```
21
21
22
- Or see more options on Nuget
23
- [ ![ Nuget] ( https://img.shields.io/nuget/v/System.CommandLine.svg )] ( https://nuget.org/packages/System.CommandLine )
24
-
25
22
## Add some code
26
23
27
24
Open ` Program.cs ` . At the top, add a ` using ` directive:
@@ -30,59 +27,55 @@ Open `Program.cs`. At the top, add a `using` directive:
30
27
using System .CommandLine ;
31
28
```
32
29
33
- Your ` Main ` method looks like this :
30
+ ` Program.cs ` contains the following code :
34
31
35
32
``` csharp
36
- static void Main (string [] args )
37
- {
38
- Console .WriteLine (" Hello World!" );
39
- }
33
+ Console .WriteLine (" Hello World!" );
40
34
```
41
35
42
- Now, let's add a parser .
36
+ This isn't doing anything with the ` args ` parameter. For that, we'll use System.CommandLine .
43
37
44
- You'll need a few more ` using ` directives:
38
+ At the top of the file, add the following ` using ` directives:
45
39
46
40
``` csharp
47
- using System ;
48
41
using System .CommandLine ;
49
- using System .CommandLine .Invocation ;
50
42
using System .IO ;
51
43
```
52
44
53
45
Now change your ` Main ` method to this:
54
46
55
47
``` csharp
56
- static int Main (string [] args )
48
+ // Create some options:
49
+ var intOption = new Option <int >(
50
+ " --int-option" ,
51
+ getDefaultValue : () => 42 ,
52
+ description : " An option whose argument is parsed as an int" );
53
+ var boolOption = new Option <bool >(
54
+ " --bool-option" ,
55
+ " An option whose argument is parsed as a bool" );
56
+ var fileOption = new Option <FileInfo >(
57
+ " --file-option" ,
58
+ " An option whose argument is parsed as a FileInfo" );
59
+
60
+ // Add the options to a root command:
61
+ var rootCommand = new RootCommand
62
+ {
63
+ intOption ,
64
+ boolOption ,
65
+ fileOption
66
+ };
67
+
68
+ rootCommand .Description = " My sample app" ;
69
+
70
+ rootCommand .SetHandler ((int i , bool b , FileInfo f ) =>
57
71
{
58
- // Create a root command with some options
59
- var rootCommand = new RootCommand
60
- {
61
- new Option <int >(
62
- " --int-option" ,
63
- getDefaultValue : () => 42 ,
64
- description : " An option whose argument is parsed as an int" ),
65
- new Option <bool >(
66
- " --bool-option" ,
67
- " An option whose argument is parsed as a bool" ),
68
- new Option <FileInfo >(
69
- " --file-option" ,
70
- " An option whose argument is parsed as a FileInfo" )
71
- };
72
-
73
- rootCommand .Description = " My sample app" ;
74
-
75
- // Note that the parameters of the handler method are matched according to the names of the options
76
- rootCommand .Handler = CommandHandler .Create <int , bool , FileInfo >((intOption , boolOption , fileOption ) =>
77
- {
78
- Console .WriteLine ($" The value for --int-option is: {intOption }" );
79
- Console .WriteLine ($" The value for --bool-option is: {boolOption }" );
80
- Console .WriteLine ($" The value for --file-option is: {fileOption ? .FullName ?? " null" }" );
81
- });
82
-
83
- // Parse the incoming args and invoke the handler
84
- return rootCommand .InvokeAsync (args ).Result ;
85
- }
72
+ Console .WriteLine ($" The value for --int-option is: {i }" );
73
+ Console .WriteLine ($" The value for --bool-option is: {b }" );
74
+ Console .WriteLine ($" The value for --file-option is: {f ? .FullName ?? " null" }" );
75
+ }, intOption , boolOption , fileOption );
76
+
77
+ // Parse the incoming args and invoke the handler
78
+ return rootCommand .Invoke (args );
86
79
```
87
80
88
81
You're ready to run your program.
0 commit comments