Project

General

Profile

Bug #15371

IRB with ARGV

Added by svnpenn (Steven Penny) 7 months ago. Updated 3 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:90239]

Description

PHP allows you to pass ARGV in interactive mode:

$ php -a -- alpha beta gamma
php > print $argv[3] . PHP_EOL;
gamma

and Python offers 4 ways:

$ python3 - alpha beta gamma
>>> import sys
>>> print(sys.argv[3])
gamma

$ python3 -- - alpha beta gamma
>>> import sys
>>> print(sys.argv[3])
gamma

$ python3 -i - alpha beta gamma
>>> import sys
>>> print(sys.argv[3])
gamma

$ python3 -i -- - alpha beta gamma
>>> import sys
>>> print(sys.argv[3])
gamma

However IRB seems to have no way to accomplish this:

$ irb - alpha beta gamma
/usr/share/ruby/2.3.0/irb/init.rb:213:in `parse_opts':
Unrecognized switch: - (IRB::UnrecognizedSwitch)

$ irb -- alpha beta gamma
/usr/share/ruby/2.3.0/irb/magic-file.rb:8:in `initialize': No such file or
directory @ rb_sysopen - alpha (Errno::ENOENT)

$ irb -- - alpha beta gamma
/usr/share/ruby/2.3.0/irb/magic-file.rb:8:in `initialize': No such file or
directory @ rb_sysopen - - (Errno::ENOENT)

$ irb - -- alpha beta gamma
/usr/share/ruby/2.3.0/irb/init.rb:213:in `parse_opts':
Unrecognized switch: - (IRB::UnrecognizedSwitch)

History

Updated by shevegen (Robert A. Heiler) 7 months ago

I can not say whether the above is a bug or not; however had a grep
shows that there are two toplevel methods for IRB to make use
of ARGV, both in init.rb:

init.rb:  def IRB.setup(ap_path, argv: ::ARGV)
init.rb:  def IRB.parse_opts(argv: ::ARGV)

By the way, I find it slightly amusing that python's "there is only
one way" became "there are four ways". ;-)

Updated by svnpenn (Steven Penny) 7 months ago

shevegen (Robert A. Heiler) wrote:

By the way, I find it slightly amusing that python's "there is only
one way" became "there are four ways". ;-)

Its actually only 2 ways if you want a true REPL:

$ python3 -i - alpha beta gamma
>>> import sys
>>> sys.argv[3]
'gamma'

$ python3 -i -- - alpha beta gamma
>>> import sys
>>> sys.argv[3]
'gamma'

Further, looks like PRY has the same issue as RUBY:

http://github.com/pry/pry/issues/1901

Workaround is to use RIPL:

$ ripl - alpha beta gamma
>> ARGV[3]
=> "gamma"

$ ripl -- alpha beta gamma
>> ARGV[3]
=> "gamma"

http://github.com/cldwalker/ripl

Updated by onlynone (Steven Willis) 3 months ago

svnpenn (Steven Penny) wrote:

shevegen (Robert A. Heiler) wrote:

By the way, I find it slightly amusing that python's "there is only
one way" became "there are four ways". ;-)

Its actually only 2 ways if you want a true REPL:

It's actually only 1 way. The -- is just a command line option/argument parsing convention to mark an explicit break between options and arguments. It's useful when one of your arguments might look like an option. Like if you're trying to remove a file called -i, you can write rm -- -i and the -i will be treated as a filename argument and not the -i option to ask for confirmation before deletion. It's not a python thing.

Also available in: Atom PDF