# DRY Documentation between method and OptionParser

**URL:** <https://www.ruby-forum.com/t/dry-documentation-between-method-and-optionparser/106233>\
**Category:** Ruby\
**Created:** [June 7, 2007, 7:11pm UTC](https://www.ruby-forum.com/t/dry-documentation-between-method-and-optionparser/106233 "2007-06-07T19:11:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [June 7, 2007, 7:11pm UTC](https://www.ruby-forum.com/t/dry-documentation-between-method-and-optionparser/106233/1 "2007-06-07T19:11:02Z")

</div>

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)  
…  
end

# The command line interface:

opts = {}  
parser = OptionParser.new 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 }  
end  
parser.parse!  
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]):  
[http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/b9a6985c1695531f/6ca8309210b85d72?lnk=gst&q=dry+documentation&rnum=1#6ca8309210b85d72](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/b9a6985c1695531f/6ca8309210b85d72?lnk=gst&q=dry+documentation&rnum=1#6ca8309210b85d72)  
but I’m not sure how I would apply it to the above situation!

Many Thanks 🙂

---

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [June 8, 2007, 8:37pm UTC](https://www.ruby-forum.com/t/dry-documentation-between-method-and-optionparser/106233/2 "2007-06-08T20:37:01Z")

</div>

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 =\> “foo.help” do  
sh “rdoc”  
end

file “foo.help” =\> “foo.rb” do  
sh “ruby foo.rb -h \>foo.help”  
end

$ cat foo.rb

# This is the foo class

# 

# Usage:

# 

# :include: foo.help

#
