目标:我正在用ruby编写一个工作流命令行程序,它依次执行UNIX shell上的其他程序,其中一些需要用户输入输入。Runy Open3.popen3从命令行输入子进程
问题:虽然我可以成功Nick Charlton处理stdout
和stderr
感谢这个有用blog post,不过,我被困在捕捉用户输入,并将其传递到子过程通过命令行。的代码如下:
方法
module CMD
def run(cmd, &block)
Open3.popen3(cmd) do |stdin, stdout, stderr, thread|
Thread.new do # STDOUT
until (line = stdout.gets).nil? do
yield nil, line, nil, thread if block_given?
end
end
Thread.new do # STDERR
until (line = stderr.gets).nil? do
yield nil, nil, line, thread if block_given?
end
end
Thread.new do # STDIN
# ????? How to handle
end
thread.join
end
end
end
调用方法
此示例调用外壳命令units
它提示用户输入的测量单位,然后提示一个单位转换为。这是怎么会看在外壳
> units
586 units, 56 prefixes # stdout
You have: 1 litre # user input
You want: gallons # user input
* 0.26417205 # stdout
/3.7854118 # stdout
当我运行这个从我的节目,我希望能够以完全相同的方式与它进行交互。
unix_cmd = 'units'
run unix_cmd do | stdin, stdout, stderr, thread|
puts "stdout #{stdout.strip}" if stdout
puts "stderr #{stderr.strip}" if stderr
# I'm unsure how I would allow the user to
# interact with STDIN here?
end
注:调用run
方法这种方法允许用户可以解析输出,控制流程,并添加自定义日志记录。
从我收集到的有关标准输入,下面的代码片段是接近我是来了解如何处理STDIN,明显有在我所知一些差距,因为我仍然不确定如何整合这个放到我上面的run
方法中,并将输入传递给子进程。
# STDIN: Constant declared in ruby
# stdin: Parameter declared in Open3.popen3
Thread.new do
# Read each line from the console
STDIN.each_line do |line|
puts "STDIN: #{line}" # print captured input
stdin.write line # write input into stdin
stdin.sync # sync the input into the sub process
break if line == "\n"
end
end
摘要:我希望了解如何从通过Open3.popen3
方法的命令行处理用户输入,这样我可以允许用户将数据输入到从我的程序称为子指令的各种序列。
我也很想知道如何做到这一点 –