Visual studio using.system to the top of file net năm 2024

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Tutorial: Get started with System.CommandLine

  • Article
  • 09/21/2022

In this article

Important

rootCommand.SetHandler((file) =>

{ 
    ReadFile(file!); 
},
fileOption);

6 is currently in PREVIEW, and this documentation is for version 2.0 beta 4. Some information relates to prerelease product that may be substantially modified before it's released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

This tutorial shows how to create a .NET command-line app that uses the

rootCommand.SetHandler((file) =>

{ 
    ReadFile(file!); 
},
fileOption);

6 library. You'll begin by creating a simple root command that has one option. Then you'll add to that base, creating a more complex app that contains multiple subcommands and different options for each command.

In this tutorial, you learn how to:

  • Create commands, options, and arguments.
  • Specify default values for options.
  • Assign options and arguments to commands.
  • Assign an option recursively to all subcommands under a command.
  • Work with multiple levels of nested subcommands.
  • Create aliases for commands and options.
  • Work with rootCommand.SetHandler((file) =>
    {  
        ReadFile(file!);  
    },  
    fileOption);  
    

    8,

    rootCommand.SetHandler((file) =>
    {  
        ReadFile(file!);  
    },  
    fileOption);  
    

    9,

    static void ReadFile(FileInfo file)

    {

    File.ReadLines(file.FullName).ToList()  
        .ForEach(line => Console.WriteLine(line));  
    
    }

    0,

    static void ReadFile(FileInfo file)

    {

    File.ReadLines(file.FullName).ToList()  
        .ForEach(line => Console.WriteLine(line));  
    
    }

    1,

    static void ReadFile(FileInfo file)

    {

    File.ReadLines(file.FullName).ToList()  
        .ForEach(line => Console.WriteLine(line));  
    
    }

    2 and enum option types.
  • Bind option values to command handler code.
  • Use custom code for parsing and validating options.

Prerequisites

  • A code editor, such as Visual Studio Code with the C# extension.
  • The .NET 6 SDK.

Or

  • Visual Studio 2022 with the .NET desktop development workload installed.

Create the app

Create a .NET 6 console app project named "scl".

  1. Create a folder named scl for the project, and then open a command prompt in the new folder.
  2. Run the following command:

    dotnet new console --framework net6.0

Install the System.CommandLine package

  • Run the following command:

    dotnet add package System.CommandLine --prerelease

    The

    static void ReadFile(FileInfo file)

    {

    File.ReadLines(file.FullName).ToList()  
        .ForEach(line => Console.WriteLine(line));  
    
    }

    3 option is necessary because the library is still in beta.
  • Replace the contents of Program.cs with the following code: using System.CommandLine;

    namespace scl; class Program {

    static async Task Main(string[] args)  
    {  
        var fileOption = new Option(  
            name: "--file",  
            description: "The file to read and display on the console.");  
        var rootCommand = new RootCommand("Sample app for System.CommandLine");  
        rootCommand.AddOption(fileOption);  
        rootCommand.SetHandler((file) =>  
            {  
                ReadFile(file!);  
            },  
            fileOption);  
        return await rootCommand.InvokeAsync(args);  
    }  
    static void ReadFile(FileInfo file)  
    {  
        File.ReadLines(file.FullName).ToList()  
            .ForEach(line => Console.WriteLine(line));  
    }  
    
    }

The preceding code:

  • Creates an named static void ReadFile(FileInfo file)

    {

    File.ReadLines(file.FullName).ToList()  
        .ForEach(line => Console.WriteLine(line));  
    
    }

    4 of type FileInfo and assigns it to the :

    var fileOption = new Option(
    name: "--file",  
    description: "The file to read and display on the console.");  
    
    var rootCommand = new RootCommand("Sample app for System.CommandLine");

    rootCommand.AddOption(fileOption);

  • Specifies that static void ReadFile(FileInfo file)

    {

    File.ReadLines(file.FullName).ToList()  
        .ForEach(line => Console.WriteLine(line));  
    
    }

    5 is the method that will be called when the root command is invoked:

    rootCommand.SetHandler((file) =>
    {  
        ReadFile(file!);  
    },  
    fileOption);  
    
  • Displays the contents of the specified file when the root command is invoked: static void ReadFile(FileInfo file)

    {

    File.ReadLines(file.FullName).ToList()  
        .ForEach(line => Console.WriteLine(line));  
    
    }

