Ruby 命令行选项

关于示例

这里的一些示例使用了命令行选项 -e,它将要执行的 Ruby 代码传递给命令行本身

$ ruby -e 'puts "Hello, World."'

这里的一些示例假设文件 desiderata.txt 存在

$ cat desiderata.txt
Go placidly amid the noise and the haste,
and remember what peace there may be in silence.
As far as possible, without surrender,
be on good terms with all persons.

选项

-0: 设置 $/ (输入记录分隔符)

选项 -0 为调用的 Ruby 程序定义输入记录分隔符 $/

该选项的可选参数必须是八进制数字,每个数字的范围为 0..7;这些数字前缀为数字 0 以形成八进制值。

如果未给出任何参数,则输入记录分隔符为 0x00

如果给出了参数,它必须紧跟在选项之后(没有中间的空格或等号字符 '=');参数值

示例

$ ruby -0 -e 'p $/'
"\x00"
ruby -00 -e 'p $/'
""
$ ruby -012 -e 'p $/'
"\n"
$ ruby -015 -e 'p $/'
"\r"
$ ruby -0377 -e 'p $/'
"\xFF"
$ ruby -0400 -e 'p $/'
nil

另请参阅

-a: 将输入行分割为字段

当选项 -a 与选项 -n-p 一起给出时,它会将 $_ 处的字符串分割为 $F 处的字符串数组

$ ruby -an -e 'p $F' desiderata.txt
["Go", "placidly", "amid", "the", "noise", "and", "the", "haste,"]
["and", "remember", "what", "peace", "there", "may", "be", "in", "silence."]
["As", "far", "as", "possible,", "without", "surrender,"]
["be", "on", "good", "terms", "with", "all", "persons."]

对于分割,默认记录分隔符为 $/,默认字段分隔符为 $;

另请参阅

-c: 检查语法

选项 -c 指定应检查指定的 Ruby 程序的语法,但实际上不执行

$ ruby -e 'puts "Foo"'
Foo
$ ruby -c -e 'puts "Foo"'
Syntax OK

-C: 设置工作目录

选项 -C 的参数指定被调用的 Ruby 程序的工作目录;不会更改当前进程的工作目录

$ basename `pwd`
ruby
$ ruby -C lib -e 'puts File.basename(Dir.pwd)'
lib
$ basename `pwd`
ruby

选项及其参数之间的空格可以省略。

-d: 设置 $DEBUGtrue

Ruby 程序中(或由其调用的)一些代码可能包含由全局变量 $DEBUG (例如,if $DEBUG) 条件化的语句或块;这些通常写入 $stdout$stderr

$DEBUG 的默认值为 false;选项 -d 将其设置为 true

$ ruby -e 'p $DEBUG'
false
$ ruby -d -e 'p $DEBUG'
true

选项 --debug 是选项 -d 的别名。

-e: 执行给定的 Ruby 代码

选项 -e 需要一个参数,它是要执行的 Ruby 代码;该选项可以多次给出

$ ruby -e 'puts "Foo"' -e 'puts "Bar"'
Foo
Bar

选项及其参数之间的空格可以省略。

该命令可以包括其他选项,但不应包括参数(如果给定,则忽略)。

-E: 设置默认编码

选项 -E 需要一个参数,该参数指定调用的 Ruby 程序的默认外部编码,或者同时指定默认的外部和内部编码

