2017 © Pedro Peláez
 

library console

Wrapper for the symfony/console package

image

davek1312/console

Wrapper for the symfony/console package

  • Sunday, April 2, 2017
  • by davek1312
  • Repository
  • 0 Watchers
  • 0 Stars
  • 21 Installations
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 17 Versions
  • 0 % Grown

The README.md

davek1312/console

Wrapper for the symfony/console package., (*1)

Installation

The package is available on Packagist, you can install it using Composer., (*2)

composer require davek1312/console

Creating Commands

Create your command class and implement process():, (*3)

<?php

namespace MyApp\Commands;

use Davek1312\Console\Command;

class MyCommand extends Command {

    protected $signature = 'myapp:mycommand {argument} {--option=}';
    protected $description = 'My command\'s short description';
    protected $help = 'My command\'s full description';

    protected function process() {
        // MyCommand's code logic
    }
}

Command Inputs

The signature property defines the command's arguments and options., (*4)

   /**
    * This command expects one argument for name and with option with a value for age.
    */
    protected $signature = 'myapp:mycommand {name} {--age=}';

Arguments

    // Required argument
    myapp:mycommand {name}
    Console useage: vendor/bin/davek1312-console myapp:mycommand David

    // Argument with a description
    myapp:mycommand {name : Name's description}

    // Optional argument
    myapp:mycommand {name?}
    Console useage: vendor/bin/davek1312-console myapp:mycommand

    // Argument with a default value
    myapp:mycommand {name=David}

    // Argument where the value is an array
    myapp:mycommand {name*}
    Console useage: vendor/bin/davek1312-console myapp:mycommand David Paul

    // Optional argument where the value is an array
    myapp:mycommand {name?*}

Options

    // Option where no value is expected. The value is when not present and true when present.
    myapp:mycommand {--age}
    Console useage: vendor/bin/davek1312-console myapp:mycommand --age

    // Option with a description.
    myapp:mycommand {--age : description}

    // Option with a command line shortcut
    myapp:mycommand {--A|age}
    Console useage: vendor/bin/davek1312-console myapp:mycommand --A=20

    // Option where a value is expected
    myapp:mycommand {--age=}
    Console useage: vendor/bin/davek1312-console myapp:mycommand --age=20    

    // Option with a default value
    myapp:mycommand {--age=100}

    // Option where the value is an array
    myapp:mycommand {--age=*}
    Console useage: vendor/bin/davek1312-console myapp:mycommand --age=20 --age=30

Registering Commands

To register your commands view the davek1312\app documentation., (*5)

Calling Commands

Command Line

vendor/bin/davek1312-console myapp:mycommand argument-value --option=option-value

Programmatically

Running call in both examples returns an integer return code., (*6)

From Another Command

$this->call('myapp:mycommand', [
    'argument' => 'argument-value', 
    '--option' => 'option-value'
]);

From Anywhere Else In Your Application

Davek1312\Console\Application::call('myapp:mycommand', [
    'argument' => 'argument-value', 
    '--option' => 'option-value'
]);

Console Input/Output

Prompting For Input

There a few ways to ask the user for input:, (*7)

// Ask the user for confirmation
$confirm = $this->inputConfirm('Do you want to continue? [y/n]');

// Ask the user for a value
$name = $this->inputAsk('What is your name?', $default);

// Ask the user for a value from a list of predefined values
$name = $this->inputAskChoice('What is your name?', ['David', 'Paul']);

// Ask the user for a value and their response will be autocompleted from the list of values provided
$name = $this->inputAskAutocomplete('What is your name?', ['David', 'Paul']);

// Ask the user for a value but hide their response as they typre
$password = $this->inputAskHidden('What is your password?');

Writing Output

To write to the console output you can use the following methods:, (*8)

// "Message" is displayed in default style
$this->outputLine('Message');

// "Message" is displayed in green text
$this->outputInfo('Message');

// "Message" is displayed in yellow text
$this->outputComment('Message');

// "Message" is displayed with text in a blue background
$this->outputQuestion('Message');

// "Message" is displayed with text in a red background
$this->outputError('Message');

Progress Bar

To display the progress of the command you can supply the ProgressBar with a count:, (*9)

$count = 3;
$progressBar = $this->getProgressBar($count);
for($i = 0; $i < $count; $i++) {
    //Do something
    $progressBar->advance();
}
$progressBar->finish();

Testing Commands

You can execute and obtain the console output of commands using the following code:, (*10)

<?php

use Davek1312\Console\Tests\CommandTest;

$command = CommandTest::getCommand(MyCommand::class,'myapp:mycommand');
/**
 * $argumentsAndOptions are the commands defined arguments and options
 * $inputs is an array of booleans that answer i/o inputs
 */
CommandTest::getCommandOutput($command, $argumentsAndOptions, $inputs);

The Versions

02/04 2017

dev-master

9999999-dev

Wrapper for the symfony/console package

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Kelly

command console

02/04 2017

v0.8.1

0.8.1.0

Wrapper for the symfony/console package

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Kelly

command console

29/03 2017

v0.7.3

0.7.3.0

Wrapper for the symfony/console package

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Kelly

command console

29/03 2017

v0.7.2

0.7.2.0

Wrapper for the symfony/console package

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Kelly

command console

26/03 2017

v0.7.1

0.7.1.0

Wrapper for the symfony/console package

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Kelly

command console

26/03 2017

v0.6.5

0.6.5.0

Wrapper for the symfony/console package

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Kelly

command console

26/03 2017

v0.6.4

0.6.4.0

Wrapper for the symfony/console package

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Kelly

command console

25/03 2017

v0.6.3

0.6.3.0

Wrapper for the symfony/console package

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Kelly

command console

19/03 2017

v0.6.2

0.6.2.0

Wrapper for the symfony/console package

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Kelly

command console

19/03 2017

v0.6.1

0.6.1.0

Wrapper for the symfony/console package

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Kelly

command console

19/03 2017

v0.5.1

0.5.1.0

Wrapper for the symfony/console package

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Kelly

command console

18/03 2017

v0.4.1

0.4.1.0

Wrapper for the symfony/console package

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Kelly

command console

17/03 2017

v0.3.1

0.3.1.0

Wrapper for the symfony/console package

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Kelly

command console

17/03 2017

v0.2.1

0.2.1.0

Wrapper for the symfony/console package

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Kelly

command console

05/03 2017

v0.1.2

0.1.2.0

Wrapper for the symfony/console package

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Kelly

command console

05/03 2017

v0.1.1

0.1.1.0

Wrapper for the symfony/console package

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Kelly

command console

05/03 2017

v0.0.1

0.0.1.0

Wrapper for the symfony/console package

  Sources   Download

MIT

The Requires

 

The Development Requires

by David Kelly

command console