如何传递字符串“*.*”将 ruby 作为命令行参数?
code:
#test_argv.rb
puts "length: #{ARGV.length} "
ARGV.each do |a|
puts "Argument: #{a}"
end
如果我在调用上述代码时提供字符串 "*.*"
(带或不带引号),我会得到以下输出:
C:\test>test_argv *.*
length: 5
Argument: afile.TXT
Argument: bfile.TXT
Argument: cfile.TXT
Argument: dfile.TXT
Argument: somethingelse.TXT
即 c:\test 中的文件列表。
其他值,例如 "s*.*"
返回 Somethingelse.TXT,正如您在执行文件操作时所期望的那样 - 但我没有。
但这的行为正如预期的那样:
C:\test>test_argv asdf
length: 1
Argument: asdf
所以我的问题是,如何制作一个用户友好的脚本,将 "*.*"
(等)作为命令行参数?此外,这在哪里记录/解释?
编辑:这发生在 Windows 和 Linux、1.8.7 和 1.9.2 上
code:
#test_argv.rb
puts "length: #{ARGV.length} "
ARGV.each do |a|
puts "Argument: #{a}"
end
If I supply the string "*.*"
(with or without quotes) when I call the above, I get the following output:
C:\test>test_argv *.*
length: 5
Argument: afile.TXT
Argument: bfile.TXT
Argument: cfile.TXT
Argument: dfile.TXT
Argument: somethingelse.TXT
i.e., a listing of the files in c:\test.
Other values, like "s*.*"
returns somethingelse.TXT, as you'd expect if you were doing file operations -- but I'm not.
But this behaves as would have expected:
C:\test>test_argv asdf
length: 1
Argument: asdf
So my question is, how can I make a user-friendly script that will take "*.*"
(etc) as a command line parameter? In addition, where is this documented/explained?
Edit: this happens on windows and linux, 1.8.7 and 1.9.2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要将其放入文字引号中:
引号应避免过早地扩展命令行参数。
You may need to put this into literal quotes:
The quotes should avoid having the command-line arguments get expanded on you prematurely.