如何使用寻呼机制作 ruby 命令行应用程序?
我正在使用 Ruby 制作一个命令行工具。它将在屏幕上打印大量文本。目前,我正在使用 shell 管道 (may_app | more
) 来执行此操作。但我认为最好有一个默认寻呼机。
这就像执行 git log 时看到的一样。可以使用 git --nopager log 禁用寻呼机。
我已经做了很多谷歌工作并找到了一个宝石: hirb ,但这似乎有点矫枉过正。
经过多次尝试,我目前正在使用 shell 包装器来执行此操作:
#!/bin/bash
# xray.rb is the core script
# doing the main logic and will
# output many rows of text on
# screen
XRAY=$HOME/fdev-xray/xray.rb
if [ "--nopager" == "$1" ]; then
shift
$XRAY $*
else
$XRAY $* | more
fi
它有效。但还有更好的办法吗?
I'm making a command line tool using Ruby. It will print a lot of text on screen. Currently, I'm using shell pipeline (may_app | more
) to do so. But I think it's better to has a default pager.
It's just like what you see when execute git log
. One can disable pager by using git --nopager log
.
I've done quite much google work and find one gem: hirb , but it seems a little overkill.
After many tries, I'm current using shell wrapper to do so:
#!/bin/bash
# xray.rb is the core script
# doing the main logic and will
# output many rows of text on
# screen
XRAY=$HOME/fdev-xray/xray.rb
if [ "--nopager" == "$1" ]; then
shift
$XRAY $*
else
$XRAY $* | more
fi
It works. But is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你做得对。但是,您最好从
$PAGER
环境变量(如果有)获取寻呼机,而不是使用more
。例如,有些人更喜欢
less
而不是more
,而其他人则在此变量中设置了他们最喜欢的解析器选项。You are doing it right. But instead using
more
you'd better get a pager from$PAGER
environment variable, if any.Some people prefer
less
tomore
for example, and others have their favorite parser options set in this var.您可以通过调用
system
在 Ruby 中使用管道并提供选项(以及一个不错的帮助界面),如下所示:现在,您支持
--pager
和 < code>--no-pager 在命令行上,这很好。You can use the pipe in Ruby via a call to
system
and provide the options (along with a nice help interface) like so:Now, you support
--pager
and--no-pager
on the command line, which is nice to do.