Ruby OptionParser:隐藏命令选项的帮助文本

7

Ruby的"OptionParser将根据描述自动生成帮助屏幕" [http://ruby.about.com/od/advancedruby/a/optionparser.htm]

是否有一种方法可以移除命令选项的帮助文本。 我可以使用隐藏命令,但更倾向于使用命令选项(开关)并隐藏其帮助内容。

1个回答

3

我能为此提供一个不太优雅的解决方案。它将从主帮助界面中隐藏选项,听起来可能适合您的需求:

require 'optparse'

options = {}

OptionParser.new do |opts|
  opts.banner = "Usage: #{$0} [options]"

  opts.on("-a", "--argument 1,2,3", Array, "Array of arguments") { |a| options[:array] = a  }
  opts.on("-v", "--verbose", "Verbose output") { |v| options[:verbose] = true }
  opts.on("-h", "--help", "Display this help") do
    hidden_switch = "--argument"
    #Typecast opts to a string, split into an array of lines, delete the line 
    #if it contains the argument, and then rejoins them into a string
    puts opts.to_s.split("\n").delete_if { |line| line =~ /#{hidden_switch}/ }.join("\n") 
    exit
  end
end

如果您运行--help命令,会看到以下输出:

Usage: test.rb [options]
    -v, --verbose                    Verbose output
    -h, --help                       Display this help

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接