Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > more optparse examples...

Reply
Thread Tools

more optparse examples...

 
 
Ara.T.Howard
Guest
Posts: n/a
 
      12-18-2003

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'
================================================== =============================

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Kamaelia 0.4.0 RELEASED - Faster! More Tools! More Examples! More Docs! ;-) Michael Python 4 06-26-2006 08:00 AM
With a Ruby Yell: more, more more! Robert Klemme Ruby 5 09-29-2005 06:37 AM
Required options in optparse Gregory (Grisha) Trubetskoy Python 0 10-04-2003 01:31 AM
blanks embedded in python 2.3 optparse pascal barbedor Python 3 07-29-2003 10:19 PM
Re: Iterating over optparse options David Goodger Python 0 06-25-2003 01:46 PM



Advertisments