2017-25 © Pedro Peláez
 

library simple-console

One file console framework to help you write build scripts.

image

asika/simple-console

One file console framework to help you write build scripts.

  • Thursday, March 8, 2018
  • by asika32764
  • Repository
  • 1 Watchers
  • 14 Stars
  • 8,322 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 5 Versions
  • 177 % Grown

The README.md

PHP Simple Console

Single file console framework to help you write build scripts., (*1)

Installation

Use composer:, (*2)

``` bash composer require asika/simple-console, (*3)


Download single file: [Download Here](https://raw.githubusercontent.com/asika32764/php-simple-console/master/src/Console.php) CLI quick download: ```shell # WGET wget https://raw.githubusercontent.com/asika32764/php-simple-console/master/src/Console.php chmod +x Console.php # CURL curl https://raw.githubusercontent.com/asika32764/php-simple-console/master/src/Console.php -o Console.php chmod +x Console.php

Getting Started

Use closure, (*4)

``` php, (*5)

!/bin/sh php

<?php // Include single file include_once DIR . '/Console.php';, (*6)

// Or use composer include_once DIR . '/vendor/autolod.php';, (*7)

$app = new \Asika\SimpleConsole\Console;, (*8)

// Use closure $app->execute(function (\Asika\SimpleConsole\Console $app) { // PHP 5.3 $app->out('Hello');, (*9)

// PHP 5.4 or higher use $this
$this->out('Hello');

// Return TRUE will auto convert to 0 exitcode.
return true;

});, (*10)


Or Create your own class. ``` php class Build extends \Asika\SimpleConsole\Console { protected $help = <<<HELP [Usage] php build.php <version> [Options] h | help Show help information v Show more debug information. HELP; protected function doExecute () { $this->out('Hello'); // Return TRUE will auto convert to 0 exitcode. return true; } } $app = new Build; $app->execute();

Show HELP

Add -h or --help to show usage, you can add custom usage to $this->help, or override $this->getHelp()., (*11)

If you want to change h and help option, override $this->helpOptions = array('...')., (*12)

Handle Error

Just throw Exception in doExecute(), Console will auto catch error., (*13)

``` php throw new \RuntimeException('...');, (*14)


Add `-v` to show backtrace if error. ## Handle Wrong Arguments Wrong Argument use `\Asika\SimpleConsole\CommandArgsException` ``` php $arg = $this->getArgument(0); if (!$arg) { throw new \Asika\SimpleConsole\CommandArgsException('Please enter a name.'); }

Console will auto show help information., (*15)

``` bash [Warning] Please enter a name., (*16)

[Usage] console.php , (*17)

[Options] h | help Show help info. v Show more debug information., (*18)


## Multiple Commands Use `delegate()` to delegate to different methods. ``` php //... protected function doExecute() { return $this->delegate($this->getArgument(0)); } protected function foo() { $this->getArgument(1); // bar } protected function baz() { // ... }

Now you can add sub commands, (*19)

``` bash php console.php foo bar php console.php baz, (*20)


If you want to strip first argument after delgated, you can follow this code: ``` php $this->delegate(array_shift($this->args));

Now can use getArgument(0) in sub method and ignore the first command name., (*21)

The is another way:, (*22)

``` php $command = array_shift($this->args);, (*23)

$this->delegate($command, ...$this->args);, (*24)


``` php protected function foo($first, $second = null) { }

API

getArgument($order[, $default = null])

``` php $first = $this->getArgument(0, 'default value');, (*25)


### `setArgument($order, $$value)` ``` php $this->setArgument(1, 'value');

getOption($name: array|string[, $default = null])

Get option --foo, (*26)

``` php $this->getOption('foo');, (*27)


Get option `-f` or `--foo`, first match will return. ``` php $this->getOption(array('f', 'foo'));

NOTE: -abc will convert to a => 1, b => 1, c => 1 And -vvv will convert to v => 3, (*28)

setOption($name, $value)

Set otpion to toption list. $name also support array., (*29)

out($string[, $newline: bool = false])

Write to STDOUT,, (*30)

``` bash $this->out('Hello')->out('World');, (*31)


### `err($string[, $newline: bool = false])` Write to STDERR ``` bash $this->err('Hello')->err('World');

in($string[$default = null, $bool = false)

Ask a question, read from STDIN, (*32)

``` bash $un = $this->in('Please enter username: ', 'default_name');, (*33)


Read as boolean, add true to third argument: ``` bash $bool = $this->in('Are you sure? [Y/n]', [default true/false], true);
  • yes, y, 1, true will convert to TRUE
  • no, n, 0, false will convert to FALSE

exec($cmd)

A proxy to execute a cmd by exec() and return value., (*34)

It will add a title >> {your command} before exec so you will know what has been executed., (*35)

The Versions

08/03 2018

dev-master

9999999-dev

One file console framework to help you write build scripts.

  Sources   Download

MIT

by Simon Asika

08/03 2018

1.0.3

1.0.3.0

One file console framework to help you write build scripts.

  Sources   Download

MIT

by Simon Asika

17/08 2017

1.0.2

1.0.2.0

One file console framework to help you write build scripts.

  Sources   Download

MIT

by Simon Asika

04/03 2017

1.0.1

1.0.1.0

One file console framework to help you write build scripts.

  Sources   Download

MIT

by Simon Asika

04/03 2017

1.0.0

1.0.0.0

One file console framework to help you write build scripts.

  Sources   Download

MIT

by Simon Asika