Skip to content

stof/console-form

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Console Form

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.

Build Status

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');
        $options = $this->form->resolveOptions($input, $output, $questionHelper);
        if ($options === false) {
            // Return an error code.
            return 1;
        }

        $output->writeln("You specified: " . print_r($options, true));

        return 0;
    }
}

Alternatives

About

Simple Symfony Console interactivity

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%