我怎样才能通过 <arguments>如果我不指定 <programfile> 到 IRB? [英] How can I pass <arguments> to IRB if I don't specify <programfile>?

查看:54
本文介绍了我怎样才能通过 <arguments>如果我不指定 <programfile> 到 IRB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为:

irb --help

用法:irb.rb [选项] [程序文件] [参数]

Usage: irb.rb [options] [programfile] [arguments]

我知道如果我包含一个程序文件

例如:

irb test.rb A B C

其中 test.irb 只是p ARGV"

where test.irb is simply "p ARGV"

产生:

["a", "b", "c"]

["a", "b", "c"]

使 programfile 在 DOS 中成为 con...我可以执行以下操作

Making programfile be con in DOS... I can do following

irb con A B C
con(main):001:0> ARGV

产生:

ARGV
=> ["A", "B", "C"]

ARGV
=> ["A", "B", "C"]

但这取决于系统并且具有回显输入的副作用:-(

but this is system dependent and has the side effect of echoing input :-(

我真正喜欢的是类似的东西

What i really like is something like

irb -- a b c

顺便说一句:我知道我可以在 irb 中设置 ARGV,但我的目的是将 special == irb -rSpecialLibrary" 设为别名,因此我可以执行以下操作:

BTW: I know I can set ARGV inside irb but I my intention is to alias special == irb -rSpecialLibrary" so I can do something like:

special A B C
<input goes here>

有什么建议吗?

推荐答案

查看 irb 可执行文件的来源:

Looking at the source of the irb executable:

#!/usr/bin/env ruby
require "irb"

if __FILE__ == $0
  IRB.start(__FILE__)
else
  # check -e option
  if /^-e$/ =~ $0
    IRB.start(__FILE__)
  else
    IRB.setup(__FILE__)
  end
end

IRB 模块的源代码:

The at the source of the IRB module:

# File lib/irb/init.rb, line 15
  def IRB.setup(ap_path)
    IRB.init_config(ap_path)
    IRB.init_error
    IRB.parse_opts
    IRB.run_config
    IRB.load_modules

    unless @CONF[:PROMPT][@CONF[:PROMPT_MODE]]
      IRB.fail(UndefinedPromptMode, @CONF[:PROMPT_MODE])
    end
  end

下至parse_opts,我们的问题方法:

Down to parse_opts, our problem method:

# File lib/irb/init.rb, line 126
  def IRB.parse_opts
    load_path = []
    while opt = ARGV.shift
      case opt
      when "-f"
        @CONF[:RC] = false
      when "-m"
        @CONF[:MATH_MODE] = true
      when "-d"
        $DEBUG = true
      when /^-r(.+)?/
        opt = $1 || ARGV.shift
        @CONF[:LOAD_MODULES].push opt if opt
      when /^-I(.+)?/
        opt = $1 || ARGV.shift
        load_path.concat(opt.split(File::PATH_SEPARATOR)) if opt
      when '-U'
        set_encoding("UTF-8", "UTF-8")
      when /^-E(.+)?/, /^--encoding(?:=(.+))?/
        opt = $1 || ARGV.shift
        set_encoding(*opt.split(':', 2))
      when "--inspect"
        @CONF[:INSPECT_MODE] = true
      when "--noinspect"
        @CONF[:INSPECT_MODE] = false
      when "--readline"
        @CONF[:USE_READLINE] = true
      when "--noreadline"
        @CONF[:USE_READLINE] = false
      when "--echo"
        @CONF[:ECHO] = true
      when "--noecho"
        @CONF[:ECHO] = false
      when "--verbose"
        @CONF[:VERBOSE] = true
      when "--noverbose"
        @CONF[:VERBOSE] = false
      when /^--prompt-mode(?:=(.+))?/, /^--prompt(?:=(.+))?/
        opt = $1 || ARGV.shift
        prompt_mode = opt.upcase.tr("-", "_").intern
        @CONF[:PROMPT_MODE] = prompt_mode
      when "--noprompt"
        @CONF[:PROMPT_MODE] = :NULL
      when "--inf-ruby-mode"
        @CONF[:PROMPT_MODE] = :INF_RUBY
      when "--sample-book-mode", "--simple-prompt"
        @CONF[:PROMPT_MODE] = :SIMPLE
      when "--tracer"
        @CONF[:USE_TRACER] = true
      when /^--back-trace-limit(?:=(.+))?/
        @CONF[:BACK_TRACE_LIMIT] = ($1 || ARGV.shift).to_i
      when /^--context-mode(?:=(.+))?/
        @CONF[:CONTEXT_MODE] = ($1 || ARGV.shift).to_i
      when "--single-irb"
        @CONF[:SINGLE_IRB] = true
      when /^--irb_debug=(?:=(.+))?/
        @CONF[:DEBUG_LEVEL] = ($1 || ARGV.shift).to_i
      when "-v", "--version"
        print IRB.version, "\n"
        exit 0
      when "-h", "--help"
        require "irb/help"
        IRB.print_usage
        exit 0
      when "--"
        if opt = ARGV.shfit
          @CONF[:SCRIPT] = opt
          $0 = opt
        end
        break
      when /^-/
        IRB.fail UnrecognizedSwitch, opt
      else
        @CONF[:SCRIPT] = opt
        $0 = opt
        break
      end
    end
    if RUBY_VERSION >= FEATURE_IOPT_CHANGE_VERSION
      load_path.collect! do |path|
        /\A\.\// =~ path ? path : File.expand_path(path)
      end
    end
    $LOAD_PATH.unshift(*load_path)

  end

将该选项作为脚本名称是硬编码的(@CONF[:SCRIPT] = opt).幸运的是,这是 Ruby.我的第一个想法是使用不同的脚本启动 IRB,首先修改模块.

It is hardcoded to take that option as the script name (@CONF[:SCRIPT] = opt). Luckily, this is Ruby. The first idea I had was using a different script to launch IRB that modifies the module first.

~/bin/custom-irb:

~/bin/custom-irb:

#!/usr/bin/env ruby
require 'irb'
module IRB
  class << self
    # sort of lame way to reset the parts we don't like about
    # parse_opts after it does the parts we do like
    def parse_opts_with_ignoring_script
      arg = ARGV.first
      script = $0
      parse_opts_without_ignoring_script
      @CONF[:SCRIPT] = nil
      $0 = script
      ARGV.unshift arg
    end
    alias_method :parse_opts_without_ignoring_script, :parse_opts
    alias_method :parse_opts, :parse_opts_with_ignoring_script
  end
end

if __FILE__ == $0
  IRB.start(__FILE__)
else
  # check -e option
  if /^-e$/ =~ $0
    IRB.start(__FILE__)
  else
    IRB.setup(__FILE__)
  end
end

您可以使用 custom-irb foo bar baz 启动它,ARGV 将是 ['foo', 'bar', 'baz'].

You can launch this with custom-irb foo bar baz and ARGV will be ['foo', 'bar', 'baz'].

这篇关于我怎样才能通过 &lt;arguments&gt;如果我不指定 &lt;programfile&gt; 到 IRB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