codeinc/console

此包已被弃用,不再维护。没有建议的替代包。

Code Inc. 的命令行界面库

1.2.0 2018-03-15 08:56 UTC

This package is auto-updated.

Last update: 2020-01-24 20:42:33 UTC


README

此 PHP 7 库提供了各种工具,通过控制台与用户交互。

用法

Console

Console 类允许您通过控制台与用户交互。

<?php
use CodeInc\Console\Console;

// the parameter specifies if the class should throw exceptions
$console = new Console(true);

/*
 * Asks a Yes / No question and returns a boolean
 */
// returns true or false
$console->askBool("Do you like chocolate?"); 

/*
 * Asks a questions expecting a string as an answer
 */
// returns the response or null if no response is provided
echo $console->askString("What is your city?"); 

// returns the response or throws an exception if no response is provided
echo $console->askString("What is your city?", false); 

/*
 * Asks a questions with a closed list of anwsers
 */
$colors = ["green", "red", "blue", "purple", "orange", "yellow"];

// returns chosen color or throws an exception if no response is provided
echo $console->askOptions("What is your favorite color?", $colors); 

// returns chosen color or null if no response is provided
echo $console->askOptions("What is your favorite color?", $colors, true); 

CommandLine

CommandLine 类提供简单的实用静态方法

<?php
use CodeInc\Console\CommandLine;

// returns true if the script is running in CLI mode
CommandLine::isCLI(); 

// returns true if the current user is 'root'
CommandLine::isRoot(); 

// returns true if the current user is 'username'
CommandLine::isUser("username"); 

// returns the current user name or null is the user is unknow.
CommandLine::getUser();

// thow an exception if not in CLI mode
CommandLine::requireCLI(); 

// thow an exception if the current user is not 'root'
CommandLine::requireRoot(); 

// thow an exception if the current user is not 'username'
CommandLine::requireUser("username"); 

Arguments

Arguments 类旨在帮助访问脚本参数。

<?php
use CodeInc\Console\Arguments;

// for the request 'myScript.php --param1=val1 --param2 -aDG'
$arguments = new Arguments();
$arguments->hasParameter("a"); // returns true
$arguments->hasParameter("D"); // returns true
$arguments->hasParameter("G"); // returns true
$arguments->hasParameter("z"); // returns false
$arguments->getArgumentValue("param1"); // returns 'val1'
$arguments->getArgumentValue("param2"); // returns null
$arguments->getArgumentValue("param3"); // returns false
$arguments->hasArgument("param2"); // returns true

安装

此库可通过 Packagist 获取,并使用 Composer 安装。

composer require codeinc/console

许可

该库采用 MIT 许可证发布(见 LICENSE 文件)。