2016-10-03 11 views
3

我正在嘗試使CLI的紅寶石gem my_command --help更清潔。如何水平對齊使用OptionParser的多行CLI幫助文本

有一些CLI選項和標誌需要一些句子來解釋它們。當解釋太長以至於無法放入普通的終端寬度視圖內時,我還沒有找到一種方法在列中正確對齊此文本。

我想有這樣的事情:

ab --help as an example AB --help爲例,注意一些標誌如何有一個正確對齊多行的解釋。

現在,我做這樣的事情在OptionParser繼續在自己列上排列的情況下,我們需要多行解釋點什麼文字:

opts.on("-d", "--directory PATH", String, "Directory to save the downloaded files into\n\t\t\t\t  Default is ./websites/ plus the domain name") do |t| 
    options[:directory] = t 
end 

它的工作,但它似乎沒有最優也不乾淨的地方有\t強制格式化。另外,我可以看到在其他終端配置中沒有正確格式化的情況。

如何以清晰的方式水平排列多行CLI幫助文本與OptionParser?

+0

對於ref的問題的創業板:https://github.com/hartator/wayback-machine-downloader – Hartator

+1

嗯,無法測試,但我會開始尋找在這裏:http://ruby-doc.org /stdlib-2.3.1/libdoc/optparse/rdoc/OptionParser.html#method-i-summarize,可以使用'indent'和'width'選項。 – lcguida

回答

1

您可以強制換行,而不需要通過增加更多的參數來添加標籤來opts.on

opts.on("-d", "--directory PATH", String, 
     "Directory to save the downloaded files into", 
     "Default is ./websites/ plus the domain name") do |t| 
    options[:directory] = t 
end 

這不是正式文件非常明確記載,但你可以看到它在complete example使用。