PHP code example of jabernardo / console.php

1. Go to this page and download the library: Download jabernardo/console.php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jabernardo / console.php example snippets



#!/usr/bin/php


// Require autoloader for console.php
erface
// ./index.php hello name:"Your Name" age:22
class HelloCommand implements \Console\Command {
    function __invoke(\Console\Input $i, \Console\Output $o) {
        if (!$i->hasOptions(['name', 'age'])) {
            $o->writeln('Invalid args.');
            return;
        }

        $o->writeln('Hello %s! So you are %d years old.', $i->getOption('name'), $i->getOption('age'));
    }
}

// Instantiate a new console application
$app = new \Console\Application();

// Register our test HelloCommand to our console application
$app->add('hello', new HelloCommand());

// You can also just declare a command by using callables
$app->add('help', function($i, $o) {
    $o->writeln('This is a sample application.');
}, true); // <-- `true` is to enable this as the default command

// Finally, App and Running!
$app->run();




class HelloCommand implements \Console\Command {
    function __invoke(\Console\Input $i, \Console\Output $o) {
        // Check if flag exists
        if ($i->hasFlag('q')) {
            // Do something
        }
    }
}




class HelloCommand implements \Console\Command {
    function __invoke(\Console\Input $i, \Console\Output $o) {
        // Check if option exists
        if ($i->hasOption('max')) {
            // Do something
        }
    }
}




class HelloCommand implements \Console\Command {
    function __invoke(\Console\Input $i, \Console\Output $o) {
        // Check if option exists
        if ($i->hasOptions(['age', 'name'])) {
            // Do something
        }
    }
}




class HelloCommand implements \Console\Command {
    private $max = 0;
    
    function __invoke(\Console\Input $i, \Console\Output $o) {
        // Check if option exists
        if ($i->hasOption('max')) {
            $this->max = $i->getOption('max');
        }
    }
}




class HelloCommand implements \Console\Command {
    function __invoke(\Console\Input $i, \Console\Output $o) {
        $options = $i->getOptions();
    }
}




class HelloCommand implements \Console\Command {
    function __invoke(\Console\Input $i, \Console\Output $o) {
        $i->setOptionDelimeter('=');
        $options = $i->getOptions();
    }
}




class HelloCommand implements \Console\Command {
    function __invoke(\Console\Input $i, \Console\Output $o) {
        $params = $i->getParameters();
    }
}




class HelloCommand implements \Console\Command {
    function __invoke(\Console\Input $i, \Console\Output $o) {
        $o->write('Hello %s!', $i->getOption('name'));
    }
}




class HelloCommand implements \Console\Command {
    function __invoke(\Console\Input $i, \Console\Output $o) {
        $o->write('Hello %s! So you\'re %d years old.', $i->getOption('name'), $i->getOption('age'));
    }
}