diff options
author | Nathan Gass | 2013-07-17 10:21:36 +0000 |
---|---|---|
committer | Nathan Gass | 2013-07-17 10:21:36 +0000 |
commit | 48251be9125f8e23df180126d50422b0e6561030 (patch) | |
tree | c4eff0421149faf8514c38ab0ebaa762791c9ed7 | |
parent | 9d908d61e587b1b34347635bb22d133314d8093a (diff) | |
download | itools-48251be9125f8e23df180126d50422b0e6561030.tar.gz itools-48251be9125f8e23df180126d50422b0e6561030.tar.bz2 itools-48251be9125f8e23df180126d50422b0e6561030.zip |
add --opts variant to shell_command which adds dashes for posix options, add tests for -opts and --opts
-rw-r--r-- | it.class | 6 | ||||
-rwxr-xr-x | tests/exec.t | 52 |
2 files changed, 56 insertions, 2 deletions
@@ -550,15 +550,17 @@ static function shell_command(/* $cmd, $values1 = array(), ... */) #for escapeshellarg in it::_exec_quotevalue $oldlocale = setlocale(LC_CTYPE, 0); setlocale(LC_CTYPE, 'de_CH'); - foreach (it::match('({(-?)([a-z0-9]\w*)})', $cmd, array('all' => true)) as $tags) + foreach (it::match('({(-?-?)([a-z0-9]\w*)})', $cmd, array('all' => true)) as $tags) { list($tag, $option, $key) = $tags; $parts = array(); if ($option) { - foreach ((array)$values["-$key"] as $key => $value) + foreach ((array)$values[$option . $key] as $key => $value) { + if ($option == '--') + $key = ($key[1] ? '--' : '-') . $key; if ($value === true || $value === false || $value === null) $parts[] = $value ? $key : ""; else foreach ((array)$value as $val) diff --git a/tests/exec.t b/tests/exec.t index 6418716..4702681 100755 --- a/tests/exec.t +++ b/tests/exec.t @@ -10,6 +10,58 @@ is(it::shell_command("echo {arg}", array('arg' => 'gna07,-:blah')), "echo gna07, is(it::shell_command("echo {arg}", array('arg' => '2>&1')), "echo '2>&1'", "quote arguments with dangerous characters"); is(it::shell_command("echo {arg}", array('arg' => '')), "echo ''", "quote empty arguments"); + +is( + it::shell_command("echo {-opts}", array('-opts' => array('--longopt' => true))), + "echo --longopt", + "options argument with long option" +); +is( + it::shell_command("echo {-opts}", array('-opts' => array('-onedash' => true))), + "echo -onedash", + "... with long option but only one dash" +); +is( + it::shell_command("echo {-opts}", array('-opts' => array('-s' => true))), + "echo -s", + "... with short option" +); +is( + it::shell_command("echo {-opts}", array('-opts' => array('--longopt' => 'val'))), + "echo --longopt val", + "... with long option with value" +); +is( + it::shell_command("echo {-opts}", array('-opts' => array('-onedash' => 'val'))), + "echo -onedash val", + "... with long option but only one dash and with value" +); +is( + it::shell_command("echo {-opts}", array('-opts' => array('-s' => 'val'))), + "echo -s val", + "... with short option with value" +); +is( + it::shell_command("echo {-opts}", array('-opts' => array('--longopt' => false))), + "echo ", + "... with disabled long option" +); +is( + it::shell_command("echo {-opts}", array('-opts' => array('-s' => false))), + "echo ", + "... with disabled short option" +); +is( + it::shell_command("echo {--opts}", array('--opts' => array('longopt' => true))), + "echo --longopt", + "... long option without dashes for --opts" +); +is( + it::shell_command("echo {--opts}", array('--opts' => array('s' => true))), + "echo -s", + "... short option without dashes for --opts" +); + foreach (array("", "C", "de_CH", "de_CH.utf8") as $locale) { setlocale(LC_ALL, $locale); $arg = "preĆ¼post"; |