Test the app

You can use any of the following ways to test while developing a command-line app:

  • Run the static void ReadFile(FileInfo file)

    {

    File.ReadLines(file.FullName).ToList()  
        .ForEach(line => Console.WriteLine(line));  
    
    }

    6 command, and then open a command prompt in the scl/bin/Debug/net6.0 folder to run the executable:

    dotnet build cd bin/Debug/net6.0 scl --file scl.runtimeconfig.json

  • Use static void ReadFile(FileInfo file)

    {

    File.ReadLines(file.FullName).ToList()  
        .ForEach(line => Console.WriteLine(line));  
    
    }

    7 and pass option values to the app instead of to the

    static void ReadFile(FileInfo file)

    {

    File.ReadLines(file.FullName).ToList()  
        .ForEach(line => Console.WriteLine(line));  
    
    }

    8 command by including them after

    static void ReadFile(FileInfo file)

    {

    File.ReadLines(file.FullName).ToList()  
        .ForEach(line => Console.WriteLine(line));  
    
    }

    9, as in the following example:

    dotnet run -- --file scl.runtimeconfig.json

    In .NET 7.0.100 SDK Preview, you can use the

    dotnet build cd bin/Debug/net6.0 scl --file scl.runtimeconfig.json

    0 of a launchSettings.json file by running the command

    dotnet build cd bin/Debug/net6.0 scl --file scl.runtimeconfig.json

    1.
  • Publish the project to a folder, open a command prompt to that folder, and run the executable:

    dotnet publish -o publish cd ./publish scl --file scl.runtimeconfig.json

  • In Visual Studio 2022, select Debug > Debug Properties from the menu, and enter the options and arguments in the Command line arguments box. For example:
    Visual studio using.system to the top of file net năm 2024
    Then run the app, for example by pressing Ctrl+F5.

This tutorial assumes you're using the first of these options.

When you run the app, it displays the contents of the file specified by the

static void ReadFile(FileInfo file) {

File.ReadLines(file.FullName).ToList()
    .ForEach(line => Console.WriteLine(line));
}

4 option.

{
  "runtimeOptions": {
    "tfm": "net6.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "6.0.0"
    }
  }
}

Help output

rootCommand.SetHandler((file) =>

{ 
    ReadFile(file!); 
},
fileOption);

6 automatically provides help output:

dotnet add package System.CommandLine --prerelease

0

dotnet add package System.CommandLine --prerelease

1

Version output

rootCommand.SetHandler((file) =>

{ 
    ReadFile(file!); 
},
fileOption);

6 automatically provides version output:

dotnet add package System.CommandLine --prerelease

2

dotnet add package System.CommandLine --prerelease

3

Add a subcommand and options

In this section, you:

  • Create more options.
  • Create a subcommand.
  • Assign the new options to the new subcommand.

The new options will let you configure the foreground and background text colors and the readout speed. These features will be used to read a collection of quotes that comes from the Teleprompter console app tutorial.

  1. Copy the sampleQuotes.txt file from the GitHub repository for this sample into your project directory. For information on how to download files, see the instructions in .
  2. Open the project file and add an

    dotnet build cd bin/Debug/net6.0 scl --file scl.runtimeconfig.json

    5 element just before the closing

    dotnet build cd bin/Debug/net6.0 scl --file scl.runtimeconfig.json

    6 tag:

    dotnet add package System.CommandLine --prerelease

    4 Adding this markup causes the text file to be copied to the bin/debug/net6.0 folder when you build the app. So when you run the executable in that folder, you can access the file by name without specifying a folder path.
  3. In Program.cs, after the code that creates the static void ReadFile(FileInfo file)

    {

    File.ReadLines(file.FullName).ToList()  
        .ForEach(line => Console.WriteLine(line));  
    
    }

    4 option, create options to control the readout speed and text colors:

    dotnet add package System.CommandLine --prerelease

    5
  4. After the line that creates the root command, delete the line that adds the static void ReadFile(FileInfo file)

    {

    File.ReadLines(file.FullName).ToList()  
        .ForEach(line => Console.WriteLine(line));  
    
    }

    4 option to it. You're removing it here because you'll add it to a new subcommand.

    dotnet add package System.CommandLine --prerelease

    6
  5. After the line that creates the root command, create a

    dotnet build cd bin/Debug/net6.0 scl --file scl.runtimeconfig.json

    9 subcommand. Add the options to this subcommand, and add the subcommand to the root command.

    dotnet add package System.CommandLine --prerelease

    7
  6. Replace the

    dotnet run -- --file scl.runtimeconfig.json

    0 code with the following

    dotnet run -- --file scl.runtimeconfig.json

    0 code for the new subcommand:

    dotnet add package System.CommandLine --prerelease

    8 You're no longer calling

    dotnet run -- --file scl.runtimeconfig.json

    0 on the root command because the root command no longer needs a handler. When a command has subcommands, you typically have to specify one of the subcommands when invoking a command-line app.
  7. Replace the static void ReadFile(FileInfo file)

    {

    File.ReadLines(file.FullName).ToList()  
        .ForEach(line => Console.WriteLine(line));  
    
    }

    5 handler method with the following code:

    dotnet add package System.CommandLine --prerelease

    9

The app now looks like this:

using System.CommandLine; namespace scl; class Program {

static async Task Main(string[] args)
{
    var fileOption = new Option(
        name: "--file",
        description: "The file to read and display on the console.");
    var rootCommand = new RootCommand("Sample app for System.CommandLine");
    rootCommand.AddOption(fileOption);
    rootCommand.SetHandler((file) => 
        { 
            ReadFile(file!); 
        },
        fileOption);
    return await rootCommand.InvokeAsync(args);
}
static void ReadFile(FileInfo file)
{
    File.ReadLines(file.FullName).ToList()
        .ForEach(line => Console.WriteLine(line));
}
}

0

Test the new subcommand

Now if you try to run the app without specifying the subcommand, you get an error message followed by a help message that specifies the subcommand that is available.

using System.CommandLine; namespace scl; class Program {

static async Task Main(string[] args)
{
    var fileOption = new Option(
        name: "--file",
        description: "The file to read and display on the console.");
    var rootCommand = new RootCommand("Sample app for System.CommandLine");
    rootCommand.AddOption(fileOption);
    rootCommand.SetHandler((file) => 
        { 
            ReadFile(file!); 
        },
        fileOption);
    return await rootCommand.InvokeAsync(args);
}
static void ReadFile(FileInfo file)
{
    File.ReadLines(file.FullName).ToList()
        .ForEach(line => Console.WriteLine(line));
}
}

1

using System.CommandLine; namespace scl; class Program {

static async Task Main(string[] args)
{
    var fileOption = new Option(
        name: "--file",
        description: "The file to read and display on the console.");
    var rootCommand = new RootCommand("Sample app for System.CommandLine");
    rootCommand.AddOption(fileOption);
    rootCommand.SetHandler((file) => 
        { 
            ReadFile(file!); 
        },
        fileOption);
    return await rootCommand.InvokeAsync(args);
}
static void ReadFile(FileInfo file)
{
    File.ReadLines(file.FullName).ToList()
        .ForEach(line => Console.WriteLine(line));
}
}

2

The help text for subcommand

dotnet build
cd bin/Debug/net6.0
scl --file scl.runtimeconfig.json

