Skip to content

Commit 2b7dfed

Browse files
committed
update getting started doc
1 parent 29c4d22 commit 2b7dfed

File tree

1 file changed

+36
-43
lines changed

1 file changed

+36
-43
lines changed

docs/Your-first-app-with-System-CommandLine.md

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This walkthrough will show you how to get started using System.CommandLine to bu
44

55
## Create a new console app
66

7-
Open a new console and run the following commands:
7+
Open a new console and run the following commands in an empty directory:
88

99
```console
1010
> dotnet new console -o myApp
@@ -13,15 +13,12 @@ Open a new console and run the following commands:
1313

1414
## Install the System.CommandLine package
1515

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:
1717

1818
```console
1919
> dotnet add package System.CommandLine --prerelease
2020
```
2121

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-
2522
## Add some code
2623

2724
Open `Program.cs`. At the top, add a `using` directive:
@@ -30,59 +27,55 @@ Open `Program.cs`. At the top, add a `using` directive:
3027
using System.CommandLine;
3128
```
3229

33-
Your `Main` method looks like this:
30+
`Program.cs` contains the following code:
3431

3532
```csharp
36-
static void Main(string[] args)
37-
{
38-
Console.WriteLine("Hello World!");
39-
}
33+
Console.WriteLine("Hello World!");
4034
```
4135

42-
Now, let's add a parser.
36+
This isn't doing anything with the `args` parameter. For that, we'll use System.CommandLine.
4337

44-
You'll need a few more `using` directives:
38+
At the top of the file, add the following `using` directives:
4539

4640
```csharp
47-
using System;
4841
using System.CommandLine;
49-
using System.CommandLine.Invocation;
5042
using System.IO;
5143
```
5244

5345
Now change your `Main` method to this:
5446

5547
```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) =>
5771
{
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);
8679
```
8780

8881
You're ready to run your program.

0 commit comments

Comments
 (0)