Add ability to forward ARGV to a Pry session when using `bin/pry` (#1902)

* Add ability to forward ARGV to a Pry session when using `bin/pry`

Fixes https://github.com/pry/pry/issues/1901
This commit is contained in:
r-obert 2019-01-13 07:29:12 +01:00 committed by Kyrylo Silin
parent 1d463198a9
commit 5cd65d3c0e
3 changed files with 41 additions and 6 deletions

View File

@ -59,11 +59,17 @@ class Pry
raise NoOptionsError, "No command line options defined! Use Pry::CLI.add_options to add command line options."
end
self.input_args = args
@pass_argv = args.index { |cli_arg| %w[- --].include?(cli_arg) }
if @pass_argv
slop_args = args[0...@pass_argv]
self.input_args = args.replace(args[@pass_argv + 1..-1])
else
self.input_args = slop_args = args
end
begin
opts = Pry::Slop.parse!(
args,
slop_args,
help: true,
multiple_switches: false,
strict: true,
@ -101,7 +107,7 @@ class Pry
context = Pry.toplevel_binding
end
if Pry::CLI.input_args.any? && Pry::CLI.input_args != ["pry"]
if !@pass_argv && Pry::CLI.input_args.any? && Pry::CLI.input_args != ["pry"]
full_name = File.expand_path(Pry::CLI.input_args.first)
Pry.load_file_through_repl(full_name)
exit

View File

@ -1,4 +1,4 @@
describe Pry::Hooks do
RSpec.describe Pry::CLI do
before do
Pry::CLI.reset
end

View File

@ -1,17 +1,46 @@
RSpec.describe 'The bin/pry CLI' do
let(:ruby) { RbConfig.ruby.shellescape }
let(:pry_dir) { File.expand_path(File.join(__FILE__, '../../../lib')).shellescape }
let(:clean_output) do
# Pry will emit silent garbage because of our auto indent feature.
# This lambda cleans the output of that garbage.
->(out) { out.strip.sub("\e[0G", "") }
end
context "ARGV forwarding" do
let(:code) { "p(ARGV) and exit".shellescape }
it "forwards ARGV as an empty array when - is passed without following arguments" do
out = clean_output.call(`#{ruby} -I#{pry_dir} bin/pry -e #{code} -`)
expect(out).to eq([].inspect)
end
it "forwards ARGV as an empty array when -- is passed without following arguments" do
out = clean_output.call(`#{ruby} -I#{pry_dir} bin/pry -e #{code} --`)
expect(out).to eq([].inspect)
end
it "forwards its remaining arguments as ARGV when - is passed" do
out = clean_output.call(`#{ruby} -I#{pry_dir} bin/pry -e #{code} - 1 -foo --bar --baz daz`)
expect(out).to eq(%w[1 -foo --bar --baz daz].inspect)
end
it "forwards its remaining arguments as ARGV when -- is passed" do
out = clean_output.call(`#{ruby} -I#{pry_dir} bin/pry -e #{code} -- 1 -foo --bar --baz daz`)
expect(out).to eq(%w[1 -foo --bar --baz daz].inspect)
end
end
context '-I path' do
it 'adds an additional path to $LOAD_PATH' do
code = 'p($LOAD_PATH) and exit'
out = `#{ruby} -I#{pry_dir} bin/pry -I /added/at/cli -e '#{code}'`
out = clean_output.call(`#{ruby} -I#{pry_dir} bin/pry -I /added/at/cli -e '#{code}'`)
expect(out).to include('/added/at/cli')
end
it 'adds multiple additional paths to $LOAD_PATH' do
code = 'p($LOAD_PATH) and exit'
out = `#{ruby} -I#{pry_dir} bin/pry -I /added-1/at/cli -I /added/at/cli/also -e '#{code}'`
out = clean_output.call(`#{ruby} -I#{pry_dir} bin/pry -I /added-1/at/cli -I /added/at/cli/also -e '#{code}'`)
expect(out).to include('/added-1/at/cli')
expect(out).to include('/added/at/cli/also')
end