Project

General

Profile

Actions

Feature #15371

closed

IRB with ARGV

Added by svnpenn (Steven Penny) almost 6 years ago. Updated about 2 years ago.

Status:
Closed
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)

Updated by shevegen (Robert A. Heiler) almost 6 years 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) almost 6 years 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) over 5 years 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.

Updated by jeremyevans0 (Jeremy Evans) over 5 years ago

  • Tracker changed from Bug to Feature
  • Backport deleted (2.4: UNKNOWN, 2.5: UNKNOWN)

I think this is a feature request, not a bug report. It does seem like a useful feature, so I've submitted a pull request to irb for it: https://github.com/ruby/irb/pull/22

Updated by hsbt (Hiroshi SHIBATA) almost 5 years ago

  • Status changed from Open to Assigned
  • Assignee set to aycabta (aycabta .)
Actions #6

Updated by jeremyevans (Jeremy Evans) about 2 years ago

  • Status changed from Assigned to Closed

Applied in changeset git|b07db967441161a84386bcbbb41d990a2f3ad31c.


[ruby/irb] Support --noscript option to not use first non-option argument as script

Also add --script option to turn the option back on.

Previously there wasn't a way to get an interactive IRB session
and access arguments provided on the command line.

Additionally, handle - as script as stdin. In Unix-like tools, -
means to take standard input instead of a file. This doesn't
result in exactly the same output for:

echo 'p ARGV' > args.rb; irb args.rb a b c

and

echo 'p ARGV' | irb - a b c

Due to how irb handles whether stdin is a tty.

However, this change allows use of - as a argument, instead of
giving an unrecognized switch error. This required some small
changes to context.rb (to handle - as standard input) and
input-method.rb (to have FileInputMethod accept IO arguments in
addition to strings).

Implements [Feature #15371]

https://github.com/ruby/irb/commit/4192683ba2

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0