Skip to content

Commit bdf7d76

Browse files
committed
Document the ability to set aliases and hidden status via command name
- Add documentation for the pipe syntax to define command aliases - Document how to make commands hidden using the `|` prefix - Show examples for both #[AsCommand] attribute and constructor usage - Include version annotation for Symfony 7.4
1 parent e9f04f4 commit bdf7d76

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

console.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,60 @@ You can register the command by adding the ``AsCommand`` attribute to it::
240240
// ...
241241
}
242242

243+
.. versionadded:: 7.4
244+
245+
The ability to define aliases and hidden status through the command name was introduced in Symfony 7.4.
246+
247+
You can also define aliases and hidden status directly in the command name using
248+
a pipe (``|``) separator::
249+
250+
// src/Command/CreateUserCommand.php
251+
namespace App\Command;
252+
253+
use Symfony\Component\Console\Attribute\AsCommand;
254+
use Symfony\Component\Console\Command\Command;
255+
256+
// Define a command with aliases
257+
#[AsCommand(
258+
name: 'app:create-user|app:add-user|app:new-user',
259+
description: 'Creates a new user.',
260+
)]
261+
class CreateUserCommand extends Command
262+
{
263+
// ...
264+
}
265+
266+
// Define a hidden command
267+
#[AsCommand(
268+
name: '|app:create-user',
269+
description: 'Creates a new user.',
270+
)]
271+
class CreateUserCommand extends Command
272+
{
273+
// ...
274+
}
275+
276+
When using the pipe syntax:
277+
278+
- The first name in the list defines the command name, the others are aliases
279+
- When the first name is empty (starts with ``|``), the command is marked as hidden
280+
281+
This syntax also works when passing the name to the parent constructor::
282+
283+
class CreateUserCommand extends Command
284+
{
285+
public function __construct()
286+
{
287+
// Define command with aliases
288+
parent::__construct('app:create-user|app:add-user');
289+
290+
// Or define a hidden command
291+
// parent::__construct('|app:create-user');
292+
}
293+
294+
// ...
295+
}
296+
243297
If you can't use PHP attributes, register the command as a service and
244298
:doc:`tag it </service_container/tags>` with the ``console.command`` tag. If you're using the
245299
:ref:`default services.yaml configuration <service-container-services-load-example>`,

console/hide_commands.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,36 @@ the ``hidden`` property of the ``AsCommand`` attribute::
2323
// ...
2424
}
2525

26+
.. versionadded:: 7.4
27+
28+
The ability to define a command as hidden using the pipe syntax in the command name was introduced in Symfony 7.4.
29+
30+
You can also define a command as hidden using the pipe (``|``) syntax in the command name::
31+
32+
// src/Command/LegacyCommand.php
33+
namespace App\Command;
34+
35+
use Symfony\Component\Console\Attribute\AsCommand;
36+
use Symfony\Component\Console\Command\Command;
37+
38+
#[AsCommand(name: '|app:legacy')]
39+
class LegacyCommand extends Command
40+
{
41+
// ...
42+
}
43+
44+
This syntax also works when passing the name to the parent constructor::
45+
46+
class LegacyCommand extends Command
47+
{
48+
public function __construct()
49+
{
50+
parent::__construct('|app:legacy');
51+
}
52+
53+
// ...
54+
}
55+
2656
Hidden commands behave the same as normal commands but they are no longer displayed
2757
in command listings, so end-users are not aware of their existence.
2858

0 commit comments

Comments
 (0)