Created
July 24, 2009 03:31
-
-
Save tangblack/153823 to your computer and use it in GitHub Desktop.
Repeat input message.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'optparse' | |
# Init parameters. | |
# Refernce: optparse.rb | |
def init_params(args) | |
# Parameters filter by OptionParser. | |
params = {:repeat => 1, :input => 'N/A'} | |
OptionParser.new do |opts| | |
opts.banner = "Usage: #{$0} [options]" | |
opts.on('-r', '--repeat [NUMBER]', 'repeat [NUMBER] times of input string') do |number| | |
params[:repeat] = number.to_i || 1 | |
end | |
opts.on('-i', '--input [STRING]', '[STRING] show on the screen') do |string| | |
params[:input] = string || 'defualt message' | |
end | |
opts.on_tail('-h', '--help', 'display this help and exit') do | |
puts opts | |
exit | |
end | |
end.parse!(args) | |
params | |
end | |
# Repeat input message. | |
def print(input, repeat) | |
repeat.times{puts "#{input}"} | |
end | |
params = init_params(ARGV) | |
print(params[:input], params[:repeat]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment