array of valid arguments in optparse

Discussion in 'Ruby' started by Ritesh Agrawal, Mar 2, 2011.

  1. Hi,

    I am trying to use optparse to parse an argument into an array and check
    for possible values. For instance consider an argument like this

    --operations count, count+, count-

    I want to parse above argument into @options[:eek:perations] = ["count",
    "count+", "count-"]. But at the same time I have limited vocabulary for
    possible operations and I want to check for that so that if some gives
    any other operation argument, it throws an error message

    @OPERATIONS=["count", "count+", "count-"]
    @optparse = OptionParser.new do |opts|
    opts.banner = "Usage: ruby #{__FILE__}
    --operations=METHOD1,METHOD2,METHOD3.."

    opts.on('-o OPERATIONS', '--operations a,b,c',
    @OPERATIONS, Array, "Possible operations are #{@OPERATIONS.join(', ')}"
    ) do |hypo|
    @options[:eek:perations]=[] unless @options[:eek:perations]
    @options[:eek:perations] << hypo.to_s.strip.downcase()
    @options[:eek:perations] = @options[:eek:perations].uniq
    end
    end
    @optparse.parse!

    --
    Posted via http://www.ruby-forum.com/.
    Ritesh Agrawal, Mar 2, 2011
    #1
    1. Advertising

  2. Ritesh Agrawal wrote in post #985009:
    > I am trying to use optparse to parse an argument into an array and check
    > for possible values. For instance consider an argument like this
    >
    > --operations count, count+, count-
    >
    > I want to parse above argument into @options[:eek:perations] = ["count",
    > "count+", "count-"].


    Well, the spaces will cause the shell to split it; the user will
    probably need to type

    --operations count,count+,count-

    or

    --operations "count, count+, count-"

    If you look at the source code for optparse.rb you'll find an example of
    list arguments:

    # # List of arguments.
    # opts.on("--list x,y,z", Array, "Example 'list' of arguments")
    do |list|
    # options.list = list
    # end

    > But at the same time I have limited vocabulary for
    > possible operations and I want to check for that so that if some gives
    > any other operation argument, it throws an error message


    Just throw the error yourself.

    OPERATIONS = ["count", "count+", "count-"]

    opts.on ... do |hypo|
    raise OptionParser::InvalidArgument, "Invalid operation" unless (hypo
    - OPERATIONS).empty?
    end

    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Mar 2, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tomi Silander
    Replies:
    3
    Views:
    901
    Grant Edwards
    Apr 6, 2005
  2. Alex Gittens
    Replies:
    1
    Views:
    456
    Peter Hansen
    Jul 10, 2005
  3. Steven Bethard
    Replies:
    0
    Views:
    390
    Steven Bethard
    May 10, 2006
  4. Ritesh Raj Sarraf

    optparse multiple arguments

    Ritesh Raj Sarraf, Jun 30, 2006, in forum: Python
    Replies:
    8
    Views:
    726
    Steven Bethard
    Jul 1, 2006
  5. Ben Woodcroft
    Replies:
    2
    Views:
    165
    Ben Woodcroft
    May 20, 2009
Loading...

Share This Page