1

i try to set my script to run something like this

ruby Script.rb --ip "192.168.3.206"

But if there is no ip parameter then it use default "192.168.1.1

I try this code, but it's always return nil as ip

options = {}

OptionParser.new do |opts|
  options[:ip] = "192.168.1.1"
  opts.on("-i", "--ip", String, "Set ip") do |command_line_ip|
    options[:ip] = command_line_ip
  end
end.parse!
p options

Output of this code is

{:ip=>nil}

Please, tell me where is my code wrong?

1 Answer 1

4

You have to indicate that there is an argument to your --ip switch like this:

opts.on("-i MANDATORY", "--ip=MANDATORY", String, "Set ip") do |command_line_ip|
Sign up to request clarification or add additional context in comments.

1 Comment

Just to clarify. It does not have to be the string 'MANDATORY', it can be any string you'd like, it simply denotes that the command accepts a value afterwards. For example: opts.on('--width NUM', Integer, 'Set width of game board') do |w|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.