/** * Test a cli run with named arguments. * * @throws Exception */ public function testArgNames() { $cli = new Cli(); $cli->description('A cli with named args.')->arg('from', 'The path from.')->arg('to', 'The path to.'); $args = $cli->parse(['script', '/var/foo.txt', '/var/bar.txt'], false); $this->assertSame('/var/foo.txt', $args->getArg('from')); $this->assertSame('/var/bar.txt', $args->getArg('to')); $this->assertSame('/var/foo.txt', $args->getArg(0)); $this->assertSame('/var/bar.txt', $args->getArg(1)); }
public function run() { if (count($this->argv) == 1) { $this->help(); return false; } $this->init(); $cli = new Cli(); $command = strtolower($this->argv[1]); array_splice($this->argv, 1, 1); switch ($command) { case 'bump': $this->argv[0] .= ' bump'; $cli->description('Bump version')->opt('patch:p', 'Bump a patch version.', false, 'bool')->opt('minor:m', 'Bump a minor version.', false, 'bool')->opt('major:M', 'Bump a major version.', false, 'bool')->opt('check:c', 'Checks stuff', false, 'bool')->opt('version:v', 'Force set version'); if (count($this->argv) == 1) { $this->help($cli); return false; } return $this->bumbCommand($cli->parse($this->argv, false)); case 'merge_into': $this->argv[0] .= ' merge_into'; $cli->description('Merge current branch into given branch')->arg('target', 'Branch to merge into', true)->opt('paranoid:P', 'Be extra cautious.', false, 'bool'); if (count($this->argv) == 1) { $this->help($cli); return false; } return $this->mergeIntoCommand($cli->parse($this->argv, false)); case 'merge_from': $this->argv[0] .= ' merge_from'; $cli->description('Merge given branch into current branch')->arg('target', 'Branch to merge from', true)->opt('paranoid:P', 'Be extra cautious.', false, 'bool'); if (count($this->argv) == 1) { $this->help($cli); return false; } return $this->mergeFromCommand($cli->parse($this->argv, false)); default: $this->help(); return false; } }
/** * Get the basic cli example. * * @return Cli */ public function getBasicCli() { // Define the cli options. $cli = new Cli(); $cli->description('Dump some information from your database.')->opt('host:h', 'Connect to host.', false, 'string')->opt('port:P', 'Port number to use.', false, 'integer')->opt('user:u', 'User for login if not current user.', true, 'string')->opt('password:p', 'Password to use when connecting to server.', false, 'string')->opt('database:d', 'The name of the database to dump.', true, 'string'); return $cli; }
//E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR); ini_set('display_errors', 'on'); ini_set('track_errors', 1); date_default_timezone_set('America/Montreal'); $paths = [__DIR__ . '/../vendor/autoload.php', __DIR__ . '/../../../autoload.php']; foreach ($paths as $path) { if (file_exists($path)) { require_once $path; break; } } use Garden\Cli\Cli; use Garden\Cli\Schema; use Mustacher\Mustacher; $cli = new Cli(); $cli->description('Run mustache templates against a JSON file.')->opt('template:t', 'The path to the template file.', true)->opt('input:i', 'The path to the input JSON data file.')->opt('output:o', 'The path where the output will be written.')->opt('format:f', 'The format of the template file. Either mustache or message.')->opt('data:d', 'A JSON formatted data object. This will be merged on top of the input file if both are specified.'); $args = $cli->parse($argv); try { $data = Mustacher::mergeData($args->getOpt('input'), $args->getOpt('data')); $str = Mustacher::generateFile($args->getOpt('template'), $data, $args->getOpt('format', Mustacher::FORMAT_MUSTACHE)); } catch (Exception $ex) { echo $cli->red($ex->getMessage() . "\n"); die; } if ($args->getOpt('output')) { $path = $args->getOpt('output'); if (!file_exists(dirname($path))) { mkdir($path, 0777, true); } echo "Writing output file: {$path}\n"; $r = file_put_contents($path, $str);