New in Symfony 5.3: Negatable Command Options

Contributed by Greg Anderson and Jérémy Derussé in #39642.

In some console commands it’s common to define two related options with opposite behaviors. For example, the default options applied to all Symfony commands include the --ansi and --no-ansi options: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17// ... use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputOption;

class SomeCommand extends Command { // ...

protected function configure(): void
{
    $this
        // ...
        ->addOption('ansi', null, InputOption::VALUE_NONE, 'Force ANSI output')
        ->addOption('no-ansi', null, InputOption::VALUE_NONE, 'Disable ANSI output')
    ;
}

}

In Symfony 5.3 we’ve introduced negatable command options to simplify these commands. A single negatable option creates two options in the command, following the pattern --xxx and --no-xxx. In practice, the following is equivalent to the previous example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27// ... use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface;

class SomeCommand extends Command { // ...

protected function configure(): void
{
    $this
        // ...
        ->addOption('ansi', null, InputOption::VALUE_NEGATABLE, 'Force/disable ANSI output')
    ;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
    // if command is run as `command --ansi`, $useAnsi = true
    // if command is run as `command --no-ansi`, $useAnsi = false
    $useAnsi = $input->getOption('ansi');

    // ...
}

}

Negatable options are only available for options that don’t allow passing any value to them (their previous type should be InputOption::VALUE_NONE).

                Sponsor the Symfony project.

http://feedproxy.google.com/~r/symfony/blog/~3/cgXbi8YPbsY/new-in-symfony-5-3-negatable-command-options

Létrehozva 4y | 2021. ápr. 27. 7:20:09


Jelentkezéshez jelentkezzen be

EGYÉB POSTS Ebben a csoportban

Symfony 7.3.0-BETA2 released

Symfony 7.3.0-BETA2 has just been released. This is a pre-release version of Symfony 7.3. If you want to test it in your own applications before its final release, run the following commands:

2025. máj. 10. 13:10:15 | Symfony
New in Symfony 7.3: Mailer Security Improvements

The Symfony Mailer component provides many security-related features like signing and encrypting email messages. In Symfony 7.3 we're pushing those features even further to give you greater control an

2025. máj. 9. 9:20:10 | Symfony
SymfonyOnline June 2025: Detect Hidden Defects: Check Your PHP Tests

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

2025. máj. 8. 14:40:19 | Symfony
New in Symfony 7.3: JsonPath Component

Contributed by Alexandre Daubois in

2025. máj. 8. 10:10:10 | Symfony
SymfonyOnline June 2025: Combining Turbo, LiveComponent & Stimulus... the Right Way?

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

2025. máj. 7. 13:10:15 | Symfony
New in Symfony 7.3: New and Improved Console Helpers

Symfony 7.3 introduces powerful improvements to the Console component, beyond the much-anticipated invokable commands and input attributes. This version also brings new helpers and features designed t

2025. máj. 7. 8:30:04 | Symfony
New in Symfony 7.3: Static Error Pages

Contributed by Loick Piera in

2025. máj. 6. 9:20:10 | Symfony