JBZoo\Utils\Cli::build PHP Метод

build() публичный статический Метод

Build params for cli options
public static build ( string $command, array $args = [] ) : string
$command string
$args array
Результат string
    public static function build($command, $args = array())
    {
        $stringArgs = array();
        $realCommand = $command;
        if (count($args) > 0) {
            foreach ($args as $key => $value) {
                $value = trim($value);
                $key = trim($key);
                if (strpos($key, '-') !== 0) {
                    if (strlen($key) == 1) {
                        $key = '-' . $key;
                    } else {
                        $key = '--' . $key;
                    }
                }
                if (strlen($value) > 0) {
                    $stringArgs[] = $key . '="' . addcslashes($value, '"') . '"';
                } else {
                    $stringArgs[] = $key;
                }
            }
        }
        if (count($stringArgs)) {
            $realCommand = $command . ' ' . implode(' ', $stringArgs);
        }
        return $realCommand;
    }

Usage Example

Пример #1
0
 public function testBuild()
 {
     is('ls', Cli::build('ls'));
     is('ls -a', Cli::build('ls', array('a' => '')));
     is('ls -a -l', Cli::build('ls', array('a' => '', 'l' => '')));
     is('ls --help', Cli::build('ls', array('help' => '')));
     is('ls --option="qwerty"', Cli::build('ls', array('option' => 'qwerty')));
     is('ls --option="qwert\'y"', Cli::build('ls', array('option' => 'qwert\'y')));
     is('ls --option="qwert\\"y"', Cli::build('ls', array('option' => 'qwert"y')));
     is('ls --option="0"', Cli::build('ls', array('option' => 0)));
 }