Given this program foo.rb
:
require "optparse"
options = {delimiter: " "}
OptionParser.new { |op|
op.on("-d", "--delimiter [DELIM]", "String separating columns")
}.parse!(ARGV, into: options)
puts ["a", "b"].join(options[:delimiter])
If invoked with:
$ ruby foo.rb -d "\t"
The "\t"
will be escaped to "\\t"
and therefore print:
a\tb
How would you make OptionParser not do this? Custom conversion seems overkill. Attempting to unescape terminal input sounds fraught.
UPDATE: Here's a graceful enough solution, use Ruby 2.5 String#undump:
require "optparse"
options = {delimiter: " "}
OptionParser.new { |op|
op.on("-d", "--delimiter [DELIM]", "String separating columns") do |val|
"\"#{val}\"".undump
end
}.parse!(ARGV, into: options)
puts ["a", "b"].join(options[:delimiter])