簡體   English   中英

Ruby OptionParser:隱藏命令選項的幫助文本

[英]Ruby OptionParser: hiding help text for a command option

Ruby“OptionParser將根據此描述為您自動生成幫助屏幕”[http://ruby.about.com/od/advancedruby/a/optionparser.htm]

有沒有辦法刪除命令選項的幫助文本。 我可以使用隱藏命令,而是使用命令選項(切換)並隱藏其幫助上下文。

我能夠把這個不那么優雅的解決方案扔到一起。 它將隱藏主幫助屏幕中的選項,聽起來它可能符合您的需求:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM