Optparse and required switches

Hi ...

Im lost with optparse.

Got a couple of questions if its OK.

1. I've been reading the documentation but I cant get it into my thick
head how I tell an option that it is required or optional? I guess all
opts are optional initially, but how do I say that an option is
required.

2. How do I gracefully tell the user that an option they provided is
non-existent. I dont want ruby to crash with a big bang and a lot of
error output.

At at last I would like to appologies if this are noobish questions. Im
new to ruby and Im struggling to get a hang of it. But I dont read ruby
code flawlessly and I dont always get the examples that the gems provide
me.

/Roger

···

--
Posted via http://www.ruby-forum.com/.

From: list-bounce@example.com
[mailto:list-bounce@example.com] On Behalf Of Roger Nordqvist
Sent: Monday, October 08, 2007 7:18 AM
To: ruby-talk ML
Subject: optparse and required switches

Hi ...

Im lost with optparse.

Got a couple of questions if its OK.

1. I've been reading the documentation but I cant get it into my thick
head how I tell an option that it is required or optional? I guess all
opts are optional initially, but how do I say that an option is
required.

I don't think support for that is built into optparse - you'll have to set
flags from the options parsed and then throw an error if a certain flag
isn't set.

2. How do I gracefully tell the user that an option they provided is
non-existent. I dont want ruby to crash with a big bang and a lot of
error output.

Read this to learn how to handle exceptions:

http://www.rubycentral.com/pickaxe/tut_exceptions.html

At at last I would like to appologies if this are noobish
questions. Im
new to ruby and Im struggling to get a hang of it. But I dont
read ruby
code flawlessly and I dont always get the examples that the
gems provide
me.

/Roger
--
Posted via http://www.ruby-forum.com/\.

HTH,

Felix

···

-----Original Message-----

Roger Nordqvist wrote:

Hi ...

Im lost with optparse.

Got a couple of questions if its OK.

1. I've been reading the documentation but I cant get it into my thick
head how I tell an option that it is required or optional? I guess all
opts are optional initially, but how do I say that an option is
required.

2. How do I gracefully tell the user that an option they provided is
non-existent. I dont want ruby to crash with a big bang and a lot of
error output.

At at last I would like to appologies if this are noobish questions. Im
new to ruby and Im struggling to get a hang of it. But I dont read ruby
code flawlessly and I dont always get the examples that the gems provide
me.

Try something like this:

require 'optparse'

opts = OptionParser.new do |opts|

  #value for option is required:
  opts.on("-t=ARG") do |val|
    puts "-t option entered with value=#{val}"
  end

  #value for option is optional:
  opts.on("-y [=RANDOM_CHARS]") do |val|
    puts "-y option entered with value=#{val}"
  end

end

begin
  #option -t is required:

···

#----------------------
  found_opt_t = false

  ARGV.each do |word|
    if word[0, 2] == '-t'
      found_opt_t = true
    end
  end

  if not found_opt_t
    puts "Option -t is required. Exiting..."
    exit
  end
  #----------------------

  opts.parse!(ARGV)

rescue OptionParser::MissingArgument => e
  puts e.to_str.capitalize.gsub("argument:", "value for:")
  puts "That option requires a value. Please try again."
  puts "Exiting.."
  exit
end

Test run:

$ ruby r8test.rb -t 10 -y 15
-t option entered with value=10
-y option entered with value=15

$ ruby r8test.rb -t10
-t option entered with value=10

$ ruby r8test.rb -t
Missing value for: -t
That option requires a value. Please try again.
Exiting..

$ ruby r8test.rb
Option -t is required.
Exiting...

$

--
Posted via http://www.ruby-forum.com/\.

For this it might be worth to look into CodeForPeople's main gem (gem
install main). I've been using it in a couple of things lately and am
quite happy. You can specify if an option is required or not, and if
it has arguments which are required or not, etc. in a very simple
syntax.

require 'main'

main {
   option("verbose", "v") {
      required
  }
}

Hope this helps,

Jesus.

···

On 10/8/07, Felix Windt <fwmailinglists@gmail.com> wrote:

> [mailto:list-bounce@example.com] On Behalf Of Roger Nordqvist

> Im lost with optparse.
>
> Got a couple of questions if its OK.
>
> 1. I've been reading the documentation but I cant get it into my thick
> head how I tell an option that it is required or optional? I guess all
> opts are optional initially, but how do I say that an option is
> required.
>

I don't think support for that is built into optparse - you'll have to set
flags from the options parsed and then throw an error if a certain flag
isn't set.

Thanx a lot people for all the helpful answers. I will check this out
asap at work tomorrow.

What Ive seen so far the greatest strength of Ruby is not it's clean
syntax, its nice OOP features or even the gem repository. Nope, for me
as a newbie its the wonderful community surounding Ruby thats make it
all worth trying to learn it.

···

--
Posted via http://www.ruby-forum.com/.

A little change here:

ARGV.each do |word|
    if word[0, 2] == '-t'
      found_opt_t = true
      break #<---if found option, no reason to continue loop
    end
end

···

--
Posted via http://www.ruby-forum.com/.

Amen.

···

-----Original Message-----
From: ruby-talk-admin@ruby-lang.org [mailto:ruby-talk-admin@ruby-lang.org]
On Behalf Of Roger Nordqvist
Sent: Monday, October 08, 2007 12:52 PM
To: ruby-talk ML
Subject: Re: optparse and required switches

Thanx a lot people for all the helpful answers. I will check this out
asap at work tomorrow.

What Ive seen so far the greatest strength of Ruby is not it's clean
syntax, its nice OOP features or even the gem repository. Nope, for me
as a newbie its the wonderful community surounding Ruby thats make it
all worth trying to learn it.
--
Posted via http://www.ruby-forum.com/.

This is what I came up with for required options. Is this the "ruby way"
of doing it or?

require 'optparse'

options = {}

OptionParser.new do |o|
  o.banner = "Usage: #$0 [options]"

  o.on("-s", "--server s", "Get servername to connect to") do |s|
    options[:server] = s
  end

  o.on("-u", "--username u", "Get username to connect as") do |u|
    options[:username] = u
  end

  o.on("-p", "--password p", "Get password for user username") do |p|
    options[:password] = p
  end

  o.on("-f", "--filename f", "Remote path and filename to tail") do |f|
    options[:filename] = f
  end

  o.on("-F", "--follow", "Use -f/--follow with remote tail command") do

follow>

    options[:follow] = follow
  end
end.parse!

# Check to see if we got the required arguments needed?
required_opts = [:server, :username, :password, :filename]

required_opts.each do |opt|
  if options.has_key?(opt)
    next
  else
    puts "Required option --#{opt} missing."
  end
end

···

--
Posted via http://www.ruby-forum.com/\.