9 shows that four options are available. It shows valid values for the enum.

using System.CommandLine; namespace scl; class Program {

static async Task Main(string[] args)
{
    var fileOption = new Option(
        name: "--file",
        description: "The file to read and display on the console.");
    var rootCommand = new RootCommand("Sample app for System.CommandLine");
    rootCommand.AddOption(fileOption);
    rootCommand.SetHandler((file) => 
        { 
            ReadFile(file!); 
        },
        fileOption);
    return await rootCommand.InvokeAsync(args);
}
static void ReadFile(FileInfo file)
{
    File.ReadLines(file.FullName).ToList()
        .ForEach(line => Console.WriteLine(line));
}
}

3

using System.CommandLine; namespace scl; class Program {

static async Task Main(string[] args)
{
    var fileOption = new Option(
        name: "--file",
        description: "The file to read and display on the console.");
    var rootCommand = new RootCommand("Sample app for System.CommandLine");
    rootCommand.AddOption(fileOption);
    rootCommand.SetHandler((file) => 
        { 
            ReadFile(file!); 
        },
        fileOption);
    return await rootCommand.InvokeAsync(args);
}
static void ReadFile(FileInfo file)
{
    File.ReadLines(file.FullName).ToList()
        .ForEach(line => Console.WriteLine(line));
}
}

4

Run subcommand

dotnet build
cd bin/Debug/net6.0
scl --file scl.runtimeconfig.json

9 specifying only the

static void ReadFile(FileInfo file) {

File.ReadLines(file.FullName).ToList()
    .ForEach(line => Console.WriteLine(line));
}

4 option, and you get the default values for the other three options.

using System.CommandLine; namespace scl; class Program {

static async Task Main(string[] args)
{
    var fileOption = new Option(
        name: "--file",
        description: "The file to read and display on the console.");
    var rootCommand = new RootCommand("Sample app for System.CommandLine");
    rootCommand.AddOption(fileOption);
    rootCommand.SetHandler((file) => 
        { 
            ReadFile(file!); 
        },
        fileOption);
    return await rootCommand.InvokeAsync(args);
}
static void ReadFile(FileInfo file)
{
    File.ReadLines(file.FullName).ToList()
        .ForEach(line => Console.WriteLine(line));
}
}

5

The 42 milliseconds per character default delay causes a slow readout speed. You can speed it up by setting

dotnet run -- --file scl.runtimeconfig.json

7 to a lower number.

using System.CommandLine; namespace scl; class Program {

static async Task Main(string[] args)
{
    var fileOption = new Option(
        name: "--file",
        description: "The file to read and display on the console.");
    var rootCommand = new RootCommand("Sample app for System.CommandLine");
    rootCommand.AddOption(fileOption);
    rootCommand.SetHandler((file) => 
        { 
            ReadFile(file!); 
        },
        fileOption);
    return await rootCommand.InvokeAsync(args);
}
static void ReadFile(FileInfo file)
{
    File.ReadLines(file.FullName).ToList()
        .ForEach(line => Console.WriteLine(line));
}
}

6

You can use

dotnet run -- --file scl.runtimeconfig.json

8 and

dotnet run -- --file scl.runtimeconfig.json

9 to set text colors:

using System.CommandLine; namespace scl; class Program {

static async Task Main(string[] args)
{
    var fileOption = new Option(
        name: "--file",
        description: "The file to read and display on the console.");
    var rootCommand = new RootCommand("Sample app for System.CommandLine");
    rootCommand.AddOption(fileOption);
    rootCommand.SetHandler((file) => 
        { 
            ReadFile(file!); 
        },
        fileOption);
    return await rootCommand.InvokeAsync(args);
}
static void ReadFile(FileInfo file)
{
    File.ReadLines(file.FullName).ToList()
        .ForEach(line => Console.WriteLine(line));
}
}

7

Provide an invalid value for

dotnet run -- --file scl.runtimeconfig.json

7 and you get an error message:

