代码拉取完成,页面将自动刷新
同步操作将从 yg178/php-console 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
a php console application library.
input
, output
NOTICE
php >= 7
(recommended use)。edit composer.json
,at require
add
"inhere/console": "dev-master",
// "inhere/console": "dev-php5", // for php5
run: composer update
git clone https://git.oschina.net/inhere/php-console.git // git@osc
git clone https://github.com/inhere/php-console.git // github
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Console\Application;
$meta = [
'name' => 'My Console App',
'version' => '1.0.2',
];
$input = new Input;
$output = new Output;
$app = new Application($meta, $input, $output);
// add command routes
$app->command('demo', function (Input $in, Output $out) {
$cmd = $in->getCommand();
$out->info('hello, this is a test command: ' . $cmd);
});
// run
$app->run();
now, you can see:
example(in terminal):
$ examples/app home/useArg status=2 name=john arg0 -s=test --page=23 --id=154 -e dev -v vvv -d -rf --debug --test=false
NOTICE:
true
) value: on|yes|true
false
) value: off|no|false
get command info:
echo $input->getScript(); // 'examples/app'
echo $input->getCommand(); // 'home/useArg'
get parsed arguments:
var_dump($input->getArgs());
output:
array(3) {
'status' => string(1) "2"
'name' => string(4) "john"
[0] => string(4) "arg0"
}
get parsed options:
var_dump($input->getOpts());
output:
array(10) {
's' => string(4) "test"
'e' => string(3) "dev"
'v' => string(3) "vvv"
'd' => bool(true)
'r' => bool(true)
'f' => bool(true)
'page' => string(2) "23"
'id' => string(3) "154"
'debug' => bool(true)
'test' => bool(false)
}
more method:
// argument
$first = $input->getFirstArg(); // 'arg0'
$status = $input->get('status', 'default'); // '2'
// option
$page = $input->getOpt('page') // '23'
$debug = $input->boolOpt('debug') // True
$test = $input->boolOpt('test') // False
echo "Your name:";
$text = $input->read();
// in terminal
// Your name: simon
echo $text; // 'simon'
basic output:
public function write(mixed $messages = '', $nl = true, $quit = false)
$output->write('hello');
$output->title()
$output->section()
$output->panel()
$output->table()
$output->helpPanel()
in the class Inhere\Console\Utils\Interact
interactive method:
Interact::select()
(alias Interact::choice()
)Select one of the options
select($description, $options, $default = null, $allowExit=true)
choice($description, $options, $default = null, $allowExit=true)
only values, no setting option
$select = Interact::select('Your city is ?', [
'chengdu', 'beijing', 'shanghai'
]);
output in terminal:
Your city is ?
0) chengdu
1) beijing
2) shanghai
q) Quit // quit option. is auto add. can setting it by 4th argument.
You choice: 0
echo $select; // '0'
custom option, setting a default value.
$select = Interact::select('Your city is ?', [
'a' => 'chengdu',
'b' => 'beijing',
'c' => 'shanghai'
], 'a');
output in terminal:
Your city is?
a) chengdu
b) beijing
c) shanghai
q) Quit // quit option. is auto add. can setting it by 4th argument.
You choice[default:a] : b
echo $select; // 'b'
Interact::confirm()
public static function confirm($question, $default = true) bool
usage:
$result = Interact::confirm('Whether you want to continue ?');
output in terminal:
Whether you want to continue ?
Please confirm (yes|no) [default:yes]: n
result:
var_dump($result); // bool(false)
Interact::question()
/Interact::ask()
public static function ask($question, $default = null, \Closure $validator = null)
public static function question($question, $default = null, \Closure $validator = null)
$answer = Interact::ask('Please input your name?', null, function ($answer) {
if ( !preg_match('/\w+/', $answer) ) {
Interact::error('The name must match "/\w+/"');
return false;
}
return true;
});
MIT
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。