-2
我已經創建了我想要與控制檯一些標誌運行一個Ruby腳本說,在與我從控制檯以文件名新文件控制檯和-o店輸出-v標誌打印輸出使用gets()
如何向像-v,-o這樣的ruby腳本提供標誌並相應地放置代碼?
我的代碼具有以下結構:
puts "Enter filename to analyze:\n\n"
filename = gets().chomp
puts "Provide filename to store result in new text file:\n\n"
output = gets().chomp
filesize = File.size(filename)
puts "File size in Bytes:\n#{filesize.to_i}\n"
pagecontent = filesize - 20
puts "\n\nData:\n#{pagecontent}\n\n"
File.open(filename,'r') do |file|
#whole process with few do..end in between that I want to do in 2 different #ways.
#If I provide -v flag on console result of this code should be displayed on console
#and with -o flag it should be stored in file with filename provided on console #stored in output variable declared above
end
end
我知道這OptionParser,但我怎樣才能把我的代碼在其選項,以便它可以作爲我想要的嗎? – 2012-04-05 05:17:53
我鏈接到的文檔有很好的例子 - 閱讀它們。這不是一個「請爲我寫代碼」的網站。嘗試使用OptionParser,如果遇到麻煩,請提出有關該問題的具體問題。 – dbenhur 2012-04-05 05:20:47
看到你的鏈接,它爲我工作,謝謝 – 2012-04-05 06:40:54