using System.CommandLine; namespace scl; class Program {

static async Task Main(string[] args)
{
    var fileOption = new Option(
        name: "--file",
        description: "The file to read and display on the console.");
    var rootCommand = new RootCommand("Sample app for System.CommandLine");
    rootCommand.AddOption(fileOption);
    rootCommand.SetHandler((file) => 
        { 
            ReadFile(file!); 
        },
        fileOption);
    return await rootCommand.InvokeAsync(args);
}
static void ReadFile(FileInfo file)
{
    File.ReadLines(file.FullName).ToList()
        .ForEach(line => Console.WriteLine(line));
}
}

8

using System.CommandLine; namespace scl; class Program {

static async Task Main(string[] args)
{
    var fileOption = new Option(
        name: "--file",
        description: "The file to read and display on the console.");
    var rootCommand = new RootCommand("Sample app for System.CommandLine");
    rootCommand.AddOption(fileOption);
    rootCommand.SetHandler((file) => 
        { 
            ReadFile(file!); 
        },
        fileOption);
    return await rootCommand.InvokeAsync(args);
}
static void ReadFile(FileInfo file)
{
    File.ReadLines(file.FullName).ToList()
        .ForEach(line => Console.WriteLine(line));
}
}

9

Provide an invalid value for

static void ReadFile(FileInfo file) {

File.ReadLines(file.FullName).ToList()
    .ForEach(line => Console.WriteLine(line));
}

4 and you get an exception:

var fileOption = new Option(

name: "--file",
description: "The file to read and display on the console.");
var rootCommand = new RootCommand("Sample app for System.CommandLine"); rootCommand.AddOption(fileOption);

0

var fileOption = new Option(

name: "--file",
description: "The file to read and display on the console.");
var rootCommand = new RootCommand("Sample app for System.CommandLine"); rootCommand.AddOption(fileOption);

1

Add subcommands and custom validation

This section creates the final version of the app. When finished, the app will have the following commands and options:

  • root command with a global* option named static void ReadFile(FileInfo file)

    {

    File.ReadLines(file.FullName).ToList()  
        .ForEach(line => Console.WriteLine(line));  
    
    }

    4

    • dotnet publish -o publish cd ./publish scl --file scl.runtimeconfig.json

      3 command

      •      dotnet build  
             cd bin/Debug/net6.0  
             scl --file scl.runtimeconfig.json  
             
        9 command with options named
        
        dotnet run -- --file scl.runtimeconfig.json
        
        7,
        
        dotnet run -- --file scl.runtimeconfig.json
        
        8, and
        
        dotnet run -- --file scl.runtimeconfig.json
        
        9 
      • dotnet publish -o publish cd ./publish scl --file scl.runtimeconfig.json 8 command with arguments named dotnet publish -o publish cd ./publish scl --file scl.runtimeconfig.json 9 and { "runtimeOptions": { "tfm": "net6.0", "framework": { "name": "Microsoft.NETCore.App", "version": "6.0.0" } } } 0
      • { "runtimeOptions": { "tfm": "net6.0", "framework": { "name": "Microsoft.NETCore.App", "version": "6.0.0" } } } 1 command with option named { "runtimeOptions": { "tfm": "net6.0", "framework": { "name": "Microsoft.NETCore.App", "version": "6.0.0" } } } 2

* A global option is available to the command it's assigned to and recursively to all its subcommands.

Here's sample command line input that invokes each of the available commands with its options and arguments:

var fileOption = new Option(

name: "--file",
description: "The file to read and display on the console.");
var rootCommand = new RootCommand("Sample app for System.CommandLine"); rootCommand.AddOption(fileOption);

