Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add support for Hidden Options in Console #54206

Open
@Luc45

Description

@Luc45

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 command app: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 the addOption Method:

Add an additional argument to the addOption 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:

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;

class SendCommand extends Command
{
    protected function configure()
    {
        $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 runs app:send --help, the output will not include the --notify option, as it's marked hidden. However, the option will still function if used explicitly with app: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.

protected function execute(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
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions