为什么 "bundle exec" 会吃掉我传递的参数?

10

当我使用bundle exec命令时,它会接受我传递的参数。例如:

bundle exec my_command run --verbose

在这种情况下,--verbose被用作捆绑器参数,而应该用于my_command。我知道以下方法可以解决问题:
bundle exec 'my_command run --verbose'

有没有可能避免引号?我使用的命令已经有很多引号了。我期望这样会起作用,但实际上并没有:

bundle exec -- my_command run --verbose

我在bundler的文档中没有看到太多相关内容。如果有任何想法,将不胜感激。

你遇到的问题我没有遇到过,你使用的 bundler 版本是多少? - Shelvacu
我正在使用 Bundler 版本 1.3.5。 - arangamani
2个回答

13

看起来这是在 shell 中将一个命令传递给另一个命令时常见的问题,而你似乎已经接近我会用的方法了。不要使用:

bundle exec my_command run --verbose

或者:
bundle exec -- my_command run --verbose

尝试:

bundle exec my_command -- run --verbose

使用bundle exec --会破坏bundle exec的命令链。 execbundle的子命令,my_commandexec的参数。关于my_command的参数,既不需要bundle也不需要exec知道,所以--放在你想要打破参数链到bundle的地方即可。

例如,要运行特定的ActiveSupport :: TestCase bundle exec ruby -- test/unit/class_test.rb -n '/test_a_method/' - eebbesen

2

bundler源代码来看,默认行为是将bundle exec后的所有参数传递给Kernel.exec,因此--verbose参数将被传递给您的命令,而不是bundle

bundle exec my_command run --verbose

在bundle的上下文环境中运行以下内容。
Kernel.exec('my_command', 'run', '--verbose')

并且。
bundle exec -- my_command run --verbose

由于没有名为--的命令/脚本,因此会导致错误。

请在此处检查测试用例:

#!/usr/bin/env ruby
# coding: utf-8
# file: test.rb

p ARGV

测试:

$ bundle exec ruby test.rb --verbose --arg1
["--verbose", "--arg1"]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接