2

  1. In Program.cs, replace the code that creates the static void ReadFile(FileInfo file)

    {

    File.ReadLines(file.FullName).ToList()  
        .ForEach(line => Console.WriteLine(line));  
    
    }

    4 option with the following code:

    var fileOption = new Option(
    name: "--file",  
    description: "The file to read and display on the console.");  
    
    var rootCommand = new RootCommand("Sample app for System.CommandLine");

    rootCommand.AddOption(fileOption);

    3 This code uses ParseArgument to provide custom parsing, validation, and error handling. Without this code, missing files are reported with an exception and stack trace. With this code just the specified error message is displayed. This code also specifies a default value, which is why it sets

    { "runtimeOptions": {

    "tfm": "net6.0",  
    "framework": {  
      "name": "Microsoft.NETCore.App",  
      "version": "6.0.0"  
    }  
    
    } }

    4 to

    { "runtimeOptions": {

    "tfm": "net6.0",  
    "framework": {  
      "name": "Microsoft.NETCore.App",  
      "version": "6.0.0"  
    }  
    
    } }

    5. If you don't set

    { "runtimeOptions": {

    "tfm": "net6.0",  
    "framework": {  
      "name": "Microsoft.NETCore.App",  
      "version": "6.0.0"  
    }  
    
    } }

    4 to

    { "runtimeOptions": {

    "tfm": "net6.0",  
    "framework": {  
      "name": "Microsoft.NETCore.App",  
      "version": "6.0.0"  
    }  
    
    } }

    5, the

    { "runtimeOptions": {

    "tfm": "net6.0",  
    "framework": {  
      "name": "Microsoft.NETCore.App",  
      "version": "6.0.0"  
    }  
    
    } }

    8 delegate doesn't get called when no input is provided for

    static void ReadFile(FileInfo file)

    {

    File.ReadLines(file.FullName).ToList()  
        .ForEach(line => Console.WriteLine(line));  
    
    }

    4.
  2. After the code that creates

    dotnet add package System.CommandLine --prerelease

    00, add options and arguments for the

    dotnet publish -o publish cd ./publish scl --file scl.runtimeconfig.json

    8 and

    { "runtimeOptions": {

    "tfm": "net6.0",  
    "framework": {  
      "name": "Microsoft.NETCore.App",  
      "version": "6.0.0"  
    }  
    
    } }

    1 commands:

    var fileOption = new Option(
    name: "--file",  
    description: "The file to read and display on the console.");  
    
    var rootCommand = new RootCommand("Sample app for System.CommandLine");

    rootCommand.AddOption(fileOption);

    4 The setting lets you omit the

    { "runtimeOptions": {

    "tfm": "net6.0",  
    "framework": {  
      "name": "Microsoft.NETCore.App",  
      "version": "6.0.0"  
    }  
    
    } }

    2 option name when specifying elements in the list after the first one. It makes the following examples of command-line input equivalent:

    var fileOption = new Option(
    name: "--file",  
    description: "The file to read and display on the console.");  
    
    var rootCommand = new RootCommand("Sample app for System.CommandLine");

    rootCommand.AddOption(fileOption);

    5
  3. Replace the code that creates the root command and the

    dotnet build cd bin/Debug/net6.0 scl --file scl.runtimeconfig.json

    9 command with the following code:

    var fileOption = new Option(
    name: "--file",  
    description: "The file to read and display on the console.");  
    
    var rootCommand = new RootCommand("Sample app for System.CommandLine");

    rootCommand.AddOption(fileOption);

    6 This code makes the following changes:

    • Removes the static void ReadFile(FileInfo file)

      {

         File.ReadLines(file.FullName).ToList()  
             .ForEach(line => Console.WriteLine(line));  
      
      }

      4 option from the

      dotnet build cd bin/Debug/net6.0 scl --file scl.runtimeconfig.json

      9 command.
    • Adds the static void ReadFile(FileInfo file)

      {

         File.ReadLines(file.FullName).ToList()  
             .ForEach(line => Console.WriteLine(line));  
      
      }

      4 option as a global option to the root command.
    • Creates a

      dotnet publish -o publish cd ./publish scl --file scl.runtimeconfig.json

      3 command and adds it to the root command.
    • Adds the

      dotnet build cd bin/Debug/net6.0 scl --file scl.runtimeconfig.json

      9 command to the

      dotnet publish -o publish cd ./publish scl --file scl.runtimeconfig.json

      3 command instead of to the root command.
    • Creates

      dotnet publish -o publish cd ./publish scl --file scl.runtimeconfig.json

      8 and

      {

       "runtimeOptions": {  
         "tfm": "net6.0",  
         "framework": {  
           "name": "Microsoft.NETCore.App",  
           "version": "6.0.0"  
         }  
       }  
      
      }

      1 commands and adds them to the

      dotnet publish -o publish cd ./publish scl --file scl.runtimeconfig.json

      3 command. The result is the following command hierarchy:
    • Root command
      •      dotnet publish -o publish  
             cd ./publish  
             scl --file scl.runtimeconfig.json  
             
        3
        
        •               dotnet build  
                        cd bin/Debug/net6.0  
                        scl --file scl.runtimeconfig.json  
                        
          9 
        • dotnet publish -o publish cd ./publish scl --file scl.runtimeconfig.json 8
        • { "runtimeOptions": { "tfm": "net6.0", "framework": { "name": "Microsoft.NETCore.App", "version": "6.0.0" } } } 1 The app now implements the recommended pattern where the parent command (

          dotnet publish -o publish cd ./publish scl --file scl.runtimeconfig.json

  4. specifies an area or group, and its children commands (

    dotnet build cd bin/Debug/net6.0 scl --file scl.runtimeconfig.json

    9,

    dotnet publish -o publish cd ./publish scl --file scl.runtimeconfig.json

    8,

    { "runtimeOptions": {

    "tfm": "net6.0",  
    "framework": {  
      "name": "Microsoft.NETCore.App",  
      "version": "6.0.0"  
    }  
    
    } }

  5. are actions.

    Global options are applied to the command and recursively to subcommands. Since

    static void ReadFile(FileInfo file)

    {

    File.ReadLines(file.FullName).ToList()  
        .ForEach(line => Console.WriteLine(line));  
    
    }

    4 is on the root command, it will be available automatically in all subcommands of the app.
  6. After the

    dotnet run -- --file scl.runtimeconfig.json

    0 code, add new

    dotnet run -- --file scl.runtimeconfig.json

    0 code for the new subcommands:

    var fileOption = new Option(
    name: "--file",  
    description: "The file to read and display on the console.");  
    
    var rootCommand = new RootCommand("Sample app for System.CommandLine");

    rootCommand.AddOption(fileOption);

    7 Subcommand

    dotnet publish -o publish cd ./publish scl --file scl.runtimeconfig.json

    3 doesn't have a handler because it isn't a leaf command. Subcommands

    dotnet build cd bin/Debug/net6.0 scl --file scl.runtimeconfig.json

    9,

    dotnet publish -o publish cd ./publish scl --file scl.runtimeconfig.json

    8, and

    { "runtimeOptions": {

    "tfm": "net6.0",  
    "framework": {  
      "name": "Microsoft.NETCore.App",  
      "version": "6.0.0"  
    }  
    
    } }

    1 are leaf commands under

    dotnet publish -o publish cd ./publish scl --file scl.runtimeconfig.json

    3, and

    dotnet run -- --file scl.runtimeconfig.json

    0 is called for each of them.
  7. Add the handlers for

    dotnet publish -o publish cd ./publish scl --file scl.runtimeconfig.json

    8 and

    { "runtimeOptions": {

    "tfm": "net6.0",  
    "framework": {  
      "name": "Microsoft.NETCore.App",  
      "version": "6.0.0"  
    }  
    
    } }

  8. var fileOption = new Option(
    name: "--file",  
    description: "The file to read and display on the console.");  
    
    var rootCommand = new RootCommand("Sample app for System.CommandLine");

    rootCommand.AddOption(fileOption);

    8

The finished app looks like this:

var fileOption = new Option(

name: "--file",
description: "The file to read and display on the console.");
var rootCommand = new RootCommand("Sample app for System.CommandLine"); rootCommand.AddOption(fileOption);

9

Build the project, and then try the following commands.

Submit a nonexistent file to

static void ReadFile(FileInfo file) {

File.ReadLines(file.FullName).ToList()
    .ForEach(line => Console.WriteLine(line));
}

4 with the

dotnet build
cd bin/Debug/net6.0
scl --file scl.runtimeconfig.json

9 command, and you get an error message instead of an exception and stack trace:

rootCommand.SetHandler((file) =>

{ 
    ReadFile(file!); 
},
fileOption);

0

rootCommand.SetHandler((file) =>

{ 
    ReadFile(file!); 
},
fileOption);

1

Try to run subcommand

dotnet publish -o publish
cd ./publish
scl --file scl.runtimeconfig.json

3 and you get a message directing you to use

dotnet build
cd bin/Debug/net6.0
scl --file scl.runtimeconfig.json

9,

dotnet publish -o publish
cd ./publish
scl --file scl.runtimeconfig.json

8, or

{
  "runtimeOptions": {
    "tfm": "net6.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "6.0.0"
    }
  }
}

1:

rootCommand.SetHandler((file) =>

{ 
    ReadFile(file!); 
},
fileOption);

2

rootCommand.SetHandler((file) =>

{ 
    ReadFile(file!); 
},
fileOption);

3

Run subcommand

dotnet publish -o publish
cd ./publish
scl --file scl.runtimeconfig.json

8, and then look at the end of the text file to see the added text:

rootCommand.SetHandler((file) =>

{ 
    ReadFile(file!); 
},
fileOption);

4

Run subcommand

{
  "runtimeOptions": {
    "tfm": "net6.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "6.0.0"
    }
  }
}

