A
Ara.T.Howard
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'
===============================================================================
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'
===============================================================================