如何在Ruby中访问原始命令行参数字符串? [英] How to access original command-line argument string in Ruby?
问题描述
我正在尝试访问Ruby中的原始命令行参数字符串(即-不使用预分割/分隔的ARGV数组)。有谁知道如何做到这一点?例如:
I'm trying to access the original command line argument string in Ruby (ie - not using the pre-split/separated ARGV array). Does anyone know how to do this? For example:
$> ruby test.rb command "line" arguments
我想知道'line'是否有
I want to be able to tell if 'line' had quotes around it:
"command \"line\" arguments"
有什么提示吗?预先感谢
Any tips? Thanks in advance
推荐答案
据我所知,ruby不会从命令行中删除这些双引号。外壳程序使用它们将内容作为字符串插入并传递给ruby。
As far as I can tell, ruby is not removing those double-quotes from your command line. The shell is using them to interpolate the contents as a string and pass them along to ruby.
您可以得到ruby收到的所有内容,如下所示:
You can get everything that ruby receives like this:
cmd_line = "#{$0} #{ARGV.join( ' ' )}"
为什么需要知道引号是什么?您可以使用其他定界符(例如':'或'#')吗?
Why do you need to know what is in quotes? Can you use some other delimiter (like ':' or '#')?
如果需要,可以通过将转义符转义成红宝石来将它们传递给ruby:
If you need to, you can pass double-quotes to ruby by escaping them:
$> ruby test.rb command "\"line\"" arguments
上面的<$ c $在这种情况下,c> cmd_line 变量将收到以下字符串:
The above cmd_line
variable would receive the following string in that case:
test.rb comand "line" arguments
这篇关于如何在Ruby中访问原始命令行参数字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!