Logo Console Commands

Console Commands

Cộng đồng
fatonh
console-commands

Use when creating or refactoring Symfony console commands — never extends Command. On Symfony 8.1+ put #[AsCommand] on a method of a service class (even for a single command); on 7.x use the invokable attribute class. Also covers typed CLI inputs via value resolvers and #[MapInput] DTOs. Use when the task mentions console command, #[AsCommand], bin/console, #[Argument], #[Option], or a CLI task.

Tổng quan

Nhà phát hànhfatonh
Kho mãsymfony-skills
Tên kỹ năngconsole-commands
Sao
36
Bản fork
3
Tệp đi kèm
Chỉ có hướng dẫn
Giấy phépMIT
Liên kết
  • Hướng dẫn bằng Markdown

    Tệp SKILL.md được mô hình tải khi cần, nên chỉ tốn token khi có yêu cầu thực sự phù hợp.

  • Dùng được với mọi LLM

    Kỹ năng AI là Markdown thuần túy, không phải mã dành riêng cho một nhà cung cấp, nên dùng được với GPT, Claude, Gemini, Grok hoặc mô hình cục bộ.

  • Đầy đủ trong hướng dẫn

    Mọi thứ mô hình cần đều nằm trong hướng dẫn — không có tệp bổ sung cần đồng bộ.

  • Mã nguồn mở

    Được fatonh công bố trên GitHub. Hãy đọc nội dung nguồn trước khi cài đặt.

Cài đặt

Cài đặt kỹ năng AI Console Commands trong TypingMind để dùng với bất kỳ LLM nào, hoặc thêm vào một tác nhân AI khác có thể đọc SKILL.md.

1

Cài đặt trong TypingMind

TypingMind cài đặt kỹ năng trực tiếp từ thư mục GitHub — đọc SKILL.md, đóng gói các tệp tài nguyên và lưu kết quả trên máy.

  1. Mở ứng dụng và vào Plugin → Kỹ năng.
  2. Chọn "Cài đặt từ GitHub".
  3. Dán URL thư mục kỹ năng bên dưới và xác nhận.
    https://github.com/fatonh/symfony-skills/tree/main/skills/console-commands
  4. Bật kỹ năng trong từng cuộc trò chuyện mà bạn muốn sử dụng kỹ năng đó.
Plugin → Kỹ năng → Thêm kỹ năng → Từ URL GitHub, sau đó dán URL thư mục và nhấn Tiếp tục.
2

Cài đặt trong tác nhân AI khác

Bất kỳ tác nhân AI nào đọc được định dạng Agent Skills đều có thể dùng kỹ năng này — hãy sao chép thư mục vào thư mục kỹ năng của tác nhân AI đó.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/fatonh/symfony-skills.git /tmp/symfony-skills
mkdir -p .claude/skills
cp -r /tmp/symfony-skills/skills/console-commands .claude/skills/console-commands
Khởi động lại Claude Code sau khi sao chép để ứng dụng nhận kỹ năng mới.

Sử dụng trong TypingMind

Bật Console Commands trong bất kỳ cuộc trò chuyện nào trên TypingMind, mô hình sẽ tự xử lý phần còn lại. Tên và mô tả của kỹ năng nằm trong prompt hệ thống. Ngay khi có yêu cầu phù hợp, mô hình tự tải toàn bộ hướng dẫn — bạn không cần gọi thủ công và kỹ năng không tốn token cho đến khi thực sự được dùng.

Mô hình tự tải Console Commands ngay khi có yêu cầu phù hợp.

Dùng được với mọi mô hình AI

Kỹ năng AI là các hướng dẫn Markdown thuần túy, không phải mã dành riêng cho một nhà cung cấp, nên Console Commands không bị giới hạn ở mô hình mà kỹ năng này được viết cho. Chỉ cần cài đặt một lần trong TypingMind rồi dùng với GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama hoặc mô hình cục bộ do bạn tự chạy — tất cả đều dùng khóa API của riêng bạn.

  • Chỉ tải khi cần

    Prompt hệ thống chỉ chứa tên và mô tả. Hướng dẫn được tải khi có yêu cầu phù hợp đầu tiên, nên kỹ năng chưa dùng không tốn chi phí.

  • Đổi mô hình ngay trong cuộc trò chuyện

    Vì kỹ năng là hướng dẫn chứ không phải mã, việc đổi mô hình không làm kỹ năng ngừng hoạt động — mô hình tiếp theo vẫn đọc cùng tệp SKILL.md.

