使用Ruby从命令行参数中提取文件名

时间:2022-11-18 23:18:28

I'm trying to use optparse to parse command line arguments. I would like my program to accept arguments like that:

我正在尝试使用optparse来解析命令行参数。我希望我的程序接受这样的论点:

$ ./myscript.rb [options] filename

I can easily manage the [options] part:

我可以轻松管理[options]部分:

require 'optparse'

options = { :verbose => false, :type => :html }

opts = OptionParser.new do |opts|
  opts.on('-v', '--verbose') do
    options[:verbose] = true
  end
  opts.on('-t', '--type', [:html, :css]) do |type|
    options[:type] = type
  end
end
opts.parse!(ARGV)

But how do I get the filename?

但是我如何获得文件名?

I could extract it manually from ARGV, but there has to be a better solution, just can't figure out how

我可以从ARGV手动提取它,但必须有一个更好的解决方案,只是无法弄清楚如何

3 个解决方案

#1


The "parse" method returns the unprocessed ARGV. So in your example, it will return a one element array with the filename in it.

“parse”方法返回未处理的ARGV。所以在你的例子中,它将返回一个带有文件名的单元素数组。

#2


I can't just use ARGV.pop. For example when the last argument is "css" it could either be a file or belong to --type switch.

我不能只使用ARGV.pop。例如,当最后一个参数是“css”时,它可以是文件或属于--type开关。

But if your script requires the last argument to be a filename (which is what your usage output inquires) this case should never happen the script should exit with a non-zero and the user should get a usage report or error.

但是如果你的脚本要求最后一个参数是一个文件名(这是你的使用输出查询的),这种情况永远不会发生,脚本应该以非零退出,用户应该得到一个使用报告或错误。

Now if you want to make a default filename or not require a filename as the last argument but leave it optional then you could just test to see if the last argument is a valid file. If so use it as expected otherwise continue without etc.

现在,如果您想创建一个默认文件名或者不需要文件名作为最后一个参数但是保留它是可选的,那么您可以测试一下,看看最后一个参数是否是一个有效文件。如果是这样,请按预期使用否则继续使用等。

#3


I don't think extracting it before sending it to OptionParser is bad, I think it makes sense. I probably say this because I have never used OptionParser before, but oh well.

我认为在将它发送到OptionParser之前将其解压缩是不好的,我认为这是有道理的。我可能会这样说,因为我之前从未使用过OptionParser,但是哦。

require 'optparse'

file = ARGV.pop
opts = OptionParser.new do |opts|
  # ...
end

#1


The "parse" method returns the unprocessed ARGV. So in your example, it will return a one element array with the filename in it.

“parse”方法返回未处理的ARGV。所以在你的例子中,它将返回一个带有文件名的单元素数组。

#2


I can't just use ARGV.pop. For example when the last argument is "css" it could either be a file or belong to --type switch.

我不能只使用ARGV.pop。例如,当最后一个参数是“css”时,它可以是文件或属于--type开关。

But if your script requires the last argument to be a filename (which is what your usage output inquires) this case should never happen the script should exit with a non-zero and the user should get a usage report or error.

但是如果你的脚本要求最后一个参数是一个文件名(这是你的使用输出查询的),这种情况永远不会发生,脚本应该以非零退出,用户应该得到一个使用报告或错误。

Now if you want to make a default filename or not require a filename as the last argument but leave it optional then you could just test to see if the last argument is a valid file. If so use it as expected otherwise continue without etc.

现在,如果您想创建一个默认文件名或者不需要文件名作为最后一个参数但是保留它是可选的,那么您可以测试一下,看看最后一个参数是否是一个有效文件。如果是这样,请按预期使用否则继续使用等。

#3


I don't think extracting it before sending it to OptionParser is bad, I think it makes sense. I probably say this because I have never used OptionParser before, but oh well.

我认为在将它发送到OptionParser之前将其解压缩是不好的,我认为这是有道理的。我可能会这样说,因为我之前从未使用过OptionParser,但是哦。

require 'optparse'

file = ARGV.pop
opts = OptionParser.new do |opts|
  # ...
end