While trying to use Pry (version 0.9.6.2) in a script, I ran into some issues getting to do multiple commands from the command line. The first part was seeing if it could do multiple commands in the first place. It's not obvious, but you can. Since all it does to the -e/--exec option is put a new line at the end and pass it to the initializer of a StringIO object, it means that I should be able to insert a new line in the option and it should perform multiple commands no problem. So I tried the following command against my Rails 2.3 project:
pry -r 'config/environment' --context Class --exec 'show-method method\n exit-all'
After this command, I ran into my first issue, undefined method or variable Class. As I found out that's because it tries to get the binding for the initial context before it loads any of the required files.
Ok, so we can put the context into the exec option:
pry -r 'config/environment' --exec 'cd Class\nshow-method method\nexit-all'
This seems like it should work, but then I receive the following error:
Bad object path: Class\nshow-method method\nexit-all
. Failed trying to resolve: Class\nshow-method method\nexit-all
Why is that happening? the cd command should only receive Class not the rest of the string. Why is that? Well it turns out that what you put in the command line and what the StringIO is initialized with are two different things:
In: 'cd Class\nshow-method method\nexit-all'
At StringIO.new: 'cd Class\\nshow-method method\\nexit-all'
This alteration is causing the StringIO not to read the separate commands.
I altered bin/pry in the gem to perform a gsub!(/\\n/, "\n") on the exec option (variable exec_string) before Pry.start is called at the end of the file.
Upon further investigation, this double slash issue is coming up as the command line arguments are being put into ARGV.
Results of ARGV.inspect
["-r", "config/environment", "--exec", "cd Class\\nshow-method method\\nexit-all"]
Being not satisfied with my hack solution, I want to look into Slop, which is what pry uses to process define and process options, to see what can be done to enter in multiple commands a different way. I would like to find a way to do multiple commands from the command line in a cleaner format rather than having to stuff new lines into the string argument.