Ruby 如果没有给出 ARGV
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14609809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby IF no ARGV given
提问by Ba7a7chy
This is my code:
这是我的代码:
if ARGV[0] == false
puts "Usage: ./script.rb argument"
exit
end
print "Yey we got an argument: " ARGV[0]
But I just cant make the code check if ARGV[0] is given or not, how should I do that ?
但是我不能让代码检查是否给出了 ARGV[0],我该怎么做?
回答by Dave Newton
Check if it's empty?(or check its length):
检查它是否empty?(或检查其长度):
if ARGV.empty?
puts ...
exit
end
Consider using any of the Ruby command line argument parsers, like OptionParser.
考虑使用任何 Ruby 命令行参数解析器,例如OptionParser。
回答by Wayne Conrad
The simplest way to process positional arguments (other than using a gem to do it) is to shiftthem off, one at a time:
处理位置参数的最简单方法(而不是使用 gem 来完成)是将它们移开,一次一个:
arg1 = ARGV.shift
arg2 = ARGV.shift
A missing argument will be nil. Let's exploit that to give arg2 a default value:
缺少的参数将为零。让我们利用它给 arg2 一个默认值:
arg1 = ARGV.shift
arg2 = ARGV.shift || 'default value goes here'
Checking for a required argument is trivial:
检查必需的参数是微不足道的:
raise "missing argument" unless arg1
It's also easy to see if too many arguments have been supplied:
也很容易查看是否提供了太多参数:
raise "too many arguments" unless ARGV.empty?
回答by Marten Veldthuis
I realize a good solution is already given, but nobody mentioned the real reason why your example didn't work.
我意识到已经给出了一个很好的解决方案,但是没有人提到您的示例不起作用的真正原因。
The issue with your example code is that if there are no arguments given, ARGV[0]will return nil, and nil == falseis false. nilis "falsy", but not equal to false. What you could have done is:
您的示例代码的问题是,如果没有给出参数,ARGV[0]将返回nil,并且nil == false为假。nil是“假”,但不等于假。你可以做的是:
unless ARGV[0]
puts "Usage: ..."
exit 1
end
Which would have worked, because this statement now depends on falsyness, rather than on equality to the actual false.
哪个会奏效,因为这个语句现在取决于虚假性,而不是与实际false.
But don't use this code, it's far more clear if you state your actual intent, which is that you want to know if there are any arguments (instead of whether the first argument is falsy). So use the code others suggested:
但是不要使用这段代码,如果您陈述您的实际意图,那就更清楚了,即您想知道是否有任何参数(而不是第一个参数是否为假)。所以使用其他人建议的代码:
if ARGV.empty?
puts "Usage..."
exit 1
end
回答by Harichandan Pulagam
To check if ARGV[0] (or more args) is given or not:
要检查是否给出了 ARGV[0](或更多参数):
puts 'Usage: ...' if ARGV.length == 0
You could also use ARGV.empty? (@DaveNewton's answer)
你也可以使用 ARGV.empty?(@DaveNewton 的回答)
回答by rld
I do this and work's:
我这样做并工作:
url = ARGV[0]
if url.nil?
puts "The shell script needs the arguments."
exit
end
回答by Sergio Tulentsev
You can check the length of ARGV. If it has zero elements then ARGV[0]wasn't given.
您可以检查ARGV. 如果它有零个元素,则ARGV[0]没有给出。
回答by KomodoDave
Here's what I do:
这是我所做的:
a = 'defaultvalue' if (a = ARGV.shift).nil?
Someone else suggested this:
其他人建议这样做:
a = ARGV.shift || 'defaultvalue'
This is wrong, since any false value will cause defaultvalueto be assigned, e.g.:
这是错误的,因为任何错误的值都会导致defaultvalue被赋值,例如:
args=[false]; a=args.shift||true; # a has final value true when it should be false

