duanejeffers/cli.class.php

用于在PHP中创建CLI程序的简单对象

dev-master 2015-04-25 13:08 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:24:41 UTC


README

用于命令行PHP应用程序的类

授权协议

版权所有 (c) 2012-2013,Duane Jeffers <github@duanejeffers.com>

以下是对任何获得本软件及其相关文档文件(以下简称“软件”)副本的个人,免费授予在此软件中不受限制地处理软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许向软件提供副本的个人这样做,前提是以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

本软件按“原样”提供,不提供任何形式的保证,明示或暗示,包括但不限于适销性、适用于特定目的和不侵犯版权的保证。在任何情况下,作者或版权所有者都不应对任何索赔、损害或其他责任负责,无论这些索赔、损害或其他责任是由于合同、侵权或其他方式引起的,无论是由于软件或其使用或与其他软件或其使用或其他方式有关。

用法

cli对象的目的在于为cli脚本/程序提供对常用函数的快速访问。

启动对象

// Starting the cli class is easy...
$cli = new cli();

// Printing with a carrage return
$cli->print_line('Welcome to cli.class.php');

// Dumping an object or array to the screen with a carrage return.
$array = array(
    0 => array(
      'maple',
      'blueberry'
	),
	1 => array(
	    'pancakes',
		'waffles'
	)
);

$cli->print_dump($array);

彩色输出

// The class has the default color options for the terminal. 
// Use the color_str static function to create the string.

$output = 'This is ';
$output .= cli::color_str('a ', cli::ColorFgLtRed);
$output .= cli::color_str('color ', cli::ColorFgBlue);
$output .= cli::color_str('string', cli::ColorFgBlack, cli::ColorBgLtGray);
$output .= cli::color_str('!!!', cli::ColorFgLtGreen);

$cli->print_line($output);

选项处理

// Options with the cli object are as simple as passing 
// an array through the constructor.
$cli = new cli(array(
    'email:' => 'Email Address', // option that requires an input
    'username::' => 'Username', // option that allows for an optional input
    'download' => 'Download switch' // option that acts as a bool switch
));

// Now, when the user calls 
// <program_name> --email=someemail@test.com --download --username
// This will auto-populate an options array, and allow you to access it
// using the cli->opt() function.

if($cli->opt('download')) { // To see if the option isset, returns a bool

// If an option has an input, you can return the input by passing TRUE
// in the second parameter.
    $url .= '&email=' . $cli->opt('email', TRUE);
}

// To help users with a list of available options, cli->print_help()
// prints a help table with a message.
$cli = new cli(array('h' => 'Show Options', 'help' => 'Show Help and Quit'));

$cli->print_help('h'); // print_help can be tied to a single option call.

// This is a full help call, with header text (arg 2), and then a die() call. (arg 3).
$cli->print_help('help', "This is the complete system's options to call", TRUE);

// Loading Options From A Configuration File