简体   繁体   English

我们如何在命令行中使用标记来运行ruby文件?

[英]How can we use flags in command line to run a ruby file?

I want to run a ruby file as ruby sample.rb --flag[yes/no]=yes 我想将ruby文件作为ruby sample.rb --flag[yes/no]=yes

Then in sample.rb I need to get the flag value(yes) and do some operation. 然后在sample.rb中,我需要获取标志值(是)并执行一些操作。

Is this possible? 这可能吗? If it is give me the solution. 如果是的话,请给我解决方案。

If you just want to read command-line-arguments you can access them in ARGV in the ruby program 如果您只想阅读命令行参数,则可以在ruby程序中的ARGV中访问它们

For one argument, use 对于一个参数,使用

flag = ARGV[0]

For many, ARGV is an array 对于许多人来说,ARGV是一个数组

ARGV.each do |a|
  # process arguments like so
  puts "Argument: #{a}"
end

您正在寻找optparse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM