OptionParser order of arguments

Discussion in 'Ruby' started by Tom Metge, Jan 15, 2008.

  1. Tom Metge

    Tom Metge Guest

    all-

    i've had a lot of success working with OptionParser, but there is one
    burr in the saddle- order of arguments parsed. sample code:

    require 'optparse'
    class MyClass
    def initialize(arguments)
    @options = {}
    opts = OptionParser.new
    opts.on('--some-param [PARAM]') {|param| @options[:param] = param}
    opts.on('--print-param') { puts @options[:param]}
    opts.parse!
    end
    end
    MyClass.new(ARGV)

    put that in a file (opt_test.rb) and run the following:

    ruby opt_test.rb --print-param --some-param tom
    $ nil
    ruby opt_test.rb --some-param tom --print-param
    $ tom

    obviously, it works as intended, but it does require that one pass
    arguments in a particular order. anyone know of a way around this?

    thanks all,
    tom
     
    Tom Metge, Jan 15, 2008
    #1
    1. Advertising

  2. Hi,

    At Tue, 15 Jan 2008 15:09:58 +0900,
    Tom Metge wrote in [ruby-talk:287471]:
    > require 'optparse'
    > class MyClass
    > def initialize(arguments)
    > @options = {}

    print_param = false
    > opts = OptionParser.new
    > opts.on('--some-param [PARAM]') {|param| @options[:param] = param}

    opts.on('--print-param') { print_param = true }
    > opts.parse!

    puts @options[:param] if print_param
    > end
    > end
    > MyClass.new(ARGV)


    --
    Nobu Nakada
     
    Nobuyoshi Nakada, Jan 15, 2008
    #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. Florian Lindner

    Problem with OptionParser

    Florian Lindner, Feb 25, 2004, in forum: Python
    Replies:
    2
    Views:
    1,742
    Florian Lindner
    Feb 26, 2004
  2. Steven W. Orr
    Replies:
    0
    Views:
    434
    Steven W. Orr
    Feb 9, 2007
  3. Steven W. Orr
    Replies:
    3
    Views:
    320
    Steven Bethard
    Feb 11, 2007
  4. hellt

    Need help with OptionParser

    hellt, Mar 25, 2008, in forum: Python
    Replies:
    2
    Views:
    252
    Stargaming
    Mar 25, 2008
  5. byrnejb
    Replies:
    0
    Views:
    106
    byrnejb
    Jul 15, 2010
Loading...

Share This Page