Langsung ke konten
Skillpedia
Pemrograman umum40k Β· #7446

system-commandline-cli

Fungsinya

Alat ini membantu membuat dan mengatur command untuk program command-line .NET. Alat ini menentukan bagaimana command, option, dan argument harus disusun serta cara kerjanya satu sama lain.

Kapan dipakai

  • βœ“Saat membuat command baru untuk program command-line.
  • βœ“Saat menambahkan option atau argument ke command yang sudah ada.
  • βœ“Saat mengelompokkan command ke dalam grup yang logis.

Link skill

https://github.com/github/awesome-copilot/blob/main/skills/system-commandline-cli/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/github/awesome-copilot/blob/main/skills/system-commandline-cli/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit β†’

Contoh permintaan ke AI

β€œBagaimana cara menambahkan option bernama --output yang menerima teks ke sebuah command?”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

Alat ini menggunakan struktur yang sudah ditentukan untuk membuat class command. Setiap command memiliki nama dan deskripsi, serta bisa memiliki option (seperti --verbose) atau argument (seperti nama file). Aksi command didefinisikan di method yang terpisah.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.

System.CommandLine CLI Developer Skill

You are working on a .NET CLI application built with System.CommandLine v2.x.x, targeting .NET 8 or later or any .NET Standard 2.0 implementation, including .NET Framework 4.6.1 or later and .NET Core 2.0 or later. Follow these rules and patterns strictly when creating or modifying CLI commands.


Architecture Overview

<CLI Project>/
β”œβ”€β”€ Program.cs                       # Entry point and command invocation
└── Commands/
    β”œβ”€β”€ CommandBase.cs               # Base class for all commands
    β”œβ”€β”€ GlobalOptions.cs             # Defines global options for the CLI
    β”œβ”€β”€ RootCommand.cs               # Registers top-level commands
    └── <Group>/                     # One folder per command group
        β”œβ”€β”€ <Group>Command.cs        # Parent command that registers its children
        └── <Group><Verb>Command.cs  # Leaf command with its handler

RULE 1 β€” Prefer a Project-Specific Command Base Class

Prefer defining a project-specific abstract CommandBase that inherits from System.CommandLine.Command. Concrete commands should inherit from this base class so shared behavior and conventions remain centralized.

internal abstract class CommandBase : Command
{
    protected CommandBase(string name, string? description = null)
        : base(name, description)
    {
    }
}

internal sealed class MyCommand : CommandBase
{
    public MyCommand()
        : base("command-name", "Help text shown in --help")
    {
        this.SetAction(CommandHandler);
    }

    private async Task<int> CommandHandler(
        ParseResult parseResult,
        CancellationToken cancellationToken)
    {
        // implementation
        return 0;
    }
}

When the project already has a command base class, preserve its established conventions. Otherwise, introduce one when commands need shared behavior; simple applications may inherit from Command directly when a base class adds no meaningful value.


RULE 2 β€” Options and Arguments

Defining Options

private readonly Option<string> _myOption;

// In constructor:
_myOption = new Option<string>("--my-option")
{
    Description = "Clear description of what this option does",
    Required = true,   // or false
};
_myOption.Aliases.Add("-m");       // Add a short alias
this.Options.Add(_myOption);

Defining Arguments (positional)

private readonly Argument<string> _fileArgument;

// In constructor:
_fileArgument = new Argument<string>("file")
{
    Description = "Path to the input file"
};
this.Arguments.Add(_fileArgument);

Reading Values in Handlers

// Required option/argument β€” use GetValue:
var value = parseResult.GetValue(_myOption);

RULE 3 β€” Command Handler Pattern

Handlers are async methods wired via SetAction:

this.SetAction(CommandHandler);

private async Task<int> CommandHandler(ParseResult parseResult, CancellationToken cancellationToken)
{
    // 1. Read option/argument values
    // 2. Load session settings (if needed)
    // 3. Validate configuration early β€” fail fast with clear error
    // 4. Execute business logic
    // 5. Output results with Console

    return 0; // or non-zero exit code
}

RULE 4 β€” Command Group (Parent with Subcommands)

A group command registers children but does not call SetAction:

internal class MyGroupCommand : CommandBase
{
    public MyGroupCommand()
        : base("mygroup", "Manages my-group resources")
    {
        this.Subcommands.Add(new MyGroupListCommand());
        this.Subcommands.Add(new MyGroupCreateCommand());
        this.Subcommands.Add(new MyGroupDeleteCommand());
    }
}

A command may define both an action and subcommands when the direct invocation has meaningful behavior.


RULE 5 β€” Registration

  • Top-level commands β†’ register in RootCommand.cs:

    this.Subcommands.Add(new MyGroupCommand());
    
  • Subcommands β†’ register inside the parent command's constructor:

    this.Subcommands.Add(new MyGroupCreateCommand());
    

RULE 6 β€” User Confirmation for Destructive Operations

Console.WriteLine("Are you sure you want to delete X? This action cannot be undone. (yes/no)");
var confirmation = Console.ReadLine();
if (confirmation?.ToLower() != "yes" && confirmation?.ToLower() != "y")
{
    Console.WriteLine("Operation cancelled.");
    return 0;
}

…

Pertanyaan umum

Apa itu 'System.CommandLine'?

Itu adalah library .NET yang memudahkan Anda membangun aplikasi command-line yang andal dan ramah pengguna.

Apa perbedaan antara Option dan Argument?

Option biasanya diawali dengan '--' (misalnya --verbose) dan bersifat opsional atau dapat dikonfigurasi. Argument adalah nilai posisional yang diharapkan oleh command (misalnya nama file).

Apa itu 'Command Handler'?

Itu adalah method yang berisi logika utama dari apa yang harus dilakukan sebuah command saat dijalankan.

Detail

Deskripsi asli dari pembuat

Use this skill when adding, modifying, or reviewing CLI commands in a .NET project built with System.CommandLine. Triggers include: creating a new CLI command, adding options or arguments, wiring command handlers, registering subcommands, building command groups, or any architecture decision about CLI command structure. Also use when the user mentions 'System.CommandLine', 'CommandBase', 'SetActio

repo: github/awesome-copilot

Popularitas

Bintang GitHub
40k
Peringkat
#7446
Lisensi
MIT
Diperbarui
15/10/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa