DRY Documentation between method and OptionParser

Discussion in 'Ruby' started by bwv549, Jun 7, 2007.

  1. bwv549

    bwv549 Guest

    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...t&q=dry documentation&rnum=1#6ca8309210b85d72
    but I'm not sure how I would apply it to the above situation!


    Many Thanks :)
    bwv549, Jun 7, 2007
    #1
    1. Advertising

  2. bwv549

    greg Guest

    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
    #

    On Jun 7, 12:10 pm, bwv549 <> wrote:
    > 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/b9...
    > but I'm not sure how I would apply it to the above situation!
    >
    > Many Thanks :)
    greg, Jun 8, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    397
  2. Marc Aymerich
    Replies:
    9
    Views:
    273
    Marc Aymerich
    Feb 3, 2011
  3. egbert

    DRY and class variables

    egbert, Sep 8, 2011, in forum: Python
    Replies:
    0
    Views:
    122
    egbert
    Sep 8, 2011
  4. Martin DeMello

    Rubyforge, RAA and DRY

    Martin DeMello, Mar 3, 2005, in forum: Ruby
    Replies:
    1
    Views:
    72
    Daniel Berger
    Mar 3, 2005
  5. Becca Girl
    Replies:
    0
    Views:
    90
    Becca Girl
    Jul 27, 2007
Loading...

Share This Page