in ruby code, define new hash:
options = { host: 'localhost', user: nil, file: nil, }
i parse hash using optionsparser
optionparser.new |opts| opts.banner = 'usage: ruby-remote-tailer.rb [options]' opts.on('-h', '--host', 'the host run ssh session with') |host| options[:host] = "#{host}" end opts.on('-h', '--user', 'the user account run session') |user| options[:user] = "#{user}" end opts.on('-f', '--file', 'the file run tail on') |file| options[:file] = "#{file}" end end
and run:
options = argv.parse! puts options[:host]
the last puts line results in error no implicit conversion of symbol integer (typeerror)
. know input put in correct doing p options
works. ideas on how fix this?
(note prefer not use .each loop suggested in other answers single values need).
thanks.
you not using optionparser correctly in several ways.
when defining options have provide placeholder actual value of option. if not, interpreted switch, returning either true or false depending on whether switch set.
opts.on('-h', '--host host', 'the host run ssh session with') |host| options[:host] = "#{host}" end
another mistake defined -h
both host , user option. better define different letter each of them, intended have -u
user anyway.
but main problem, 1 causing error treat return value of #parse!
method if return parsed values. return value remainder of argv
array not matched parser. , because try access hash asking element symbol, complains because array elements accessed integer values. fix it, keep options
reference before , don't assign return value it.
argv.parse!
from here on, give further criticism, things recommend should not reason of errors:
additionally might skip default nil values provided in hash in beginning, if ask hash undefined key, provide nil anyway.
options = { host: 'localhost' }
i'd calling #parse!
on the command line argument array argv
, while seems option so, not obvious. i'd recommend saving reference parser variable , call method on parser.
parser = optionparser.new |opts| … end parser.parse!(argv)
if want can call without argument, use argv
default. again make code harder understand in opinion.
and can rid of string conversion of values through interpolation. @ least when getting values command line arguments array argv
can quite sure elements string objects. if intend feed parser other arrays not entirely built string elements, should keep conversion.
opts.on('-h', '--host host', 'the host run ssh session with') |host| options[:host] = host end
also please note there very, widespread convention use of 2 spaces each level of indentation in ruby code. in examples use 4 , 8 spaces, lot of rubyists dislike much. see the ruby styleguide more information.
here fixed-up version of code:
#!/usr/bin/env ruby require 'optionparser' options = { host: 'localhost' } parser = optionparser.new |opts| opts.banner = 'usage: ruby-remote-tailer.rb [options]' opts.on('-h', '--host host', 'the host run ssh session with') |host| options[:host] = host end opts.on('-u', '--user user', 'the user account run session') |user| options[:user] = user end opts.on('-f', '--file file', 'the file run tail on') |file| options[:file] = file end end parser.parse!(argv) puts options[:host]
Comments
Post a Comment