You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/fundamentals/tutorials/file-based-programs.md
+11-77Lines changed: 11 additions & 77 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -152,19 +152,15 @@ If you prefer, you can remove the extension so you can type `./AsciiArt` instead
152
152
153
153
Next, add a package that supports ASCII art, [Colorful.Console](https://www.nuget.org/packages/Colorful.Console). To add a package to a file based program, you use the `#:package` directive. Add the following directive after the `#!` directive in your AsciiArt.cs file:
> The version `1.2.15` was the latest version when this tutorial was last updated. If there's a newer version available, use the latest version to ensure you have the latest security packages.
162
160
163
161
Next, change the lines that call `Console.WriteLine` to use the `Colorful.Console.WriteAscii` method instead:
Run the program, and you see ASCII art output instead of echoed text. Next, let's add command line parsing. The current version writes each word as a different line of output. The command line arguments you add support two features:
170
166
@@ -193,110 +189,48 @@ The `System.CommandLine` library offers several key benefits:
193
189
194
190
To add command line parsing capabilities, first add the `System.CommandLine` package. Add this directive after the existing package directive:
> The version `2.0.0-beta6` was the latest version when this tutorial was last updated. If there's a newer version available, use the latest version to ensure you have the latest security packages.
203
197
204
198
Next, add the necessary using statements at the top of your file (after the `#!` and `#:package` directives):
Define the delay option and messages argument. In command-line applications, options typically begin with `--` (double dash) and can accept arguments. The `--delay` option accepts an integer argument that specifies the delay in milliseconds. The `messagesArgument` defines how any remaining tokens after options are parsed as text. Each token becomes a separate string in the array, but text can be quoted to include multiple words in one token. For example, `"This is one message"` becomes a single token, while `This is four tokens` becomes four separate tokens. Add the following code to create the <xref:System.CommandLine.Option`1?displayProperty=nameWithType> and <xref:System.CommandLine.Argument`1?displayProperty=nameWithType> objects to represent the command line option and argument:
212
203
213
-
```csharp
214
-
Option<int>delayOption=new("--delay")
215
-
{
216
-
Description="Delay between lines, specified as milliseconds.",
The preceding code defines the argument type for the `--delay` option, and that the arguments are an array of `string` values. This application has only one command, so you use the *root command*. Create a root command and configure it with the option and argument. Add the argument and option to the root command:
227
207
228
-
```csharp
229
-
RootCommandrootCommand=new("Ascii Art file-based program sample");
Next, add the code to parse the command line arguments and handle any errors. This code validates the command line arguments and stores parsed arguments in the <xref:System.CommandLine.ParseResult?displayProperty=nameWithType> object:
The preceding code validates all command line arguments. If the validation fails, errors are written to the console, and the app exits.
250
216
251
217
## Use parsed command line results
252
218
253
219
Now, finish the app to use the parsed options and write the output. First, define a record to hold the parsed options. File-based apps can include type declarations, like records. They must be after all top-level statements and local functions. Add a `record` declaration to store the messages and the delay option value:
Now that you declared the record to store those results, add a local function to process the parse results and store the values in an instance of the record. Add the following local function before the record declaration. This method handles both command line arguments and standard input, and returns a new record instance:
Next, create a local function to write the ASCII art with the specified delay. This function writes each message in the record with the specified delay between each message:
0 commit comments