我正在使用Ruby來執行一個代碼,它採用命令行參數。 現在我試圖使用不同的選項相同的程序,所以我把選項放在一個文件中,我希望程序讀取每行解釋選項並相應地執行程序。OptionParser解析參數的形式文件,而不是命令行
但我得到這個錯誤。 「C:/Ruby193/lib/ruby/1.9.1/optparse.rb:1348:block in parse_in_order': undefined method
shift」for「--c execue --query unix --Servername abc123」:String(NoMethodError)「
i理解它讀取文件並將該行視爲字符串。但是想知道是否有辦法克服這種移位錯誤,並將該行看作是在命令提示符下輸入的。或者更好的解決方案。
這是我的代碼。
require 'optparse'
require 'micro-optparse'
# --command execue --query unix command --Servername abc123
f =File.open("list_of_commands.txt", "r")
f.each_line { |line|
line= line.chomp
#line = "--c execue --query unix --Servername abc123"
#line = eval("\"#{line}\"")
puts line
options = {}
OptionParser.new do |opts|
opts.on("-c", "--command result,execue,chart,scpfile", String, "Single command to execute ") do |c|
options[:comd] = c
end
opts.on("-q", "--query remote command, unix command", String, "performs the command on local or remote machine") do |q|
options[:query] = q
end
opts.on("-s", "--servername CHSXEDWDC002 ", String, "server name to execute the command") do |v|
options[:hname] = v
end
opts.on_tail('-h', '--help', 'Show this message') do
puts opts
exit
end
end.parse!(line)
p options
}
該文件的內容是以下 --c execue --query UNIX --servername ABC123
我還試圖用微optparse但面臨同樣的錯誤。任何解決方法?
更新: 根據「@mu太短」的建議,我嘗試下面的選項。 (「#{Shellwords.shellsplit(line)}」)和/或 end.parse!(Shellwords.shellsplit(line))。 但他們都沒有工作。
我還嘗試使用「line = line.split(」\ t「)」將數據行拆分爲數組,然後使用end.parse!(line)。出把儘可能 --c execue --query UNIX --servername ABC123
,但現在我得到的塊錯誤:無效選項--C執行
更新:#2 看着錯誤,問題是用錯誤的參數(-c但由於用戶「@mu太短」的建議使用陣列
更新:3 傳遞數組只工作編輯了諸如-c之類的參數的簡短格式,但是當長格式被提供時,它以失敗的參數erorr失敗。
我沒有看到有關optparse的文檔。我甚至試過微解析,但它requres默認valuves和它不是我的選擇:(
嗨感謝您的快速回復。我試過這些選項。 end.parse!(「#{Shellwords.shellsplit(line)}」)和end.parse!(Shellwords.shellsplit(line))。但他們都沒有工作。我也嘗試使用「line = line.split(」\ t「)」然後end.parse!(line)將行分割爲數組。但不是我得到'塊在':無效選項 - c執行 –
user2300908
2013-04-24 22:25:29
您的'opts.on'參數看起來不適合'--c'選項。當你可能指' - 命令'時,爲什麼你有'--Command'? – 2013-04-24 22:54:04
該問題還沒有完全解決,通過傳遞數組我可以使用參數的簡短形式,但是當傳遞長的形式我得到錯誤爲「無效的選項: - 命令執行(OptionParser :: InvalidOption)。是我我使用命令而不是命令 – user2300908 2013-04-25 06:46:57