如何使用 'conflicts' 和 ruby trolloop
How to use 'conflicts' with ruby trolloop
您好,我正在使用 trollop 来解析 ruby 中的命令行选项,我有四个互斥选项,并且总是需要一个选项。
我一直在努力弄清楚如何确保只通过四个选项中的一个。如果使用多个选项调用,我希望显示使用帮助(教育?)。
我从 trollop 源中看到有一种叫做冲突的东西
http://www.rubydoc.info/gems/trollop/2.1.2/Trollop/Parser#conflicts-instance_method
这听起来像是我想要的 (?) 但我不知道如何正确使用它。
我当前的节实际上是这样的
require 'trollop'
opts = Trollop::options do
opt :last, "last"
opt :first, "first"
opt :file, "filename",
:type => String
opt :date, "date to read",
:type => Date
end
Trollop::die :file, "must exist" unless File.exist?(opts[:file]) if opts[:file]
谢谢
您可以像这样在 do
块中将 conflicts
添加为一行:
require 'trollop'
opts = Trollop::options(ARGV) do
opt :last, "last"
opt :first, "first"
opt :file, "filename", :type => String
opt :date, "date to read", :type => Date
conflicts :last, :first
end
puts "Your Options Are: "
puts opts
然后你得到以下输出:
ruby test_options.rb --last Last --first First
Error: --last conflicts with --first.
Try --help for help.
您好,我正在使用 trollop 来解析 ruby 中的命令行选项,我有四个互斥选项,并且总是需要一个选项。
我一直在努力弄清楚如何确保只通过四个选项中的一个。如果使用多个选项调用,我希望显示使用帮助(教育?)。
我从 trollop 源中看到有一种叫做冲突的东西
http://www.rubydoc.info/gems/trollop/2.1.2/Trollop/Parser#conflicts-instance_method
这听起来像是我想要的 (?) 但我不知道如何正确使用它。
我当前的节实际上是这样的
require 'trollop'
opts = Trollop::options do
opt :last, "last"
opt :first, "first"
opt :file, "filename",
:type => String
opt :date, "date to read",
:type => Date
end
Trollop::die :file, "must exist" unless File.exist?(opts[:file]) if opts[:file]
谢谢
您可以像这样在 do
块中将 conflicts
添加为一行:
require 'trollop'
opts = Trollop::options(ARGV) do
opt :last, "last"
opt :first, "first"
opt :file, "filename", :type => String
opt :date, "date to read", :type => Date
conflicts :last, :first
end
puts "Your Options Are: "
puts opts
然后你得到以下输出:
ruby test_options.rb --last Last --first First
Error: --last conflicts with --first.
Try --help for help.