# No option -E.
$ ruby -e 'p [Encoding::default_external, Encoding::default_internal]'
[#<Encoding:UTF-8>, nil]
# Option -E with default external encoding.
$ ruby -E cesu-8 -e 'p [Encoding::default_external, Encoding::default_internal]'
[#<Encoding:CESU-8>, nil]
# Option -E with default external and internal encodings.
$ ruby -E utf-8:cesu-8 -e 'p [Encoding::default_external, Encoding::default_internal]'
[#<Encoding:UTF-8>, #<Encoding:CESU-8>]

选项及其参数之间的空格可以省略。

另请参阅

选项 --encoding 是选项 -E 的别名。

-F: 设置输入字段分隔符

选项 -F,当与选项 -a 一起给出时,指定其参数是要用于分割的输入字段分隔符

$ ruby -an -Fs -e 'p $F' desiderata.txt
["Go placidly amid the noi", "e and the ha", "te,\n"]
["and remember what peace there may be in ", "ilence.\n"]
["A", " far a", " po", "", "ible, without ", "urrender,\n"]
["be on good term", " with all per", "on", ".\n"]

该参数可以是正则表达式

$ ruby -an -F'[.,]\s*' -e 'p $F' desiderata.txt
["Go placidly amid the noise and the haste"]
["and remember what peace there may be in silence"]
["As far as possible", "without surrender"]
["be on good terms with all persons"]

该参数必须紧跟在选项之后(没有中间的空格或等号字符 '=')。

另请参阅

-h: 打印简短的帮助信息

选项 -h 打印一个简短的帮助信息,其中包括单连字符选项(例如 -I),并且主要省略双连字符选项(例如 --version)。

参数和其他选项将被忽略。

要获得更长的帮助信息,请使用选项 --help

-i: 设置 ARGF 就地模式

选项 -i 为调用的 Ruby 程序设置 ARGF 就地模式;请参阅 ARGF#inplace_mode=

$ ruby -e 'p ARGF.inplace_mode'
nil
$ ruby -i -e 'p ARGF.inplace_mode'
""
$ ruby -i.bak -e 'p ARGF.inplace_mode'
".bak"

-I: 添加到 $LOAD_PATH

选项 -I 的参数指定要添加到全局变量 $LOAD_PATH 中的数组中的目录;该选项可以多次给出

$ pushd /tmp
$ ruby -e 'p $LOAD_PATH.size'
8
$ ruby -I my_lib -I some_lib -e 'p $LOAD_PATH.size'
10
$ ruby -I my_lib -I some_lib -e 'p $LOAD_PATH.take(2)'
["/tmp/my_lib", "/tmp/some_lib"]
$ popd

选项及其参数之间的空格可以省略。

-l: 设置输出记录分隔符;删除行尾

当选项 -l 与选项 -n-p 一起给出时,它会通过以下方式修改行尾处理

不使用选项 -l (未删除)

$ ruby -n -e 'p $_' desiderata.txt
"Go placidly amid the noise and the haste,\n"
"and remember what peace there may be in silence.\n"
"As far as possible, without surrender,\n"
"be on good terms with all persons.\n"

使用选项 '-l' (已删除)

$ ruby -ln -e 'p $_' desiderata.txt
"Go placidly amid the noise and the haste,"
"and remember what peace there may be in silence."
"As far as possible, without surrender,"
"be on good terms with all persons."

另请参阅

-n: 在 gets 循环中运行程序

选项 -nKernel#gets 循环中运行您的程序

while gets
  # Your Ruby code.
end

请注意,gets 读取下一行并将全局变量 $_ 设置为最后读取的行

$ ruby -n -e 'puts $_' desiderata.txt
Go placidly amid the noise and the haste,
and remember what peace there may be in silence.
As far as possible, without surrender,
be on good terms with all persons.

另请参阅

-p: -n,带打印

选项 -p 类似于选项 -n,但也会打印每一行

$ ruby -p -e 'puts $_.size' desiderata.txt
42
Go placidly amid the noise and the haste,
49
and remember what peace there may be in silence.
39
As far as possible, without surrender,
35
be on good terms with all persons.

另请参阅

-r: 引入库

选项 -r 的参数指定在执行 Ruby 程序之前要引入的库;该选项可以多次给出

$ ruby -e 'p defined?(JSON); p defined?(CSV)'
nil
nil
$ ruby -r CSV -r JSON -e 'p defined?(JSON); p defined?(CSV)'
"constant"
"constant"

选项及其参数之间的空格可以省略。

-s: 定义全局变量

选项 -s 指定“自定义选项”是在调用的 Ruby 程序中定义一个全局变量

可以给出多个自定义选项

$ cat t.rb
p [$foo, $bar]
$ ruby t.rb
[nil, nil]
$ ruby -s t.rb -foo=baz
["baz", nil]
$ ruby -s t.rb -foo
[true, nil]
$ ruby -s t.rb -foo=baz -bar=bat
["baz", "bat"]

该选项不能与 选项 -e 一起使用

-S: 在 ENV['PATH'] 中搜索目录

选项 -S 指定 Ruby 解释器(如有必要)搜索其路径位于程序的 PATH 环境变量中的目录;该程序在 shell 的当前工作目录中执行(不一定在找到该程序的目录中)。

此示例使用将路径 'tmp/' 添加到 PATH 环境变量

$ export PATH=/tmp:$PATH
$ echo "puts File.basename(Dir.pwd)" > /tmp/t.rb
$ ruby -S t.rb
ruby

-v: 打印版本;设置 $VERBOSE

选项 -v 打印 Ruby 版本并设置全局变量 $VERBOSE

$ ruby -e 'p $VERBOSE'
false
$ ruby -v -e 'p $VERBOSE'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x64-mingw-ucrt]
true

-w: -W1 的同义词

选项 -w(小写字母)等同于选项 -W1(大写字母)。

-W: 设置警告策略

任何 Ruby 代码都可以通过调用 Kernel#warn 方法来创建警告消息;Ruby 核心库和标准库中的方法也可以创建警告消息。此类消息可能会打印到 $stderr 上(或者不打印,具体取决于某些设置)。

选项 -W 通过设置全局变量 $-W 的初始值,帮助确定是否写入特定的警告消息。

$-W 的值反过来决定哪些警告消息(如果有)将被打印到 $stdout(参见 Kernel#warn)。

$ ruby -W1 -e 'p $foo'
nil
$ ruby -W2 -e 'p $foo'
-e:1: warning: global variable '$foo' not initialized
nil

Ruby 代码也可以为某些类别定义警告;以下是已定义类别的默认设置。

Warning[:experimental] # => true
Warning[:deprecated]   # => false
Warning[:performance]  # => false

它们也可以被设置。

Warning[:experimental] = false
Warning[:deprecated]   = true
Warning[:performance]  = true

您可以通过在类别名称前添加 no- 前缀来禁止该类别。

$ ruby -W:no-experimental -e 'p IO::Buffer.new'
#<IO::Buffer>

-x: 执行在文本中找到的 Ruby 代码

选项 -x 执行一个 Ruby 程序,其代码嵌入在其他非代码文本中。

Ruby 代码

示例

$ cat t.txt
Leading garbage.
#!ruby
puts File.basename(Dir.pwd)
__END__
Trailing garbage.

$ ruby -x t.txt
ruby

可选参数指定要查找文本文件的目录;Ruby 代码在该目录中执行。

$ cp t.txt /tmp/
$ ruby -x/tmp t.txt
tmp
$

如果给出了参数,它必须紧跟在选项之后(没有中间的空格或等号字符 '=')。

--backtrace-limit: 设置回溯限制

选项 --backtrace-limit 设置回溯中显示的条目数量的限制。

参见 Thread::Backtrace.limit

选项 --copyright 打印版权信息。

$ ruby --copyright
ruby - Copyright (C) 1993-2024 Yukihiro Matsumoto

--debug: -d 的别名

选项 --debug选项 -d 的别名。

--disable: 禁用功能

选项 --disable 指定要禁用的功能;参数是要禁用的功能的逗号分隔列表。

ruby --disable=gems,rubyopt t.rb

支持的功能

另请参见 选项 –enable

--dump: 转储项目

选项 --dump 指定要转储的项目;参数是项目的逗号分隔列表。

某些参数值会导致命令的行为就像给定了不同的选项。

有关其他参数值和示例,请参见 选项 –dump

--enable: 启用功能

选项 --enable 指定要启用的功能;参数是要启用的功能的逗号分隔列表。

ruby --enable=gems,rubyopt t.rb

有关功能的信息,请参见 选项 –disable

--encoding: -E 的别名。

选项 --encoding选项 -E 的别名。

--external-encoding: 设置默认外部编码

选项 --external-encoding 设置调用的 Ruby 程序的默认外部编码;有关 encoding 的值,请参见 编码:名称和别名

$ ruby -e 'puts Encoding::default_external'
UTF-8
$ ruby --external-encoding=cesu-8 -e 'puts Encoding::default_external'
CESU-8

--help: 打印帮助消息

选项 --help 打印长帮助消息。

参数和其他选项将被忽略。

对于较短的帮助消息,请使用选项 -h

--internal-encoding: 设置默认内部编码

选项 --internal-encoding 设置调用的 Ruby 程序的默认内部编码;有关 encoding 的值,请参见 编码:名称和别名

$ ruby -e 'puts Encoding::default_internal.nil?'
true
$ ruby --internal-encoding=cesu-8 -e 'puts Encoding::default_internal'
CESU-8

--verbose: 设置 $VERBOSE

选项 --verbose 将全局变量 $VERBOSE 设置为 true,并禁用来自 $stdin 的输入。

--version: 打印 Ruby 版本

选项 --version 打印 Ruby 解释器的版本,然后退出。

实验性选项

这些选项在当前 Ruby 版本中是实验性的,可能会在以后的版本中进行修改或撤销。

--jit

选项 -jit 使用默认选项启用 JIT 编译。

--jit-debug

选项 --jit-debug 启用 JIT 调试(非常慢);如果给定,则添加编译器标志。

--jit-max-cache=num

选项 --jit-max-cache=num 设置要在缓存中进行 JIT 编译的方法的最大数量;默认值:100)。

--jit-min-calls=num

选项 jit-min-calls=num 设置触发 JIT 的最小调用次数(用于测试);默认值:10000)。

--jit-save-temps

选项 --jit-save-temps 将 JIT 临时文件保存在 $TMP 或 /tmp 中(用于测试)。

--jit-verbose

选项 --jit-verbose 将级别 num 或更低的 JIT 日志打印到 $stderr;默认值:0。

--jit-wait

选项 --jit-wait 每次都等待 JIT 编译完成(用于测试)。

--jit-warnings

选项 --jit-warnings 启用 JIT 警告的打印。