From: "ara.t.howard" Date: 2008-07-03T13:01:37+09:00 Subject: Re: Lost with OptionParser On Jul 2, 2008, at 6:54 PM, Greg Willits wrote: > I'm having a real problem understanding OptionParser -- the docs in > pickaxe are rather cryptic, Ruby Way doesn't even talk about it, and > all > the Q&A I can find on the web doesn't clear it up for me. > > I've gotten this far: > > # test.rb --------- > require 'optparse' > > commands = OptionParser.new > commands.on("-h", "--help") # more or less, I get the .on stuff > commands.on("-e=RUN_ENV") > > commands.parse(ARGV) > > puts commands > > # --------- > > That will dump a display of the commands, but for the life of me, I > can't figure out how to determine which options were actually > submitted. > And if the -e param was supplied, what's its value is. > > I have seen: > > commands.parse(ARGV) > commands.parse!(ARGV) > options = commands.parse(ARGV) > > So, confused by that to start with, then really confused by where the > parsed commands are, and what format they're in. > > -- gw > -- > Posted via http://www.ruby-forum.com/. > cfp:~ > cat a.rb require 'yaml' require 'rubygems' ### gem install rubygems require 'main' Main { option('environment', 'e'){ synopsis 'specify run time environment' default 'development' argument :required validate{|arg| %w[ production development test ].include? arg} } def run environment = params['environment'] y :given => environment.given?, :value => environment.value end } cfp:~ > ruby a.rb --- :value: development :given: cfp:~ > ruby a.rb --environment=foobar invalid: option(--environment)="foobar" cfp:~ > ruby a.rb --environment=test --- :value: test :given: true cfp:~ > ruby a.rb --help NAME a.rb SYNOPSIS a.rb [options]+ DESCRIPTION main.rb takes the pain of argument parsing away PARAMETERS specify run time environment --help, -h the packages ships with a tons of examples. a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama