Ruby OptParse example

An example of command line argument handling including deletion of argument options

# wc.rb
require 'optparse'
ProgramOptions =, :onlyWords, :onlyLines)
options =, false, false)
opts =
p options
opts.on("-c","--char") { options.onlyChars = true; ARGV.delete("-c"); ARGV.delete("--char") }
opts.on("-w","--word") { options.onlyWords = true; ARGV.delete("-w"); ARGV.delete("--word") }
opts.on("-l","--line") { options.onlyLines = true; ARGV.delete("-l"); ARGV.delete("--line") }
p options

rubyoptparse.txt · Last modified: 2014/10/25 21:52 (external edit)
CC Attribution-Share Alike 3.0 Unported Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0