dealnews / console
创建控制台应用程序的实用工具
0.2.1
2018-02-22 01:02 UTC
Requires
- php: >=5.5.0
- ext-date: *
This package is auto-updated.
Last update: 2024-09-11 03:50:05 UTC
README
命令行参数
#!/bin/env php <?php /** * This is a test app called test.php */ use \DealNews\Console\Console; $console = new Console( array( "copyright" => array( "owner" => "DealNews.com, Inc.", "year" => "1997-".date("Y") ), "help" => array( "header" => "This is a test app" ) ), array( "s" => array( "description" => "Some option called -s", "param" => "SPARAM", "optional" => Console::ONE_REQUIRED ), "e" => array( "description" => "Some option called -e", "param" => "EPARAM", "optional" => Console::ONE_REQUIRED ), "long" => array( "description" => "Some long option called --long", "param" => "LONG", "optional" => Console::OPTIONAL ), ) ); $console->run(); ?>
您免费获得 -h
!示例帮助输出
$ php test.php -h This is a test app USAGE: test.php -h [-e EPARAM | -s SPARAM] [--long LONG] [-q] [-v] OPTIONS: -e EPARAM Some option called -e -h Shows this help --long LONG Some long option called --long -q Be quiet. Will override -v -s SPARAM Some option called -s -v Be verbose. Additional v will increase verbosity. e.g. -vvv Copyright DealNews.com, Inc. 1997-2015
详细程度
您还可以免费获得 -v
和 -q
!
<?php if($console->verbosity >= Console::VERBOSITY_VERBOSE){ // Write stuff out } // Or use the console's write method and let it decide $console->write("Normal output"); $console->write("Normal output", Console::VERBOSITY_NORMAL); $console->write("Verbose output", Console::VERBOSITY_VERBOSE); $console->write("Info output", Console::VERBOSITY_INFO); $console->write("Debug output", Console::VERBOSITY_DEBUG); ?>
如果命令行上设置了 -q
,则详细程度将设置为静默,且 write()
不会输出任何内容。
详细程度由命令行上出现的 -v
的数量控制。
详细程度设置还控制着当 Console 类遇到错误时将抛出哪种级别的错误消息。
检查选项
<?php // You can use two methods to check options $eValue = $console->getOpt("e"); if($eValue){ // do stuff for e } // or simply use the object if($console->long){ // do stuff for long } ?>
必需与可选
命令行选项可以对可选参数有三种不同的设置。
- 可选 - 完全可选的参数
- 必需 - 必须始终存在
- 一个必需 - 至少必须提供这种类型的一个选项
PID 文件管理
<?php // check there is no other PID running this script. By default, the // command line arguments are used to build a unique PID file name. // You can disable this by passing false. You can also, pass in a // second parameter that is a unique id for this script that will // be used to create the pid file name. $status = $console->checkPid(); if($status !== Console::PID_OK){ /** * We could do more here for the other statuses as well. * PID_OTHER_RUNNING * PID_OTHER_NOT_RUNNING * PID_OTHER_UNKNOWN */ fputs(STDERR, "A PID file already exists for this script"); exit(1); } ?>
用户反馈
进度条
<?php // Show the status of some progress // Includes elapsed time and estimated time to completion use \DealNews\Console\Status; $total = 100000; for ($x=1; $x<=$total; $x++){ Status::progress($x, $total); usleep(100); } ?>
示例输出
[======> 23% ] 23k/100k ET: 15 sec ETC: 50 sec
旋转器
<?php // Unsure how long something will run? Just show a spinner use \DealNews\Console\Status; $total = rand(10000, 30000); for ($x=1; $x<=$total; $x++) { Status::spinner(); usleep(100); } // clear the line after the spinner is done Status::clearLine(); ?>
用户输入
检查交互性
<?php use \DealNews\Console\Interact; if (Interact::isInteractive()) { // do stuff on the console like show status and ask questions } ?>
请求输入
您可以请求用户输入内容。如果为第二个参数传递 true,则用户的输入将不会在终端中回显。
<?php use \DealNews\Console\Interact; $answer = Interact::prompt("What is the secret word?", true); if ($answer == "secret") { $console->write("Yay! You got it!"); } else { $console->write("Boo! That is wrong!"); } ?>
询问是或否问题
<?php use \DealNews\Console\Interact; if (!Interact::confirm("Do you want to continue?")) { exit(0); } ?>