Sunday, 15 August 2010

ruby - With Slop, how can I get both options and the arguments at the end? -



ruby - With Slop, how can I get both options and the arguments at the end? -

i'm using slop command-line argument parsing. know reading documentation how retrieve options:

opts = slop.parse on 'name=', 'your name' on 'p', 'password', 'an optional password', argument: :optional on 'v', 'verbose', 'enable verbose mode' end

... , how retrieve final arguments ...

opts = slop.parse! on :foo end

but not how both @ same time. took through unit tests , tried few things, i'm new ruby , not sure how specify arguments correctly. here's method signature slop.parse! (thanks @engineersmnky)

def parse!(items = argv, config = {}, &block)

i tried:

items = [] options = slop.parse!(optional_arguments: true) |x| on :d, :debug, "print debug messages" items << x end

.. next error:

undefined method `on' #<rspec::examplegroups::configurationprovider_2:0x007fcd4a83d550> # ./spec/configuration_provider_spec.rb:18:in `block (4 levels) in <top (required)>' # ... , on ...

any insight welcome.

i figured out! in retrospect, documentation stating parse! strips out handled arguments contained answer.

options = slop.parse! on :d, :debug, "print debug messages" end remaining_args = argv

when run "prog.rb --debug 12345", options = {:debug => true} , remaining_args = [12345]

ruby

No comments:

Post a Comment