From: mathew
Date: 2005-12-17T06:52:45+09:00
Subject: Re: GetoptLong example
Steve Litt wrote:
> def usage()
> puts "Usage:"
> puts "myprog [--help] [--short optional_arg] [--offset
> required_arg]"
> end
Handy hint: use RDoc::usage
e.g.
#!/usr/bin/ruby
# == Synopsis
#
# What the program does
#
# == Usage
#
# progname [ -h | --help ] [ --arg1 val1 | -a val1 ] [ --flag ] arg
#
# arg:
# The thing to do stuff to.
#
# val1:
# An optional value.
#
# --flag makes the program do something else.
#
# == Author
# Your Name Here (mailto:you@example.com)
#
# == Copyright
# Copyright (c) 2005 Your Name. Licensed under the same terms as Ruby.
require 'getoptlong'
require 'rdoc/usage'
opts = GetoptLong.new(
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--flag', '-f', GetoptLong::NO_ARGUMENT ],
[ '--arg1', '-a', GetoptLong::REQUIRED_ARGUMENT ]
)
@flag = false
@arg1 = nil
opts.each do |opt, arg|
case opt
when '--help'
RDoc::usage
when '--flag'
@flag = true
when '--val1'
@arg1 = arg
else
puts "Unrecognized option #{opt} (try --help)"
exit 0
end
end
mathew
--
My parents went to the lost kingdom of Hyrule
and all I got was this lousy triforce.