Skip to content

Commit 71b1c5e

Browse files
committed
minor #21166 [Console] Document the ability to set aliases and hidden status via command name (OskarStark)
This PR was merged into the 7.4 branch. Discussion ---------- [Console] 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 Fix #21070 Commits ------- f5ca9c5 Document the ability to set aliases and hidden status via command name
2 parents 2f9c5cc + f5ca9c5 commit 71b1c5e

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

console.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,33 @@ After configuring and registering the command, you can run it in the terminal:
206206
As you might expect, this command will do nothing as you didn't write any logic
207207
yet. Add your own logic inside the ``__invoke()`` method.
208208

209+
Command Aliases
210+
~~~~~~~~~~~~~~~
211+
212+
You can define alternative names (aliases) for a command directly in its name
213+
using a pipe (``|``) separator. The first name in the list becomes the actual
214+
command name; the others are aliases that can also be used to run the command::
215+
216+
// src/Command/CreateUserCommand.php
217+
namespace App\Command;
218+
219+
use Symfony\Component\Console\Attribute\AsCommand;
220+
use Symfony\Component\Console\Command\Command;
221+
222+
#[AsCommand(
223+
name: 'app:create-user|app:add-user|app:new-user',
224+
description: 'Creates a new user.',
225+
)]
226+
class CreateUserCommand extends Command
227+
{
228+
// ...
229+
}
230+
231+
.. versionadded:: 7.4
232+
233+
The ability to define aliases through the command name was introduced in
234+
Symfony 7.4.
235+
209236
Console Output
210237
--------------
211238

console/hide_commands.rst

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,28 @@ the ``hidden`` property of the ``AsCommand`` attribute::
2222
// ...
2323
}
2424

25-
Hidden commands behave the same as normal commands but they are no longer displayed
26-
in command listings, so end-users are not aware of their existence.
25+
You can also define a command as hidden using the pipe (``|``) syntax in the
26+
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
43+
command name was introduced in Symfony 7.4.
44+
45+
Hidden commands behave the same as normal commands but they are no longer
46+
displayed in command listings, so end-users are not aware of their existence.
2747

2848
.. note::
2949

0 commit comments

Comments
 (0)