Forum: Ruby DRY Documentation between method and OptionParser

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
883ee4d990ef9e57a79b5b0ff3b66241?d=identicon&s=25 bwv549 (Guest)
on 2007-06-07 19:11
(Received via mailing list)
I suspect that lots of folks have run into this very problem:

I have a method.  It has lots of options.  I need a command line
program that will give users command line access to run the method.  I
can use OptionParser to parse the command line options into a hash and
give this hash to my method, like this:

# The method:
def my_method(arg1, arg2, *opts)

# The command line interface:
opts = {}
parser = do |op|
  op.banner = "usage: progname.rb arg1 arg2 [OPTIONS]"
  op.on("-f", "--first", Float, "this is the first option") {|v|
opts[:first] = v }
  op.on("-s", "--second", Array, "second option is an array") {|v|
opts[:second] = v }
my_method(ARGV, opts)

I'm trying to figure out the DRY way to document these options, since
they will all be identical.  OptionParser documentation is in the code
and that's great, but how do I embed documentation in the method that
also can end up in OptionParser documentation.  Any ideas?  (This
situation comes up frequently in my work, or I wouldn't ask)

I've read through this post and it looks helpful (albeit fairly old
[will it still work]):
but I'm not sure how I would apply it to the above situation!

Many Thanks :)
Eae21abfabb19c4617b2630386994fd9?d=identicon&s=25 greg (Guest)
on 2007-06-08 20:37
(Received via mailing list)
rdoc has a :include tag that will allow you to include another file.
So generate your documentation with a rake task and then include that
file into RDoc

task :foo_doc => "" do
  sh "rdoc"

file "" => "foo.rb" do
  sh "ruby foo.rb -h >"

$ cat foo.rb
# This is the foo class
# Usage:
#  :include:
This topic is locked and can not be replied to.