Skip to content

Commit 31f45da

Browse files
committed
Document the ability to set aliases and hidden status via command name
- Add separate documentation sections for command aliases and hidden commands - Document 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 separate version annotations for Symfony 7.4 for each feature
1 parent e9f04f4 commit 31f45da

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

console.rst

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

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

console/hide_commands.rst

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

26+
You can also define a command as hidden using the pipe (``|``) syntax in the command name::
27+
28+
// src/Command/LegacyCommand.php
29+
namespace App\Command;
30+
31+
use Symfony\Component\Console\Attribute\AsCommand;
32+
use Symfony\Component\Console\Command\Command;
33+
34+
#[AsCommand(name: '|app:legacy')]
35+
class LegacyCommand extends Command
36+
{
37+
// ...
38+
}
39+
40+
.. versionadded:: 7.4
41+
42+
The ability to define a command as hidden using the pipe syntax in the command name was introduced in Symfony 7.4.
43+
2644
Hidden commands behave the same as normal commands but they are no longer displayed
2745
in command listings, so end-users are not aware of their existence.
2846

0 commit comments

Comments
 (0)