1. Go to this page and download the library: Download vanilla/garden-cli 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/ */
vanilla / garden-cli example snippets
// All of the command line classes are in the Garden\Cli namespace.
use Garden\Cli\Cli;
// Require composer's autoloader.
, 'Connect to host.', true)
->opt('port:P', 'Port number to use.', false, 'integer')
->opt('user:u', 'User for login if not current user.', true)
->opt('password:p', 'Password to use when connecting to server.')
->opt('database:d', 'The name of the database to dump.', true);
// Parse and return cli args.
$args = $cli->parse($argv, true);
$args = $cli->parse($argv);
$host = $args->getOpt('host', '127.0.0.1'); // get host with default 127.0.0.1
$user = $args->getOpt('user'); // get user
$database = $args['database']; // use the args like an array too
$port = $args->getOpt('port', 123); // get port with default 123
// Define a cli with commands.
$cli = Cli::create()
// Define the first command: push.
->command('push')
->description('Push data to a remote server.')
->opt('force:f', 'Force an overwrite.', false, 'boolean')
->opt('set-upstream:u', 'Add a reference to the upstream repo.', false, 'boolean')
// Define the second command: pull.
->command('pull')
->description('Pull data from a remote server.')
->opt('commit', 'Perform the merge and commit the result.', false, 'boolean')
// Set some global options.
->command('*')
->opt('verbose:v', 'Output verbose information.', false, 'integer')
->arg('repo', 'The repository to sync with.', true);
$args = $cli->parse($argv);
class App extends Garden\Cli\Application\CliApplication {
protected function configureCli(): void {
parent::configureCli();
// Add methods with addMethod().
$this->addMethod('SomeClassName', 'someMethod');
$this->addMethod('SomeClassName', 'someOtherMethod', [CliApplication::OPT_SETTERS => false]);
$this->addMethod('SomeOtherClassName', 'someMethod', [CliApplication::OPT_COMMAND => 'command-name']);
// Add command classes with addCommandClass().
$this->addCommandClass('ExampleCommand', 'run');
// Add ad-hoc closures with addCallable().
$this->addCallable('foo', function (int $count) { });
// Wire up dependencies with addConstructor() or addFactory().
$this->addFactory(\PDO::class, [\Garden\Cli\Utility\DbUtils::class, 'createMySQL']);
}
protected function configureContainer(Container $container): void {
parent::configureContainer($container);
// Configure the container here.
}
}
class App extends Garden\Cli\Application\CliApplication {
protected function configureCli(): void {
parent::configureCli();
// This will make the database connection get created by the DbUtils::createMySQL() method with command line opts for the same.
$this->addFactory(\PDO::class, [\Garden\Cli\Utility\DbUtils::class, 'createMySQL']);
$this->getContainer()->setShared(true);
// This will wire up the constructor parameters for the the StreamLogger to the command line and set is as the logger for the app.
$this->addConstructor(\Garden\Cli\StreamLogger::class);
$this->getContainer()->setShared(true);
$this->getContainer()->rule(\Psr\Log\LoggerInterface::class)->setAliasOf(\Garden\Cli\StreamLogger::class);
}
}
class App extends Garden\Cli\Application\CliApplication {
protected function configureCli(): void {
parent::configureCli();
// Wire up your github client's API key to the command line.
$this->addCall(GithubClient::class, 'setAPIKey', [\Garden\Cli\Application\CliApplication::OPT_PREFIX => 'git-']);
}
}
$app = new App();
$app->main($argv);
$log = new TaskLogger();
$log->info('This is a message.');
$log->error('This is an error.'); // outputs in red
$log->beginInfo('Begin a task');
// code task code goes here...
$log->end('done.');
$log->beginDebug('Make an API call');
$log->endHttpStatus(200); // treated as error or success depending on code
$log->begin(LogLevel::NOTICE, 'Multi-step task');
$log->info('Step 1');
$log->info('Step 2');
$log->beginDebug('Step 3');
$log->debug('Step 3.1'); // steps will be hidden because they are level 3
$log->debug('Step 3.2');
$log->end('done.');
$log->end('done.');