platformsh/console-form

A lightweight Symfony Console form system.

Maintainers

👁 pjcdawkins

Package info

github.com/platformsh/console-form

pkg:composer/platformsh/console-form

Statistics

Installs: 82 813

Dependents: 2

Suggesters: 0

Stars: 21

Open Issues: 0

v1.0.0-beta1 2024-11-25 23:14 UTC

Requires

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT e7d95f5b04cd0d5a522107ae292865ee52d6ba6c

  • Patrick Dawkins

README

A lightweight form system for Symfony Console commands.

Commands can define forms which can be used both via command-line options and via interactive input.

Example

<?php
namespace MyApplication;

use Platformsh\ConsoleForm\Field\EmailAddressField;
use Platformsh\ConsoleForm\Field\Field;
use Platformsh\ConsoleForm\Form;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MyCommand extends Command
{
 protected function configure()
 {
 $this->setName('my:command')
 ->setDescription('An example command');
 $this->form = Form::fromArray([
 'name' => new Field('Name', ['description' => 'Your full name']),
 'mail' => new EmailAddressField('Email', ['description' => 'Your email address']),
 ]);
 $this->form->configureInputDefinition($this->getDefinition());
 }

 protected function execute(InputInterface $input, OutputInterface $output)
 {
 $questionHelper = $this->getHelper('question');
 $result = $this->form->resolveOptions($input, $output, $questionHelper);

 $output->writeln("Your name: " . $result['name']);
 $output->writeln("Your email address: " . $result['mail']);
 }
}

Alternatives