Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.6k
Description
Description
Sometimes, we want to deprecate an option but not remove it immediately as to not break CI integration.
It would be ideal to be able to add a "hidden" option that doesn't show up on--help
, but still works if you use it.
Example
Suppose we have a commandapp:send
with an existing option--notify
. We plan to deprecate--notify
but want to maintain it for backward compatibility. The hidden feature could be implemented as follows:
Modifying theaddOption
Method:
Add an additional argument to theaddOption
method in Symfony Console. This argument,isHidden
, is a boolean indicating whether the option should be hidden from the--help
output.
Here's an example of defining the--notify
option as a hidden option:
useSymfony\Component\Console\Command\Command;useSymfony\Component\Console\Input\InputOption;class SendCommandextends Command{protectedfunctionconfigure() {$this ->setName('app:send')// other options and configuration ->addOption('notify',null, InputOption::VALUE_NONE,'Send a notification upon completion.',false,true// New argument 'isHidden' set to true ); }}
Behavior with the --help Option:
When a user runsapp:send --help
, the output will not include the--notify
option, as it's marked hidden. However, the option will still function if used explicitly withapp:send --notify
.
Use case: Deprecating options
The developer can use the option and warn the user about it's deprecation, without breaking backwards compatibility immediately. This is just one example use case.
protectedfunctionexecute(InputInterface$input,OutputInterface$output){if ($input->getOption('notify')) {$output->writeln('<comment>Warning: The --notify option is deprecated and will be removed in a future version.</comment>');// Execute notify functionality }// Rest of the command execution}