1. Go to this page and download the library: Download simsoft/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/ */
use Symfony\Component\Console\Input\InputArgument;
Application::command('example:closure:command2', function() {
$name = $this->argument('name');
$this->info("I got your name: $name");
})
->purpose('Get user name')
->input(function(){
$this->addArgument('name', InputArgument::REQUIRED, 'Name
namespace App;
use Simsoft\Console\Command;
class HelloWorldCommand extends Command
{
static string $name = 'screen:welcome';
static string $description = 'Hi Guest';
protected bool $lockable = true; // Enable lock. Default: false.
protected function init(): void
{
$this
// Add arguments
->addArgument('name', InputArgument::REQUIRED, 'Who do you want to greet?')
->addArgument('last_name', InputArgument::OPTIONAL, 'Your last name?')
// Add option
->addOption(
'iterations',
'i',
InputOption::VALUE_REQUIRED,
'How many times should the message be printed?',
1
)
;
}
protected function handle(): void
{
// get arguments and options.
$name = $this->argument('name');
$lastName = $this->argument('last_name');
$iterations = $this->option('iterations');
$arguments = $this->arguments();
// $arguments contains ['name' => '..user input.. ', 'last_name' => '...']
$options = $this->options();
// $options contains ['iterations' => '..user input.. ']
for($i = 0; $i < $iterations; ++$i) {
$this->info('Hi');
}
$this->error('Display error message');
}
}
/**
* @throws \Throwable
*/
protected function handle(): void
{
$this->info('Hello World'); // Write info message
$this->comment('Comment text');
$this->question('My Question?');
$this->error('Warning'); // Write error message
$this->line('Simple line'); // Write simple un-formatted message
$this->errorBlock('Block Header', 'Block message');
$this->newLine(); // Write a single blank line
$this->newLine(3); // Write three blank lines
}
/**
* @throws \Throwable
*/
protected function handle(): void
{
// Simple question
// Other argument: $default.
$name = $this->ask('What is your name?');
$this->info("Your name is $name");
// Hide user input question
// Other argument: $default
$secret = $this->secret('Please tell me a secret?');
$this->info("Your secret is $secret");
// Asking for confirmation.
// Other argument: $default.
if ($this->confirm('Are you above 18yo (y/n)?')) {
$this->info('You have grow up!');
} else {
$this->info("You are very young!");
}
// Multiple choice questions.
// Other arguments: $defaultIndex, $maxAttempts, $allowMultipleSelections.
$myChoice = $this->choice('Which color do you like?', ['Yellow', 'Orange', 'Blue']);
$this->info("You have selected $myChoice");
}