如何获得在Ruby中使用提示的命令名称? [英] How can I get the name of the command called for usage prompts in Ruby?

查看:85
本文介绍了如何获得在Ruby中使用提示的命令名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个很好的小Ruby脚本,而我很喜欢。我想通过检查正确数量的参数来提高其鲁棒性:

I wrote a nice little Ruby script a while back that I'm rather fond of. I'd like to improve its robustness by checking for the proper number of arguments:

if ARGV.length != 2 then
  puts "Usage: <command> arg1 arg2"
end

这是伪代码。无论如何,在C或C ++中,我可以使用 argv [0] 获取用户用于获取我的命令的名称,无论他们是否像 ./ myScript.rb myScript.rb /usr/local/bin/myScript.rb 。在Ruby中,我知道 ARGV [0] 是第一个真正的参数, ARGV 不包含命令名称。

Of course that's pseudocode. Anyways, in C or C++ I could use argv[0] to get the name that the user used to get to my command, whether they called it like ./myScript.rb or myScript.rb or /usr/local/bin/myScript.rb. In Ruby, I know that ARGV[0] is the first true argument, and ARGV does not contain the command name. Is there any way that I can get this?

推荐答案

Ruby有三种方法给我们指定被调用脚本的名称: / p>

Ruby has three ways of giving us the name of the called script:

#!/usr/bin/env ruby

puts "$0            : #{$0}"
puts "__FILE__      : #{__FILE__}"
puts "$PROGRAM_NAME : #{$PROGRAM_NAME}"


b $ b

将代码保存为test.rb并调用它几种方式显示,脚本接收到它被操作系统传递给它的名称。一个脚本只知道操作系统所说的:

Saving that code as "test.rb" and calling it a couple ways shows that the script receives the name as it was passed to it by the OS. A script only knows what the OS tells it:

$ ./test.rb 
$0            : ./test.rb
__FILE__      : ./test.rb
$PROGRAM_NAME : ./test.rb

$ ~/Desktop/test.rb 
$0            : /Users/ttm/Desktop/test.rb
__FILE__      : /Users/ttm/Desktop/test.rb
$PROGRAM_NAME : /Users/ttm/Desktop/test.rb

$ /Users/ttm/Desktop/test.rb 
$0            : /Users/ttm/Desktop/test.rb
__FILE__      : /Users/ttm/Desktop/test.rb
$PROGRAM_NAME : /Users/ttm/Desktop/test.rb

使用 $ HOME的快捷方式显示第二个示例中的操作系统将其替换为展开的路径,与第三个示例中的匹配。在所有情况下,它都是操作系统传递的。

Calling it using the ~ shortcut for $HOME in the second example shows the OS replacing it with the expanded path, matching what is in the third example. In all cases it's what the OS passed in.

使用硬链接和软链接链接到文件显示一致的行为。我为test1.rb创建了一个硬链接,并为test2.rb创建了一个软链接:

Linking to the file using both hard and soft links shows consistent behavior. I created a hard link for test1.rb and a soft link for test2.rb:

$ ./test1.rb 
$0            : ./test1.rb
__FILE__      : ./test1.rb
$PROGRAM_NAME : ./test1.rb

$ ./test2.rb 
$0            : ./test2.rb
__FILE__      : ./test2.rb
$PROGRAM_NAME : ./test2.rb

使用脚本名称上的任何变体启动 ruby​​ test.rb 会返回一致的结果。

Launching ruby test.rb with any of the variations on the script name returns consistent results.

如果你只想调用文件名,你可以使用文件的 basename 方法与其中一个变量或分割在分隔符,并采取最后一个元素。

If you only want the called filename, you can use File's basename method with one of the variables or split on the delimiter and take the last element.

$ 0 __ FILE __

puts File.basename($0)






小增:


Minor addition:

使用 File.basename File.extname File.dirname 套件方法。 basename 使用可选参数,它是strip的扩展名,因此如果您只需要不带扩展名的基本名

There are some benefits to using the File.basename,File.extname and File.dirname suite of methods. basename takes an optional parameter, which is the extension to strip, so if you need just the basename without the extension

File.basename($0, File.extname($0)) 

无需重新发明轮子或不必处理可变长度或缺少的扩展或可能不正确地截断扩展链 .rb.txt 例如:

does it without reinventing the wheel or having to deal with variable-length or missing extensions or the possibility of incorrectly truncating extension chains ".rb.txt" for instance:

ruby-1.9.2-p136 :004 > filename = '/path/to/file/name.ext'
 => "/path/to/file/name.ext" 
ruby-1.9.2-p136 :005 > File.basename(filename, File.extname(filename))
 => "name" 
ruby-1.9.2-p136 :006 > filename = '/path/to/file/name.ext' << '.txt'
 => "/path/to/file/name.ext.txt" 
ruby-1.9.2-p136 :007 > File.basename(filename, File.extname(filename))
 => "name.ext" 

这篇关于如何获得在Ruby中使用提示的命令名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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