get_cli_args() - WordPress 函数参考中文文档

返回介绍

get_cli_args()

发布于 2017-09-10 字数1927 浏览 941 评论 0

get_cli_args( string $param,  bool $required = false )

Returns value of command line params.


描述

Exits when a required param is not set.


参数

$param

(string)
(Required)

$required

(bool)
(Optional)

Default value: false


返回值

(mixed)


源代码

File: wp-admin/includes/class-wp-importer.php

function get_cli_args( $param, $required = false ) {
	$args = $_SERVER['argv'];

	$out = array();

	$last_arg = null;
	$return = null;

	$il = sizeof( $args );

	for ( $i = 1, $il; $i < $il; $i++ ) {
		if ( (bool) preg_match( "/^--(.+)/", $args[$i], $match ) ) {
			$parts = explode( "=", $match[1] );
			$key = preg_replace( "/[^a-z0-9]+/", "", $parts[0] );

			if ( isset( $parts[1] ) ) {
				$out[$key] = $parts[1];
			} else {
				$out[$key] = true;
			}

			$last_arg = $key;
		} elseif ( (bool) preg_match( "/^-([a-zA-Z0-9]+)/", $args[$i], $match ) ) {
			for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
				$key = $match[1]{$j};
				$out[$key] = true;
			}

			$last_arg = $key;
		} elseif ( $last_arg !== null ) {
			$out[$last_arg] = $args[$i];
		}
	}

	// Check array for specified param
	if ( isset( $out[$param] ) ) {
		// Set return value
		$return = $out[$param];
	}

	// Check for missing required param
	if ( !isset( $out[$param] ) && $required ) {
		// Display message and exit
		echo "\"$param\" parameter is required but was not specified\n";
		exit();
	}

	return $return;
}

Collapse full 源代码 code

View on Trac


User Contributed Notes

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!