gpt4 book ai didi

ruby - 如何最好地包装 Ruby optparse 代码和输出?

转载 作者:太空宇宙 更新时间:2023-11-03 16:23:45 24 4
gpt4 key购买 nike

对于下面的代码,根据风格指南,它应该以 80 个字符换行:

opts.on('--scores_min <uint>', Integer, 'Drop reads if a single position in ',
'the index have a quality score ',
'below scores_main (default= ',
"#{DEFAULT_SCORE_MIN})") do |o|
options[:scores_min] = o
end

结果输出是:

    --scores_min <uint>          Drop reads if a single position in
the index have a quality score
below scores_main (default=
16)

它以 72 个字符换行,看起来不对 :o(

我真的希望它包含 80 个字符并像这样对齐:

    --scores_min <uint>          Drop reads if a single position in the
index have a quality score below
scores_min (default=16)

如何巧妙地实现这一点?

最佳答案

在这种情况下最简单的解决方案是像这样堆叠参数:

opts.on('--scores_min <uint>',
Integer,
"Drop reads if a single position in the ",
"index have a quality score below ",
"scores_min (default= #{DEFAULT_SCORE_MIN})") do |o|
options[:scores_min] = o
end

这会产生一个相当令人愉快的输出:

    --scores_min <uint>          Drop reads if a single position in the 
index have a quality score below
scores_min (default= 16)

更一般地说,here docs可以更轻松地以在代码和输出中看起来都不错的方式格式化输出字符串:

       # Deeply nested code
puts <<~EOT
Drop reads if a single position in the
index have a quality score below
scores_min (default= #{DEFAULT_SCORE_MIN})
EOT

但在这种情况下效果不佳,因为描述字符串会自动缩进。

关于ruby - 如何最好地包装 Ruby optparse 代码和输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29229059/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com