@@ -42,6 +42,9 @@ function _M.new(address)
42
42
43
43
local buffer = " "
44
44
local ret = {stdout = " " , stderr = " " , exitcode = " " , termsig = " " }
45
+
46
+ -- detach callbacks from self
47
+ local det_stdout , det_stderr
45
48
46
49
if # args > 0 then
47
50
if type (args [1 ]) == " table" then
@@ -54,6 +57,23 @@ function _M.new(address)
54
57
end
55
58
if args [1 ].stdin then self .stdin = args [1 ].stdin end
56
59
if args [1 ].timeout_fatal then self .timeout_fatal = args [1 ].timeout_fatal end
60
+
61
+ if args [1 ].stdout then
62
+ if type (args [1 ].stdout ) == " function" then
63
+ det_stdout = args [1 ].stdout
64
+ else
65
+ return nil , " invalid argument 'stdout' (requires function)"
66
+ end
67
+ end
68
+
69
+ if args [1 ].stderr then
70
+ if type (args [1 ].stderr ) == " function" then
71
+ det_stderr = args [1 ].stderr
72
+ else
73
+ return nil , " invalid argument 'stderr' (requires function)"
74
+ end
75
+ end
76
+
57
77
else
58
78
self .argv = args
59
79
end
@@ -112,13 +132,17 @@ function _M.new(address)
112
132
if not curfield then curfield = v
113
133
else
114
134
if curfield == " stdout" then
115
- if self .stdout then
135
+ if det_stdout then
136
+ det_stdout (v )
137
+ elseif self .stdout then
116
138
self .stdout (v )
117
139
else
118
140
ret .stdout = ret .stdout .. v
119
141
end
120
142
elseif curfield == " stderr" then
121
- if self .stderr then
143
+ if det_stderr then
144
+ det_stderr (v )
145
+ elseif self .stderr then
122
146
self .stderr (v )
123
147
else
124
148
ret .stderr = ret .stderr .. v
0 commit comments