From df723ea22907ee2b387daa43273da45f6698aac1 Mon Sep 17 00:00:00 2001 From: AitorATuin Date: Sun, 30 Apr 2017 23:30:00 +0200 Subject: [PATCH 1/3] Don't set the hook to map undefined variables by default. While this is a nice feature when using lua as a pure shell script language when using luash as a module to run shell commands embedded in another program it's undesirable not to have undefined variables. That feature can be enabled at any time calling the `install` function provided by the module or at `require` time as: ``` local sh = require('sh').install() ``` --- sh.lua | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/sh.lua b/sh.lua index e09ebdf..ce873ef 100644 --- a/sh.lua +++ b/sh.lua @@ -89,11 +89,6 @@ if mt == nil then setmetatable(_G, mt) end --- set hook for undefined variables -mt.__index = function(t, cmd) - return command(cmd) -end - -- export command() function and configurable temporary "input" file M.command = command M.tmpfile = '/tmp/shluainput' @@ -105,4 +100,16 @@ setmetatable(M, { end }) +-- Let's luash to deal with undefined variables, those will be commands +M.install = function() + -- set hook for undefined variables + mt.__index = function(t, cmd) + return command(cmd) + end + -- Remove install from M now that the feature has been activated + M.install = nil + + return M +end + return M From 9254be17f7a2d667e6607d039af9237c4d9c5b9d Mon Sep 17 00:00:00 2001 From: AitorATuin Date: Sun, 30 Apr 2017 23:35:52 +0200 Subject: [PATCH 2/3] Modify README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index dc11d83..fff7b18 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Every command that can be called via os.execute can be used a global function. All the arguments passed into the function become command arguments. ``` lua -require('sh') +require('sh').install() local wd = tostring(pwd()) -- calls `pwd` and returns its output as a string @@ -45,7 +45,7 @@ one I/O loop at a time). So the inner-most command is executed, its output is read, the the outer command is execute with the output redirected etc. ``` lua -require('sh') +require('sh').install() local words = 'foo\nbar\nfoo\nbaz\n' local u = uniq(sort({__input = words})) -- like $(echo ... | sort | uniq) @@ -97,7 +97,7 @@ not work in Lua 5.1 and current LuaJIT. Key-value arguments can be also specified as argument table pairs: ```lua -require('sh') +require('sh').install() -- $ somecommand --format=long --interactive -u=0 somecommand({format="long", interactive=true, u=0}) From 18d385740a7adbb673262a265ef78e105a18e764 Mon Sep 17 00:00:00 2001 From: AitorATuin Date: Sun, 30 Apr 2017 23:44:19 +0200 Subject: [PATCH 3/3] Fix tests --- tests/test.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test.lua b/tests/test.lua index fd1348a..93a35e8 100644 --- a/tests/test.lua +++ b/tests/test.lua @@ -16,7 +16,7 @@ require('gambiarra')(function(e, test, msg) end end) -local sh = require('sh') +local sh = require('sh').install() test('Check command output', function() ok(tostring(seq(1, 5)) == '1\n2\n3\n4\n5', 'seq 1 5')