more optparse examples...

Discussion in 'Ruby' started by Ara.T.Howard, Dec 18, 2003.

  1. Ara.T.Howard

    Ara.T.Howard Guest

    i've been slowing adding to my optparse examples over the last few days...

    if anyone wants to add one to these, send one to me and i'll post it back on
    this list - i know there are examples out there but i'm simple minded and
    needed to figure some of it out myself. to add an example, just do a
    cut-n-paste of one of mine... this is a work in progress so no flaming ;-)

    feel free to contact me offline with additions

    file: optparse/eg.rb
    ====
    #!/usr/bin/env ruby
    require 'optparse'


    def EXAMPLE(tag='')
    @example ||= 0
    STDOUT.printf "\n%s\nEXAMPLE %d - %s\n\n", '=' * 42, @example, tag
    @example += 1
    fork do
    def exit; exit!; end
    yield
    STDOUT.flush; STDERR.flush
    end
    Process.wait; STDOUT.flush; STDERR.flush
    end


    ###############################################################################

    EXAMPLE('arg with no option') do

    ###############################################################################

    command_line = %w(--help)
    ARGV.replace command_line

    ARGV.options do |q|
    q.def_option('--help', '-h', 'show this message') do
    puts q
    exit
    end
    q.parse! # parse here so errors are automatically caught!
    end

    end # EXAMPLE



    ###############################################################################

    EXAMPLE('arg with optional option') do

    ###############################################################################

    command_lines = [
    %w(--verbose),
    %w(--verbose=42),
    ]

    command_lines.each do |command_line|
    STDOUT.printf "RUNNING WITH <%s>\n", command_line
    ARGV.replace command_line

    q = nil
    ARGV.options do |q|
    q.def_option('--verbose=[verbosity]', '-v', 'vebose mode') do |arg|
    $verbose = arg || true
    end
    end
    q.parse! # parse here so errors _aren't_ caught!

    STDOUT.printf "$verbose = %s\n", $verbose
    end

    end # EXAMPLE



    ###############################################################################

    EXAMPLE('arg with required option') do

    ###############################################################################

    command_lines = [
    %w(--required=42),
    %w(--required),
    ]

    command_lines.each do |command_line|
    STDOUT.printf "RUNNING WITH <%s>\n", command_line
    ARGV.replace command_line

    q = nil
    ARGV.options do |q|
    q.def_option('--required=ARBITRARY', '-a', 'arg') { $r = 42}
    end
    q.parse! # parse here so errors aren't caught!

    STDOUT.printf "$r = %s\n", $r
    end

    end # EXAMPLE



    ###############################################################################

    EXAMPLE('parsing something other than ARGV') do

    ###############################################################################

    command_line = %w(--help)

    op = OptionParser.new
    op.def_option('--help', '-h', 'show this message') do
    puts op
    exit
    end
    op.def_option '--foo', '-f', 'foo'
    op.parse command_line

    end # EXAMPLE



    ###############################################################################

    EXAMPLE('parsing something other than ARGV destructively') do

    ###############################################################################

    command_line = %w(--foo)

    op = OptionParser.new
    op.def_option '--foo', '-f', 'foo'

    printf "before: %s\n", command_line.inspect
    op.parse! command_line
    printf "after: %s\n", command_line.inspect

    end # EXAMPLE



    ###############################################################################

    EXAMPLE('parsing something other than ARGV non-destructively') do

    ###############################################################################

    command_line = %w(--foo)

    op = OptionParser.new
    op.def_option '--foo', '-f', 'foo'

    printf "before: %s\n", command_line.inspect
    op.parse command_line
    printf "after: %s\n", command_line.inspect

    end # EXAMPLE
    ====

    -a
    --

    ATTN: please update your address books with address below!

    ===============================================================================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | STP :: http://www.ngdc.noaa.gov/stp/
    | NGDC :: http://www.ngdc.noaa.gov/
    | NESDIS :: http://www.nesdis.noaa.gov/
    | NOAA :: http://www.noaa.gov/
    | US DOC :: http://www.commerce.gov/
    |
    | The difference between art and science is that science is what we
    | understand well enough to explain to a computer.
    | Art is everything else.
    | -- Donald Knuth, "Discover"
    |
    | /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
    ===============================================================================
     
    Ara.T.Howard, Dec 18, 2003
    #1
    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. GMTaglia

    optparse examples

    GMTaglia, Oct 17, 2004, in forum: Python
    Replies:
    4
    Views:
    510
    GMTaglia
    Oct 18, 2004
  2. Michael
    Replies:
    4
    Views:
    419
    Matt Hammond
    Jun 26, 2006
  3. Mark Wilson
    Replies:
    1
    Views:
    129
    Takashi & Kayoko Sano
    Sep 4, 2003
  4. Jeff Dickens

    optparse examples

    Jeff Dickens, Dec 5, 2003, in forum: Ruby
    Replies:
    1
    Views:
    121
  5. Robert Klemme

    With a Ruby Yell: more, more more!

    Robert Klemme, Sep 28, 2005, in forum: Ruby
    Replies:
    5
    Views:
    218
    Jeff Wood
    Sep 29, 2005
Loading...

Share This Page