I have an method that takes an string option containing whitespace:
desc 'events', 'List events'
method_option :since, :desc => 'Show events since', :default => "2 years ago"
def events
# ...
end
but it seems that the parameters are naively split on whitespace so I get this error:
$ example events --since="1 hour ago"
ERROR: "example events" was called with arguments ["hour", "ago"]
Usage: "example events"
If I change the type to array I can get it to accept the full value, but that's not exactly what I'm after.
Any advice would be appreciated.
Edit
@mrlew's answer demonstrated that my error wasn't a Thor issue, and it made me to go back and check my assumptions. I'm using the Thor CLI rather than Thor modules, and in my wrapper script I wasn't handling args correctly. I just needed to wrap ${@}
in double quotes, as below, and now everything works as expected.
#!/usr/bin/env bash
ruby -Ilib ./exe/example "${@}"
Thanks @mrlew :)
I don't know thor gem very well, but this code just might work:
class Example < Thor
desc 'events', 'List events'
method_option :since, desc: 'Show events since', default: "2 years ago"
def events
puts "since: " + options[:since]
end
end
Some points:
desc
first parameter must be the name of the method called. In your
example, it was different.options[:key]
to retrieve the argument value.It works here:
$thor example:events
since: 2 years ago
$ thor example:events --since="long time ago"
since: long time ago