jabernardo / console.php
PHP 命令行应用程序骨架
0.1.1
2018-07-07 07:59 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2024-09-29 04:43:17 UTC
README
console.php
是 PHP 命令行应用程序的骨架。
#!/usr/bin/php <?php // Require autoloader for console.php require("../src/autoload.php"); // Sample Command Implementation using \Console\Command Interface // ./index.php hello name:"Your Name" age:22 class HelloCommand implements \Console\Command { function __invoke(\Console\Input $i, \Console\Output $o) { if (!$i->hasOptions(['name', 'age'])) { $o->writeln('Invalid args.'); return; } $o->writeln('Hello %s! So you are %d years old.', $i->getOption('name'), $i->getOption('age')); } } // Instantiate a new console application $app = new \Console\Application(); // Register our test HelloCommand to our console application $app->add('hello', new HelloCommand()); // You can also just declare a command by using callables $app->add('help', function($i, $o) { $o->writeln('This is a sample application.'); }, true); // <-- `true` is to enable this as the default command // Finally, App and Running! $app->run();
输入
hasFlag :boolean
检查是否在应用程序中传递了标志。
<?php class HelloCommand implements \Console\Command { function __invoke(\Console\Input $i, \Console\Output $o) { // Check if flag exists if ($i->hasFlag('q')) { // Do something } } }
hasOption :boolean
检查是否在应用程序中传递了选项。
<?php class HelloCommand implements \Console\Command { function __invoke(\Console\Input $i, \Console\Output $o) { // Check if option exists if ($i->hasOption('max')) { // Do something } } }
hasOptions :boolean
检查是否在应用程序中传递了选项。
<?php class HelloCommand implements \Console\Command { function __invoke(\Console\Input $i, \Console\Output $o) { // Check if option exists if ($i->hasOptions(['age', 'name'])) { // Do something } } }
getOption :mixed
返回传递的选项的值。
<?php class HelloCommand implements \Console\Command { private $max = 0; function __invoke(\Console\Input $i, \Console\Output $o) { // Check if option exists if ($i->hasOption('max')) { $this->max = $i->getOption('max'); } } }
getOptions :array
返回所有选项。
<?php class HelloCommand implements \Console\Command { function __invoke(\Console\Input $i, \Console\Output $o) { $options = $i->getOptions(); } }
setOptionDelimeter :void
设置选项分隔符(默认值为 :
)
<?php class HelloCommand implements \Console\Command { function __invoke(\Console\Input $i, \Console\Output $o) { $i->setOptionDelimeter('='); $options = $i->getOptions(); } }
getOptionDelimeter :string
获取选项分隔符(默认值为 :
)
getParameters :array
返回所有参数。
<?php class HelloCommand implements \Console\Command { function __invoke(\Console\Input $i, \Console\Output $o) { $params = $i->getParameters(); } }
输出
write :void
写入字符串缓冲区。
<?php class HelloCommand implements \Console\Command { function __invoke(\Console\Input $i, \Console\Output $o) { $o->write('Hello %s!', $i->getOption('name')); } }
writeln :void
写入一行字符串。
<?php class HelloCommand implements \Console\Command { function __invoke(\Console\Input $i, \Console\Output $o) { $o->write('Hello %s! So you\'re %d years old.', $i->getOption('name'), $i->getOption('age')); } }
许可
console.php
是开源软件,根据 MIT 许可 许可。