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;
}
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))); }