Ruby 中的 ARGV 有什么意义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13329132/
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
What's the point of ARGV in Ruby?
提问by Jason
What's the point of ARGV in Ruby?
Ruby 中的 ARGV 有什么意义?
first, second, third = ARGV
puts "The script is called: #{ruby ex1.rb
}"
puts "Your first variable is: #{first}"
puts "Your second variable is: #{second}"
puts "Your third variable is: #{third}"
What's the point of this when to run the file I need to do:
什么时候运行我需要做的文件:
ruby ex1.rb blah blah blah
and to put in the first, second and third variables I need to type in
并输入我需要输入的第一个、第二个和第三个变量
user = ARGV.first
prompt = '> '
puts "Hi #{user}, I'm the #{ruby ex1.rb -a -b 3
} script."
puts "I'd like to ask you a few questions."
puts "Do you like me #{user}?"
print prompt
likes = STDIN.gets.chomp()
puts "Where do you live #{user}?"
print prompt
lives = STDIN.gets.chomp()
puts "What kind of computer do you have?"
print prompt
computer = STDIN.gets.chomp()
puts <<MESSAGE
Alright, so you said #{likes} about liking me.
You live in #{lives}. Not sure where that is.
And you have a #{computer} computer. Nice.
MESSAGE
How does this benefit at all the person running the program? They can't do it anyway since I'd assume it be an executable:
这对运行该程序的所有人有何好处?他们无论如何都做不到,因为我认为它是一个可执行文件:
int main(int argc, char **argv)
{
return(0);
}
Can someone please explain this to me?
有人可以向我解释一下吗?
回答by Andrew Marshall
What's the point of
ARGVin Ruby?
ARGVRuby的重点是什么?
ARGV"contains the arguments passed to your script, one per element."
ARGV“包含传递给脚本的参数,每个元素一个。”
What's the point of this when to run the file you need to do:
ruby ex1.rband to put in the first, second and third variables you need to type inruby ex1.rb blah blah blah.
什么时候运行你需要做的文件:
ruby ex1.rb并输入你需要输入的第一个、第二个和第三个变量ruby ex1.rb blah blah blah。
That is the exact point, to be able to specify arguments when running the file like:
这就是在运行文件时能够指定参数的确切点,例如:
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options]"
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
options[:verbose] = v
end
end.parse!
p options
p ARGV
How does this benefit at all the person running the program?
这对运行该程序的所有人有何好处?
Same benefit any other command-line program gets from being able to do so: the user can specify upfront what they want, and it's easier to script than an interactive CLI.
任何其他命令行程序能够这样做的好处相同:用户可以预先指定他们想要的内容,并且比交互式 CLI 更容易编写脚本。
They can't do it anyway since I'd assume it be an executable.
他们无论如何都做不到,因为我认为它是一个可执行文件。
Yes, they can. You just gave an example of exactly how the user would run your program that way. Whether it's executable or not doesn't change anything.
是的他们可以。您刚刚给出了一个示例,说明用户将如何以这种方式运行您的程序。它是否可执行不会改变任何东西。
回答by tadman
ARGVhas a long tradition and comes from the UNIX/POSIX world where most C programs must contain the following:
ARGV具有悠久的传统,来自 UNIX/POSIX 世界,大多数 C 程序必须包含以下内容:
test_script --live "New York" --computer "Lenovo" --like
There argcrepresents the number of arguments supplied, and argvis a low-level pointer to the first of potentially a number of string pointers. The name argvhas stuck around in various forms.
这里argc表示提供的参数数量,并且argv是一个指向潜在多个字符串指针中第一个的低级指针。这个名字argv以各种形式存在。
Command-line arguments are very popular with developers, though they're usually not used quite as you seem to think. Un-named arguments are much harder to deal with. That's why things like optparseexist to help deal with them.
命令行参数在开发人员中非常流行,尽管它们通常不像您想象的那样使用。未命名的参数更难处理。这就是为什么optparse存在这样的东西来帮助处理它们。
Here's an example from the OptionParser documentation:
这是OptionParser 文档中的一个示例:
$ rails new my_app
$ rake tests
$ gem install rails --no-rdoc --no-ri
$ bundle update
This allows you to define much more usable command-line arguments and then use them. For your example, I'd expect it to work like this:
这允许您定义更多可用的命令行参数,然后使用它们。对于您的示例,我希望它像这样工作:
puts "Email of Account"
account_email = gets.chomp
puts "Enter Password"
password = gets.chomp
# code that logs me in
That makes it quite obvious what the arguments are because they're named.
这使得参数是什么就很明显了,因为它们被命名了。
回答by Gregory Brown
Consider some Ruby command line utilities like rails, rake,gem, or bundle. While none of these are end-user applications (like web apps or GUI apps), they are still programs written in Ruby that users interact with. All of them take arguments:
考虑到一些Ruby的命令行实用程序一样rails,rake,gem,或bundle。虽然这些都不是最终用户应用程序(如 Web 应用程序或 GUI 应用程序),但它们仍然是用户与之交互的用 Ruby 编写的程序。他们都接受论据:
account_email, password = ARGV
# code that logs me in
It is possible to use ARGVto implement these kinds of command line programs that accept arguments. While we'll often use the OptionParser standard libraryor some other tool for this purpose, ARGV is the low level collection those tools are built on top of.
可以用来ARGV实现这些接受参数的命令行程序。虽然我们经常为此使用OptionParser 标准库或其他一些工具,但 ARGV 是这些工具构建在其之上的低级集合。
So if you've ever run gem, rails, rake, or bundle, or any command line developer tool that is written in Ruby, you will have benefited from ARGV!
因此,如果您曾经运行过gem、rails、rake、 或bundle或任何用 Ruby 编写的命令行开发人员工具,那么您将从中受益ARGV!
回答by Gary
The point of ARGVis that it enables scripts to be able to run without human input, and also allows for the case where the input may vary from one iteration to the next.
关键ARGV是它使脚本能够在没有人工输入的情况下运行,并且还允许输入在一次迭代与下一次迭代之间可能有所不同的情况。
For example you may be running three different scripts to get three values. These values could then be passed into this script and ARGVcan be used to get the values of these options.
例如,您可能正在运行三个不同的脚本来获取三个值。然后可以将这些值传递到此脚本中,ARGV并可用于获取这些选项的值。
回答by John Escobedo
I used to run a script that asked me for an email and password. It was used to log into one of 100's of accounts. Like most Ruby scripts, I'd run it from the command line. This had to semblance to being an "executable", especially not one with an icon that you click.
我曾经运行一个脚本,要求我提供电子邮件和密码。它用于登录 100 个帐户之一。像大多数 Ruby 脚本一样,我会从命令行运行它。这必须看起来像是一个“可执行文件”,特别是不是一个带有您单击的图标的文件。
I'd run the program and it would ask me for email and password then log me in using Watir.
我会运行该程序,它会要求我提供电子邮件和密码,然后使用 Watir 登录。
$ ruby my_script.rb [email protected] mypassword
However, I found that taking in those values via ARGV was much faster for my purposes.
但是,我发现通过 ARGV 获取这些值对我的目的来说要快得多。
ruby my_script.rb [email protected] mypassword
ruby my_script.rb [email protected] otherpassword
Then I could just type
然后我就可以打字
ruby my_script.rb option1 option2 option3 option4
and hit enter. Also I could just press the up arrow in the console to run it again and again or recent versions with different names and passwords to test my script quickly.
并按回车键。此外,我可以在控制台中按向上箭头来一次又一次地运行它,或者使用不同的名称和密码来快速测试我的脚本。
Taking it a step further, I made a shell script that could run this scripts, args and all in many combinations
更进一步,我制作了一个 shell 脚本,可以运行这些脚本、参数以及所有组合
##代码##Finally, sometimes I needed to set dozens of settings per each time I ran a script, and then run that script many times. So I could also easily insert a dozens variables into a script from the command line
最后,有时我每次运行脚本时都需要设置几十个设置,然后多次运行该脚本。所以我也可以很容易地从命令行插入几十个变量到脚本中
##代码##I'm very new to using ARGV and hope to learn better ways to use it, but this is one feature I found very useful.
我对使用 ARGV 非常陌生,并希望学习更好的使用方法,但这是我发现非常有用的一项功能。

