你能告诉我一个快速的工作片段来解决这个命令行选项的情况吗?(Can you show me a quick working snippet to solve this command line opt

编程入门 行业动态 更新时间:2024-10-23 07:39:03
你能告诉我一个快速的工作片段来解决这个命令行选项的情况吗?(Can you show me a quick working snippet to solve this command line option case?)

我希望能够从命令行运行我的脚本时支持这样的事情:

script.rb -n 2 -t first.txt -t second.txt

我希望能够拿到1个或更多的t开关,但我不知道如何实现这一点。 我想不必这样做:

script.rb -n 2 -tfirst.txt,second.txt

有什么想法?

I'd like to be able to support something like this when running my script from the command line:

script.rb -n 2 -t first.txt -t second.txt

I'd like to be able to take 1 or more t switches, but I have no clue how to achieve this. I would like to not have to do this:

script.rb -n 2 -tfirst.txt,second.txt

Got any ideas?

最满意答案

您可能想要使用OptionParser http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html

会有这样的工作吗?

require 'optparse' files = [] OptionParser.new do |opts| opts.on("-t", "--text-file TEXTFILE","Text file to run against" ) do |text_file_name| files << text_file_name end end.parse! puts files.inspect

You might want to use OptionParser http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html

Would something like this work ?

require 'optparse' files = [] OptionParser.new do |opts| opts.on("-t", "--text-file TEXTFILE","Text file to run against" ) do |text_file_name| files << text_file_name end end.parse! puts files.inspect

更多推荐

本文发布于:2023-07-04 17:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1027023.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:你能   命令行   片段   选项   快速

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!