帮助我为我的小脚本找到可行的选择

发布于 2024-11-03 00:09:43 字数 830 浏览 0 评论 0原文

我试图让我的脚本根据选项做不同的事情。但是...我根本不了解 ruby​​。我什至无法告诉你数组到底是什么。这就是我得到的:

require 'optparse'
require 'pp'

# the options eventually get put here
options = {}

optparse = OptionParser.new do|opts|

# the help info
opts.banner = "Usage: script.rb [options] input-file output-file"

# This sets the default of 'flag' to 'false' and says it should be
# 'true' if the '-f' option is present
options[:flag] = false
  opts.on( '-f', '--flag', "Flag has been set" ) do
  options[:flag] = true
  end
end

optparse.parse!

# if no input-file or output-file is given, spit out the help
if ARGV.empty?
  puts optparse
  exit(-1)
end

# If the flag is true then tell me so, if not, tell me it isn't.
if options[:flag] = true
  pp "Flag is true"
else
  pp "Flag is false"
end

提前感谢,很抱歉在不知道如何编码的情况下尝试编码。我向你们鞠躬,伟大的人们。

I'm trying to get my script to do different things based on options. But... I don't know ruby, at all. I can't even tell you what an array really is. Here's what I got:

require 'optparse'
require 'pp'

# the options eventually get put here
options = {}

optparse = OptionParser.new do|opts|

# the help info
opts.banner = "Usage: script.rb [options] input-file output-file"

# This sets the default of 'flag' to 'false' and says it should be
# 'true' if the '-f' option is present
options[:flag] = false
  opts.on( '-f', '--flag', "Flag has been set" ) do
  options[:flag] = true
  end
end

optparse.parse!

# if no input-file or output-file is given, spit out the help
if ARGV.empty?
  puts optparse
  exit(-1)
end

# If the flag is true then tell me so, if not, tell me it isn't.
if options[:flag] = true
  pp "Flag is true"
else
  pp "Flag is false"
end

Thanks in advance, and sorry for trying to code without knowing how to code. I bow to you oh great ones.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

已下线请稍等 2024-11-10 00:09:43

options 哈希

options 不是一个数组,它是 哈希。在哈希中,存储了对象,每个对象都有自己的特殊键。在你的例子中,关键是 :flag,这样每当你调用 options[:flag] 时,你都会得到那里存储的任何内容。

options = {}

这只是创建了一个空的新哈希。

选项解析器

让我们从头开始:您的代码从

optparse = OptionParser.new do|opts|
  ...
end

您创建 OptionParser 的实例开始。这个类可以帮助您解析选项。 new() 方法实际上可以占用一个块,这是一个特殊的 Ruby 命令。如果您不知道数组是什么,您可能需要 阅读更多有关 Ruby 块的内容,因为它是一种相当特殊的语言元素。

不管怎样,它所做的就是创建一个名为 opts 的对象,它将处理传递给脚本的参数。您可以在 |opts| (在管道内)中看到这个对象,它是块变量——您现在已经阅读了有关块的内容,是吗?

首先,它有助于使用 banner() 方法打印横幅。稍后,它将使用 on() 方法检查是否传递了参数。

另外,请记住,您现在拥有之前的空 options 哈希值。在本例中,我们想要查找传递的 -f 选项,因此我们调用

opts.on( '-f', '--flag', "Flag has been set" ) do

如前所述,on() 方法查找其第一个参数 (' -f'),或类似 '--flag' 的替代方案。它也有一个描述。如果选项被传递,下面的行将被执行,因为我们想记住选项是否被传递:

options[:flag] = true

现在,您的 options 哈希包含键 :flag我们知道这是真的。

注意: optparse.parse! 实际上只会开始所有解析。请记住,我们之前已经创建了这个 Option Parser 对象,但它本身并没有真正执行任何操作。因此,它有一个名为parse!()的方法。人们喜欢在 Ruby 中对某些方法使用感叹号。有时你会遇到它。不管怎样,如果你不调用parse!,什么也不会发生。这正是 Ruby 选项解析器的构建方式。

检查选项

现在,在脚本末尾,您将假设所有选项都在 options 哈希中,因为选项解析器之前解析了所有内容。只需查看键 :flag 处的元素是否已设置为 true:

if options[:flag] == true

瞧! 重要:您在代码中忘记了双==!您想要进行比较,并且使用 = 无论如何都会将键设置为 true。所以请记住,对于条件表达式,您需要 == 而不是 =

注意:我们也可以只是寻找那里的钥匙。由于哈希之前是空的,因此它根本没有任何密钥。解析选项 -f 后,哈希值包含键 :flag。因此,我们不必寻找值(显然只能是 true),而是只寻找键:

if options.key?(:flag)
  # => the flag was set
end

添加更多选项

就像示例中的 on() 方法一样您可以添加另一个。只需记住为您的哈希使用另一个密钥,例如 :execute

opts.on( '-x', '--execute', "Do something!" ) do
  options[:execute] = true
end

The options Hash

options is not an Array, it's Hash. In a hash, there are objects stored, each with a special key of their own. In your case, the key is :flag, so that whenever you call options[:flag], you will get whatever is stored there.

options = {}

This just creates a new Hash which is empty.

The options parser

Let's take it from the beginning: Your code starts at

optparse = OptionParser.new do|opts|
  ...
end

You create an instance of an OptionParser. This is a class that helps you, well, parse options. The new() method actually can take a block, which is a special Ruby command. If you don't know what an Array is, you'll maybe need to read a bit more about Ruby blocks because it's a rather special language element.

Anyway, what it does is create an object named opts which will handle the arguments passed to your script. You can see this object in |opts| (within the pipes) and it's the block variable -- you've read about blocks now, have you?

First it helps to print a banner with the banner() method. Later, it will check with the on() method whether there was an argument passed.

Also, remember that you now have your empty options Hash from before. In this case we want to look for a -f option passed, so we call

opts.on( '-f', '--flag', "Flag has been set" ) do

As mentioned before, the on() method looks for its first parameter ('-f'), or an alternative like '--flag'. It also has a description. If the option was passed, the line below will be executed, because we want to remember if the option was passed or not:

options[:flag] = true

Now, your options hash contains the key :flag and we know that it is true.

Note: optparse.parse! will actually just start all the parsing. Remember that we've created this Option Parser object before, but it didn't really do anything on its own yet. Therefore, it has this method called parse!(). The exclamation mark is something people like to use in Ruby for some methods. You'll come across it sometimes. Anyway, if you don't call parse!, nothing will happen. This is just the way Ruby's option parser is built.

Checking the options

Now at the end of the script you will assume that all your options are in the options hash, as the option parser parsed everything before. Just look if the element at the key :flag has been set to true:

if options[:flag] == true

And voila! Important: You forgot double == in your code! You want to make a comparison and using = would set the key to true no matter what. So please remember that for conditional expressions you need == and not =!

Note: We could have also just looked for the key to be there. As the hash was empty before, it didn't have any key at all. After the option -f was parsed, the hash included the key :flag. So instead of looking for the value, which obviously can only be true, we could just look for the key:

if options.key?(:flag)
  # => the flag was set
end

Adding more options

Just like the on() method in your example you can add another one. Just remember to take another key for your hash, for example :execute.

opts.on( '-x', '--execute', "Do something!" ) do
  options[:execute] = true
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文