PHP code example of phpixie / console

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

    

phpixie / console example snippets


namespace Project\App\Console;

class Greet extends \PHPixie\Console\Command\Implementation
{
    public function __construct($config)
    {
        // Specify command description
        $config->description('Greet the user');
        
        //Define a 'message' argument
        $config->argument('message')
            ->description("Message to display");
        
        parent::__construct($config);
    }
    
    /**
     * Gets called when the command is executed.
     * $argumentData and $optionData work in the same
     * way as HTTP $request->query() and $request->data()
     */
    public function run($argumentData, $optionData)
    {
        $message = $argumentData->get('message', "Have fun coding!");
        $this->writeLine($message);
    }
}

$config->option('user')

    //Mark option as  option does.
    //this is displayed by the 'help' command
    ->description("User to connect to the database with");
    
$config->option('skip-missing')
    ->description("Don't throw an error if the tables are missing")
    
    //mark option as flag,
    //flag options don't accept a value,
    //but are set to 'true' if they are present.
    ->flag();

$config->option('f')
    ->flag()
    ->description("Force database dump");

$config->argument('database')
    ->o dump the tables from");
    
$config->argument('tables')
    ->description("Tables to dump")
    
    // Can accept more than one value.
    // There can be only one argument marked with `arrayOf`
    // and it has to be the last one.
    ->arrayOf();

public function run($argumentData, $optionData)
{
    $database = $argumentData->get('database');
    
    // specifying default value
    $user = $optionData->get('user', 'phpixie');
}

public function run($argumentData, $optionData)
{
    // Write text without line break
    $this->write("Hello ");
    
    // Write text with new line
    $this->writeLine("World");
    
    // Read a line of user input
    $str = $this->readLine();
    
    // Throwing a CommandException will output the error message
    // and ensure that the command exits with a non-zero exit code
    throw new \PHPixie\Console\Exception\CommandException("Something bad happened");
}

public function run($argumentData, $optionData)
{
    $context = $this->cliContext();
    
    $inputStream = $cliContext->inputStream();
    $outputStream = $cliContext->outputStream();
    $errorStream = $cliContext->errorStream();
    
    $outputStream->write("Hello");
    $errorStream->writeLine("Something bad happened");
    $context->setExitCode(1); // set the exit code
}

if ./console app:somecommand ; then
    echo "Command succeeded"
else
    echo "Command failed"
fi