Skip to content

Instantly share code, notes, and snippets.

@tangblack
Created July 24, 2009 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tangblack/153823 to your computer and use it in GitHub Desktop.
Save tangblack/153823 to your computer and use it in GitHub Desktop.
Repeat input message.
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