如何创建一个支持Ruby的shell命令? [英] How to create a shell command supporting Ruby?
问题描述
我正在创建一个gem,我希望能够在shell中执行一个函数。在更简单的上下文中,我需要创建一个shell脚本,当shell命令被触发时,该脚本执行带有选项的Ruby函数。最简单的方法是什么?
I'm creating a gem in which I would like to be able to make a shell command performing a function in the gem. In a more simple context, I need to create a shell script that performs a Ruby function with options when the shell command is triggered. What is a easiest way to do this?
例如:
$ cow moo
会使用cow命令的脚本执行cow gem并执行'moo
would take a cow gem with a script for the cow command and perform the 'moo' function in the Ruby gem.
如果可以的话,我也想在shell中支持常见的'options'格式化:
If I could, I would also like support for common 'options' formatting in the shell:
$ cow -t moo
上面的例子将采用一个选项并在Ruby中应用一个脚本来处理它(在这个例子中 -t
会打印两次moo)。
Which the above example would take an option and apply a script in Ruby to handle it (in this example -t
would print 'moo' twice).
如果任何人可以帮助我们这将是一个很大的帮助。非常感谢!
If anyone could help we with this it would be a great help. Thanks!
推荐答案
如何使用 http://visionmedia.github.com/commander/ ?从来没有使用过。但是这是一个宝石,使开发命令行应用程序更容易:)对你来说很繁重。
How about using http://visionmedia.github.com/commander/? Never used it before. But it's a gem that makes developing commandline apps easier :) Does the heavy lifting for you.
这里有一堆更多: https://www.ruby-toolbox.com/categories/CLI_Option_Parsers
这篇关于如何创建一个支持Ruby的shell命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!