【发布时间】:2015-03-04 14:59:47
【问题描述】:
我在 Ruby 中使用 Docopt 来解析我的命令选项,稍后在脚本中我使用 gets.chomp 获取控制台输入。问题是在Docopt使用options = Docopt::docopt(doc)解析之后,运行程序的所有参数仍然留在ARGF中,并且在尝试从STDIN获取之前执行gets命令从ARGF获取。
我试图清除 ARGF,但由于某种原因,ARGF.gets 试图将输入作为命令运行。我认为清除 ARGF 或使用其他输入法都可能是解决方案,但我还没有找到任何东西。我不得不想象我不是第一个尝试在 Ruby 中使用 Docopt 获得交互式命令行输入的人,所以我希望答案就在那里。
更多代码供喜欢的人使用:
#!/usr/bin/ruby
require 'docopt'
doc=<<eos
Usage:
account_creator.rb --noldap [options]
Options:
-h, --help Show help page
-l, --ldap
-n, --noldap
-s SERVER With or without http[s]://, followed by port
--ad
--od
-d NUM
-u USER
-p PASS
-o OUTPUT-FILE Default behavior is to append output to file if it exists
eos
options = {}
begin
options = Docopt::docopt(doc)
rescue Docopt::Exit => e
puts e.message
exit 1
end
if options['-u']
username = options['-u']
else
while username.eql? '' or username == nil
puts "Enter Username:"
username = Kernel.gets.chomp.strip
end
end
【问题讨论】:
-
请发布一些代码,以便我们更好地帮助您解决问题。
标签: ruby console interactive gets docopt