docopt.rb
docopt.rb copied to clipboard
Cannot print dictionary items?
This is my simple code: #encoding: UTF-8 #!/usr/bin/env ruby require "net/http" require "docopt" require "uri"
doc=<<DOCOPT
Usage: screengrab (application_id) [-c=(us|pl|de|gb|au)] screengrab -h|--help screengrab --version
Options: -h|--help Show this help --version Show version -c=(us|pl|de|gb|au) Lookup in specific Store.
DOCOPT
begin require "pp" options = Docopt::docopt(doc,version:"1.0") pp options rescue Docopt::Exit => e puts e.message exit 1 end
no matter what I do, command pp options do not produce any dictionary on screen
I tried to run test example by ruby -d and got weird results:
Exception LoadError' at /Users/sebastian/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/rubygems.rb:1388 - cannot load such file -- rubygems/defaults/operating_system Exception LoadError' at /Users/sebastian/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/rubygems.rb:1397 - cannot load such file -- rubygems/defaults/ruby
Exception LoadError' at /Users/sebastian/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59 - cannot load such file -- docopt Exception Docopt::Exit' at /Users/sebastian/.rvm/gems/ruby-2.5.1/gems/docopt-0.6.1/lib/docopt.rb:667 - Docopt::Exit
WTF?
This is something I struggled with but I was able to get around it by using pry.
Sample code: require "docopt" require "pry"
doc = <<DOCOPT
Usage:
#{__FILE__} <country> <state> <city> <street_name>
Options:
-h --help show this help message and exit
DOCOPT
begin require "pp" pp = Docopt::docopt(doc) binding.pry rescue Docopt::Exit => e puts e.message end
The output will be something like this: 19: DOCOPT 20: 21: begin 22: require "pp" 23: pp = Docopt::docopt(doc) => 24: binding.pry 25: rescue Docopt::Exit => e 26: puts e.message 27: end 28: 29: # SETUP for calls.
[1] pry(main)> pp
=> {"<country>"=>"usa",
"<state>"=>"tx",
"<city>"=>"houston",
"<street_name>"=>"Clark county"}