PHP code example of sumpygump / qi-console

1. Go to this page and download the library: Download sumpygump/qi-console 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/ */

    

sumpygump / qi-console example snippets




$terminal = new Qi_Console_Terminal();
// ...

array(
    'myscript.php',
    '-v',
    '--flag',
    '--param=value',
    'okay',
);

// Example rules with some help messages
$rules = array(
    'v' => 'Use verbose messaging',
    'o' => 'Another random option',
    'flag' => 'Flag something as special',
    'name|n:' => 'Provide a name to use',
    'arg:filename' => 'Filename to parse',
);

// Our example input
// php scriptname -v --flag --name "a name" filename.txt
$argv = array(
    'scriptname', // The first argument is always ignored by ArgV
    '-v',
    '--flag',
    '--name',
    '"a name"',
    'filename.txt',
);

$arguments = new Qi_Console_ArgV($argv, $rules);

// Now we can reference the following:
$arguments->v; // true
$arguments->o; // false
$arguments->flag; // true
$arguments->name; // equal to 'a name'
$arguments->filename; // equal to 'filename.txt'

class MyClient extends Qi_Console_Client
{
    public function init()
    {
        // Initialization logic
    }

    public function execute()
    {
        // Execute the main logic of this client
        // Use $this->_args (ArgV object) to handle input and react
        // Use $this->_displayWarning() to output warning message to user
        // etc.
    }
}

$arguments = new Qi_Console_ArgV($argv, $rules);
$terminal = new Qi_Console_Terminal();

$myClient = new MyClient($arguments, $terminal);
$myClient->execute();

$terminal = new Qi_Console_Terminal();
$exceptionHandler = new Qi_Console_ExceptionHandler($terminal);
$exceptionHandler->bindHandlers();

// Now any time an exception is thrown, it will output a pretty message
// with colors and exit.

// Receive input from STDIN
$input = Qi_Console_Std::in();

// Output to STDOUT
Qi_Console_Std::out('Some text');

// Output to STDERR
Qi_Console_Std::err('Error output');

// Define the table data
$tableData = array(
    array('John', '28', 'Green'),
    array('Hannah', '7', 'Violet'),
    array('Michael', '43', 'Red'),
);

// Define the headers for the columns
$headers = array(
    'Name',
    'Age',
    'Favorite Color',
);

// Define optional alignment for columns
$alignment = array(
    'L',
    'R',
    'L'
);

$tabular = new Qi_Console_Tabular(
    $tableData,
    array(
        'headers'   => $headers,
        'cellalign' => $alignment,
    )
);

$tabular->display();

$terminal = new Qi_Console_Terminal();

// Clear the screen
$terminal->clear();

$terminal->set_fgcolor(1);
echo "Text is now red.\n";

$terminal->set_fgcolor(2);
echo "Text is now green.\n";

$terminal->bold_type();
echo "Text is now bold.\n";

$terminal->center_text("This text is centered.");

// This is using the terminfo capability "Original Pair" to set the colors
// back to default.
$terminal->op();
echo "Now text is back to default color.\n";

// This is using the terminfo capability sgr0 which turns off all text
// attributes, meaning it is not bold anymore.
$terminal->sgr0();
echo "Now text is not bold anymore.\n";