我正在尝试使用 OptionParser 创建一个默认选项列表,该列表可用于共享相同选项的单独文件中。前任:
文件 1:
default = OptionParser.new do |opts|
opts.banner = "This banner is shared by multiple files"
opts.on("-d", "--debug [level]", "debug logging level: critical, error, warning, info, debug1, debug2") do |opt|
$debug_level = opt
end
end
default.parse!(argv)
文件 2:
options = OptionParser.new do |opts|
opts.on("-v", "--version [release]", "release version") do |opt|
release = opt
end
end
options.parse!(argv)
无需在每个文件中重复 opts.banner 和 -d opt,我希望能够让文件 2 将本地 opts (-v) 与默认值 (banner, -d) 结合起来。完全写出来看起来像:
options = OptionParser.new do |opts|
opts.banner = "This banner is shared by multiple files"
opts.on("-d", "--debug [level]", "debug logging level: critical, error, warning, info, debug1, debug2") do |opt|
$debug_level = opt
end
opts.on("-v", "--version [release]", "release version") do |opt|
release = opt
end
end
options.parse!(argv)
我已经看到了一些关于子命令等的东西,但从来没有任何东西严格表明可以组合选项或跨文件使用它们。