require 'getoptlong'

opts = GetoptLong.new(
  [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
  [ '--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT ],
  [ '--name', GetoptLong::OPTIONAL_ARGUMENT ]
)

dir = nil
name = nil
repetitions = 1
opts.each do |opt, arg|
  case opt
    when '--help'
      puts
-h, --help:
   show help

--repeat x, -n x:
   repeat x times

--name [name]:
   greet user by name, if name not supplied default is John

DIR: The directory in which to issue the greeting.
      EOF
    when '--repeat'
      repetitions = arg.to_i
    when '--name'
      if arg == ''
        name = 'John'
      else
        name = arg
      end
  end
end

if ARGV.length != 1
  puts "Missing dir argument (try --help)"
  exit 0
end

dir = ARGV.shift

Dir.chdir(dir)
for i in (1..repetitions)
  print "Hello"
  if name
    print ", #{name}"
  end
  puts
end

一个简单例子

#!/usr/bin/ruby
require 'getoptlong'
def help()
        puts "help info"
end

def puta(hostname)
        puts hostname
end

opts = GetoptLong.new(
        ['--hostname','-H',GetoptLong::REQUIRED_ARGUMENT],
        ['--help','-h',GetoptLong::NO_ARGUMENT]
)

opts.each do |opt,arg|
        case opt
        when '--hostname'
                hostname = arg
                puta(hostname)
        when '--help'
                help
        end
end
09-04 09:43