Hướng dẫn của kỹ năng

Đây là nội dung SKILL.md mà mô hình tải. Hãy đọc trước khi cài đặt — kỹ năng là những hướng dẫn mà mô hình của bạn sẽ làm theo.

Symfony Console commands

A console command is just another transport into the service layer — treat the CLI like a controller: thin, typed at the edges, delegating to services.

Never extends Command. No configure(), no execute(), no addArgument()/addOption(), no #[AsCommand(name: ..., description: ...)] named parameters. That is the legacy Symfony 6/7 boilerplate this skill exists to replace — on every supported version, including a single one-off command.

Step 1 — detect the Symfony version, then pick the form

Before writing anything, read the project's symfony/console constraint in composer.json and choose:

  • Symfony 8.1 or newer → always the method-based form (#[AsCommand] on a method of a service class). This holds even for a single command — one method on one class. Do not drop to the class-based form just because there is only one command.
  • Symfony 7.x → the invokable class form (#[AsCommand] on the class, an __invoke() method, no extends).

If composer.json is missing or the constraint is ambiguous, check symfony/console in composer.lock, or ask which version the project targets — don't guess. The two forms differ enough that picking the wrong one is worse than a one-line question.

Symfony 8.1+: a single command (method on a service class)

Even one command uses the method-based form on 8.1+. This is the shape to reach for first:

php
<?php

declare(strict_types=1);

namespace App\Service;

use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;

final class UserService
{
    public function __construct(
        private readonly UserRepository $users,
        private readonly EntityManagerInterface $em,
    ) {}

    #[AsCommand('app:user:delete', 'delete a user by email')]
    public function delete(SymfonyStyle $io, #[Argument('email of the user to delete')] string $email): int
    {
        $user = $this->users->findOneBy(['email' => $email]);
        if (null === $user) {
            $io->error(sprintf('No user found with email "%s".', $email));

            return Command::FAILURE;
        }

        if (!$io->confirm(sprintf('Delete user "%s"? This cannot be undone.', $email), false)) {
            $io->comment('Aborted.');

            return Command::SUCCESS;
        }

        $this->em->remove($user);
        $this->em->flush();
        $io->success(sprintf('User "%s" deleted.', $email));

        return Command::SUCCESS;
    }
}

Note what is absent: no extends Command, no configure(), no execute(), no addArgument(). The argument is declared inline with #[Argument]; dependencies are constructor-injected. When a second related command appears (app:user:create, app:user:promote), add it as another method on this same class — see the family form below.

Symfony 7.x fallback: one command per class

php
<?php

declare(strict_types=1);

namespace App\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand('site:scan', 'scan monitored sites for availability')]
final class SiteScanCommand
{
    public function __construct(
        private readonly SiteRepository $sites,
    ) {}

    public function __invoke(SymfonyStyle $io): int
    {
        $io->success(sprintf('%d sites scanned', \count($this->sites->findAll())));

        return Command::SUCCESS;
    }
}
  • Use the invokable command (__invoke) — no extends Command, no configure()/execute() boilerplate.
  • Inject dependencies through the constructor.

Symfony 8.1+: a family of commands on one service class

The single-command form above scales directly to a family: keep adding method-level #[AsCommand] to the same service class. One class exposes a whole family of related commands that share a single constructor and private helpers.

php
<?php

declare(strict_types=1);

namespace App\Service;

use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Attribute\MapInput;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;

final class SiteService
{
    public function __construct(
        private readonly SiteRepository $sites,
        private readonly EntityManagerInterface $em,
        private readonly HttpClientInterface $http,
    ) {}

    #[AsCommand('site:add', 'add a site to monitor')]
    public function add(SymfonyStyle $io, #[Argument] SiteUrl $url): int
    {
        // ...
        return Command::SUCCESS;
    }

    #[AsCommand('site:scan', 'scan monitored sites for availability')]
    public function scan(SymfonyStyle $io, #[MapInput] ScanInput $input): int
    {
        // ...
        return Command::SUCCESS;
    }

    // Shared by the sibling commands above — the reason they live together.
    private function findOrFail(SiteUrl $url): Site
    {
        return $this->sites->findOneByUrl($url) ?? throw new \RuntimeException('unknown site');
    }
}
  • One class holds a cohesive family of commands (site:add, site:scan, site:list) that share injected dependencies and private helpers.
  • The class name reflects its identity as a service, not a command holder: SiteService, not SiteCommands. The CLI is just another transport into it.
  • When a method's private helpers stop being shared with its siblings, that's the signal to split it into its own class. Don't build god-classes, and don't go back to one-class-per-command.

Rules

  • Never extends Command. Import Symfony\Component\Console\Command\Command only for the return constants.
  • Return Command::SUCCESS / Command::FAILURE / Command::INVALID.
  • #[AsCommand('name', 'description')] — positional description (second argument), not the description: named parameter.
  • #[Argument('desc')] and #[Option('desc')] — same convention, positional description string. Never description:.
  • First parameter is typically SymfonyStyle $io (when you want its helpers — success, table, progressBar) or OutputInterface $output (leaner). Pick per command.
  • declare(strict_types=1); in every file.
  • Verbose output via $io->writeln() gated on $io->isVerbose() / $io->isVeryVerbose(). No dump()/dd() in committed commands.

Typed inputs — value resolvers and DTOs

Validation, normalization, and parsing belong in value objects and their resolvers, not in command bodies. (Available with attribute-based commands.)

  • Single typed atoms (URL, email, ULID, path) → a value object with a custom ValueResolverInterface. The value object's fromString() factory validates; the resolver maps the raw CLI string to the typed object. Used as #[Argument] SiteUrl $url.
  • Groups of related inputs (several args/options that travel together) → #[MapInput] DTO classes. Public properties carry #[Argument] / #[Option]. Validate in property hooks (PHP 8.4) or via the Symfony Validator — not in the constructor, because #[MapInput] DTOs are hydrated without calling the constructor.
  • Composition — DTOs can contain other DTOs (ApplyInput { public ScanInput $scan; /* + options */ }); Symfony merges them automatically.
  • Why this matters — a description like 'url of the site to monitor' lives once on the value object or DTO, not repeated across five command methods. Same for the validation logic.
php
final class ScanInput
{
    #[Argument('url of the site to scan')]
    public SiteUrl $url;

    #[Option('follow redirects')]
    public bool $follow = false {
        // validate in a property hook, not the constructor
        set => $value;
    }
}

Use Symfony Validator constraints (#[Assert\Url], #[Assert\Email], …) for validation — idiomatic and consistent with the rest of Symfony.

Gotchas

  • Agent writes extends Command with configure()/execute()/addArgument() boilerplate — that's legacy Symfony 6/7 style; use the attribute command on every supported version.
  • Agent drops to the class-based (or extends Command) form on Symfony 8.1+ just because it's a single command — a single command on 8.1+ is still one method with #[AsCommand] on a service class. Check composer.json first.
  • Agent puts #[AsCommand] on a class on Symfony 8.1+ when several related commands exist — prefer one service class with method-level #[AsCommand].
  • Agent names the class *Commands — name it *Service; the CLI is a transport into the service.
  • Agent uses the description: named parameter — descriptions are positional on #[AsCommand], #[Argument], and #[Option].
  • Agent parses/validates CLI strings inside the command body — move it into a value object + ValueResolverInterface, or a #[MapInput] DTO.
  • Agent puts validation in a #[MapInput] DTO constructor — it's never called; use property hooks (PHP 8.4) or the Validator.
  • Agent splits a cohesive command family into one-class-per-command — keep them together while they share helpers; split only when they stop sharing.
  • Agent returns 0/1 integers — use Command::SUCCESS / Command::FAILURE / Command::INVALID.
  • Agent leaves dump()/dd() or ungated verbose output — gate on $io->isVerbose().

Câu hỏi thường gặp

Kỹ năng AI Console Commands làm được gì?

Use when creating or refactoring Symfony console commands — never extends Command. On Symfony 8.1+ put #[AsCommand] on a method of a service class (even for a single command); on 7.x use the invokable attribute class. Also covers typed CLI inputs via value resolvers and #[MapInput] DTOs. Use when the task mentions console command, #[AsCommand], bin/console, #[Argument], #[Option], or a CLI task.

Vì sao nên dùng Console Commands trên TypingMind?

Vì bạn chỉ cần cài một lần rồi dùng với bất kỳ mô hình nào. Console Commands là nội dung Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên cùng một kỹ năng có thể chạy trên GPT-5, Claude, Gemini, Grok hoặc mô hình trên thiết bị — và bạn có thể đổi mô hình ngay trong cuộc trò chuyện mà kỹ năng vẫn hoạt động. TypingMind dùng khóa API riêng của bạn, nên bạn thanh toán trực tiếp cho nhà cung cấp thay vì trả phí thuê bao theo suất người dùng, đồng thời kỹ năng và các cuộc trò chuyện vẫn nằm trong bộ lưu trữ của bạn.

Làm thế nào để cài Console Commands trong TypingMind?

Mở Plugin → Kỹ năng → Cài đặt từ GitHub trong TypingMind rồi dán https://github.com/fatonh/symfony-skills/tree/main/skills/console-commands. TypingMind đọc tệp SKILL.md và cài đặt thành một kỹ năng mà bạn có thể bật cho từng cuộc trò chuyện.

Những mô hình AI nào có thể dùng Console Commands?

Bất kỳ mô hình nào bạn kết nối trong TypingMind. Kỹ năng AI là các hướng dẫn bằng Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên GPT, Claude, Gemini, Grok và các mô hình chạy trên thiết bị đều có thể tải kỹ năng này khi có yêu cầu phù hợp.

Bạn có thể dùng Console Commands với bao nhiêu mô hình AI?

Bao nhiêu cũng được. Miễn là mô hình hỗ trợ kỹ năng, bạn có thể dùng Console Commands với mô hình đó — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama và nhiều mô hình khác — tất cả đều trên TypingMind với khóa API riêng của bạn.

Kỹ năng AI Console Commands có miễn phí không?

Có. Kỹ năng này được fatonh phát hành trên GitHub theo giấy phép MIT. Bạn chỉ trả tiền cho nhà cung cấp AI của mình theo số token đã dùng.

Kỹ năng AI là gì?

Kỹ năng AI là một gói hướng dẫn có thể tái sử dụng, chỉ cho mô hình AI cách thực hiện một tác vụ cụ thể. Kỹ năng tuân theo định dạng mở Agent Skills: một tệp SKILL.md chứa tên và mô tả, cùng các tập lệnh, mẫu hoặc tệp tham khảo mà mô hình có thể cần. Mô hình chỉ đọc hướng dẫn khi yêu cầu của bạn phù hợp với kỹ năng, nên kỹ năng đã cài đặt không phát sinh chi phí cho đến khi được sử dụng.

Kỹ năng AI khác gì với plugin hoặc máy chủ MCP?

Plugin hoặc máy chủ MCP cung cấp cho mô hình các công cụ mới để gọi — mã được chạy ở đâu đó và trả về kết quả. Còn kỹ năng AI cung cấp kiến thức và quy trình: cách tiếp cận tác vụ, các bước cần thực hiện và tiêu chí của đầu ra tốt. Kỹ năng chỉ là nội dung Markdown, nên không cần máy chủ, khóa API hay môi trường thực thi và hoạt động với mọi mô hình.

Xem tất cả

Tạo ngay không gian làm việc AI riêng

Đăng ký bản tin của chúng tôi để nhận thông báo về tính năng mới và các đợt tặng quà sắp tới 👇