tj / commander

The complete solution for Ruby command-line executables
http://visionmedia.github.com/commander
MIT License
1.09k stars 64 forks source link

Support for pry #83

Closed KrauseFx closed 9 years ago

KrauseFx commented 9 years ago

When using commander with pry I get the following error when using binding.pry:

before_session hook failed: NoMethodError: undefined method `process_line' for #<Hash:0x007f8aa253cdf0>
.../.rvm/gems/ruby-2.0.0-p481@global/gems/pry-0.10.1/lib/pry/pry_instance.rb:448:in `run_command'

I don't know if it's a commander or a pry issue.

I still get to enter some code, but the results are not displayed.

ggilder commented 9 years ago

I'm not sure, but both gems add a lot of methods to Object so there's definitely potential for conflict. FWIW, you can use Commander's "modular" style to avoid this, and it seems to allow pry to operate normally.

Here's a small example:

require 'commander'
require 'pry'

class MyApplication
  include Commander::Methods

  def run
    program :name, 'Foo Bar'
    program :version, '1.0.0'
    program :description, 'Stupid command that prints foo or bar.'

    command :foo do |c|
      c.action do |args, options|
        binding.pry
        say 'foo'
      end
    end

    run!
  end
end

MyApplication.new.run if $0 == __FILE__
KrauseFx commented 9 years ago

Thanks @ggilder, I tried the modular code from the README:

class MyApplication
  include Commander::Methods

  def run
    program :name, 'Foo Bar'
    program :version, '1.0.0'
    program :description, 'Stupid command that prints foo or bar.'

    command :foo do |c|
      c.syntax = 'foobar foo'
      c.description = 'Displays foo'
      c.action do |args, options|
        say 'foo'
      end
    end

    run!
  end
end

MyApplication.new.run if $0 == __FILE__

and I get the same error:

[1] pry(Fastlane::LaneManager)> puts 'hi'
NoMethodError: undefined method `process_line' for #<Hash:0x007f980546da80>
from /Users/felixkrause/.rvm/gems/ruby-2.0.0-p481@global/gems/pry-0.10.1/lib/pry/pry_instance.rb:404:in `process_command'

Does it work on your computer?

ggilder commented 9 years ago

The example I pasted above does work; can you try running it? I'm guessing there's something extra in what you're running since the script you mention doesn't actually require pry anywhere.

KrauseFx commented 9 years ago

Thanks @ggilder, I found the problem. I've been using

require 'commander/import'

instead of

require 'commander'

Seems like require 'commander/import' should only be used with the classic style.

Summary: Use the modular style and change the require statement to require 'commander'


I'm using commander for my open source project deliver and many other iOS developer tools, and this makes debugging so much easier! Thank you @ggilder!

ggilder commented 9 years ago

Awesome, glad that worked out for you. We do have the modular example using require 'commander' but we should probably call that out more explicitly.

Very cool to see another use of commander in the wild!

toobulkeh commented 9 years ago

Running into this requirement with a thoroughly established gemspec -- github.com/nomad/cupertino. Unfortunately, I don't want to change to this require 'commander' set, so it seems like I'm stuck without pry.

What do you use for writing/creating Gems and you want a simple debugger to start playing around with data structures while developing code? Since I can't use binding.pry I need another solution :(

ggilder commented 9 years ago

@toobulkeh wish I had a good answer for you; I always use pry too. Unfortunately it seems this is not uncommon; see for instance https://github.com/davetron5000/gli/issues/196

I've filed an issue for pry here: https://github.com/pry/pry/issues/1344

You could try the debugger gem: https://github.com/cldwalker/debugger — definitely less features but it might do in a pinch for light debugging needs.

Global namespaces are a pain :(

ralfclaassens commented 9 years ago

@toobulkeh I always use byebug instead: https://github.com/deivid-rodriguez/byebug. Does that help you?

toobulkeh commented 9 years ago

Yeah, I used byebug last night to just move forward. Definitely debugger :+1: for ruby 2.x.

I'm pretty new to non-rails dev, so my workflow is lacking, so thanks for the help!