1 with search strings from the beginning of the file, and then look at the beginning of the text file to see where text was removed:

rootCommand.SetHandler((file) =>

{ 
    ReadFile(file!); 
},
fileOption);

5

Note

If you're running in the bin/debug/net6.0 folder, that folder is where you'll find the file with changes from the

dotnet publish -o publish
cd ./publish
scl --file scl.runtimeconfig.json

8 and

{
  "runtimeOptions": {
    "tfm": "net6.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "6.0.0"
    }
  }
}

1 commands. The copy of the file in the project folder remains unchanged.

Next steps

In this tutorial, you created a simple command-line app that uses

rootCommand.SetHandler((file) =>

{ 
    ReadFile(file!); 
},
fileOption);

6. To learn more about the library, see System.CommandLine overview.

Collaborate with us on GitHub

The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.

How to sort using System first in C#?

From Tools Menu, Navigate to Options –> Text Editor –-> C# –> Advanced. In the Advance options select the “Place 'System' directives first when sorting usings” check box. That's all.nullAutomatically place the 'System' directives first when 'Sorting Usings ...dailydotnettips.com › automatically-place-the-system-directives-first-when-...null

What is using System IO in C#?

The System.IO namespace contains all the classes that deal with input/outputs like reading/writing files. IO stands for Input and Output . Basically System.IO contains methods for reading and writing files as well as using Streams like you have said already.nullWhat does "System.IO" mean in C#? [closed] - Stack Overflowstackoverflow.com › questions › what-does-system-io-mean-in-cnull

Should I use top level statements C#?

WriteLine("Hello World!"); Top-level statements let you write simple programs for small utilities such as Azure Functions and GitHub Actions. They also make it simpler for new C# programmers to get started learning and writing code.5 ngày trướcnullTop-level statements - programs without Main methods - C

learn.microsoft.com › dotnet › csharp › fundamentals › program-structurenull

Do you need using System in C#?

WriteLine, the System namespace is required. Discussion. The C# language uses aliased keywords that map to types. For example, if you use the keyword int, you are using System.nullC# System (using System namespace) - Dot Net Perlswww.dotnetperls.com › systemnull