System.CommandLine Documentation

repository·main·Indexed 25 days ago

https://github.com/dotnet/command-line-api

Libraries and tools for building robust command-line interfaces in .NET. Includes the System.CommandLine library for parsing, model binding, and invocation, as well as the dotnet-suggest global tool for providing shell completions across Bash, Zsh, PowerShell, Fish, and Nushell. Supports .NET 8.0+, Trimming, and AOT compilation.

Tokens
4.3K
Snippets
15
Records
22
Agent score
87%

What's inside System.CommandLine

  1. Overview of System.CommandLine and dotnet-suggest

    main

    This repository provides the core libraries and tools for building command-line interfaces in .NET:

    • System.CommandLine: A library for command-line parsing, model binding, invocation, and shell completions.
    • dotnet-suggest: A global tool designed to provide shell completions for applications built using System.CommandLine.
  2. Generate shell completion scripts for users

    main

    Once the completions command is integrated, users can generate scripts for their specific shell. If the shell argument is omitted, the tool detects the current shell from the environment (e.g., SHELL on Unix or PowerShell on Windows).

    # Generate a script for a specific shell
    > myapp completions script bash > ~/.local/share/bash-completion/completions/myapp
    
    # The shell argument is optional - it defaults to the current shell
    > myapp completions script >> $PROFILE
  3. Choose between Arcade template sets

    main

    Arcade provides two distinct template sets depending on your pipeline requirements:

    • 1ES Pipeline Templates: Use eng/common/templates-official for any internal production-graded pipelines or runs that must be managed by 1ES pipeline templates (including 1ES Microbuild templates).
    • Standard Templates: Use eng/common/templates for all other pipeline runs.

    Refer to azure-pipelines.yml for a templates-official example and azure-pipelines-pr.yml for a standard templates example.

  4. Build complex CLI applications with subcommands

    main

    You can nest Command objects within each other to create a hierarchy of subcommands. Each subcommand can have its own unique options and arguments.

    var rootCommand = new RootCommand("My application");
    
    var configCommand = new Command("config", "Configure the application");
    var configSetCommand = new Command("set", "Set a configuration value");
    var configGetCommand = new Command("get", "Get a configuration value");
    
    var keyOption = new Option<string>("--key")
    {
        Description = "Configuration key"
    };
    var valueOption = new Option<string>("--value")
    {
        Description = "Configuration value"
    };
    
    configSetCommand.Options.Add(keyOption);
    configSetCommand.Options.Add(valueOption);
    configGetCommand.Options.Add(keyOption);
    
    configCommand.Subcommands.Add(configSetCommand);
    configCommand.Subcommands.Add(configGetCommand);
    rootCommand.Subcommands.Add(configCommand);
    
    // Usage: myapp config set --key "apiUrl" --value "https://api.example.com"
    // Usage: myapp config get --key "apiUrl"
  5. Create a basic command-line application

    main

    Use RootCommand to create the entry point of your application. A RootCommand automatically provides --help and --version options and integrates with dotnet-suggest for shell completions. You can define Options and use SetAction to define the logic that executes when the command is run.

    using System.CommandLine;
    
    RootCommand rootCommand = new("Sample command-line app");
    
    Option<string> nameOption = new("--name", "-n")
    {
        Description = "Your name"
    };
    
    rootCommand.Options.Add(nameOption);
    
    rootCommand.SetAction(parseResult =>
    {
        string name = parseResult.GetValue(nameOption);
        Console.WriteLine($"Hello, {name ?? "World"}!");
    });
    
    return rootCommand.Parse(args).Invoke();
  6. Enable shell completions

    main

    System.CommandLine supports tab completion. You can add custom suggestions to options using CompletionSources. To allow users to use these completions, they should install dotnet-suggest and register your command.

    // Add custom completions using CompletionSources
    var fileOption = new Option<FileInfo>("--file")
    {
        Description = "The file to process"
    };
    
    fileOption.CompletionSources.Add(ctx =>
        // hard-coded list of files
        ["file1.txt", "file2.txt", "file3.txt" ]
    );
    
    // Or add simple string suggestions
    fileOption.CompletionSources.Add("option1", "option2", "option3");
    
    // To use in shell:
    // 1. Install dotnet-suggest
    // 2. Register your app:
    // dotnet suggest register --command-path /path/to/myapp