简体   繁体   English

如何创建支持Ruby的shell命令?

[英]How to create a shell command supporting 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. 我正在创建一个gem,我希望能够在该gem中执行执行该功能的shell命令。 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. 在更简单的上下文中,我需要创建一个shell脚本,该脚本在触发shell命令时执行带有选项的Ruby函数。 What is a easiest way to do this? 最简单的方法是什么?

For example: 例如:

$ cow moo

would take a cow gem with a script for the cow command and perform the 'moo' function in the Ruby gem. 将使用带Cow命令脚本的Cow gem并在Ruby gem中执行'moo'功能。

If I could, I would also like support for common 'options' formatting in the shell: 如果可以的话,我还希望在shell中支持常见的“选项”格式:

$ cow -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). 上面的示例将带一个选项,并在Ruby中应用脚本来处理它(在此示例中-t将两次打印'moo')。

If anyone could help we with this it would be a great help. 如果有人可以帮助我们,这将是一个很大的帮助。 Thanks! 谢谢!

You could create a regular executable Ruby file named cow containing: 您可以创建一个名为cow的常规可执行Ruby文件,其中包含:

#!/usr/bin/env ruby
require 'cow'

Cow.say ARGV.first

Note the first line, which is the shebang line . 注意第一行,即shebang行 It tells your shell which program to use in order to interpret the script. 它告诉您的Shell使用哪个程序来解释脚本。 This allows the user to simply call cow instead of ruby $(which cow) . 这使用户可以简单地调用cow而不是ruby $(which cow)

Put your script in the bin directory: 将您的脚本放在bin目录中:

cow/
  bin/
    cow  <- your executable file
  lib/
    cow.rb
    cow/
      say.rb
  cow.gemspec

Now, all you need to do is put that in your gem specification: 现在,您需要做的就是将其放入您的gem规范中:

Gem::Specification.new 'cow' do |gem|
  gem.executables = %w(bin/cow)
end

During installation, Rubygems will install your gem's binaries somewhere in the user's path, ensuring they can be found. 在安装过程中,Rubygems会将您的gem二进制文件安装在用户路径中的某个位置,以确保可以找到它们。

As for option parsing, the standard library includes optparse , but there are also many gems available. 至于选项解析,标准库包括optparse ,但也有许多gems可用。

I created my own option parser, called Acclaim , which I use in my own utilities. 我创建了自己的选项解析器Acclaim ,该解析器在自己的实用程序中使用。 Here's a sample: 这是一个示例:

class Cow::Command < Acclaim::Command
  option :all_caps, '-A', '--all-caps'

  when_called do |options, args|
    text = args.first.to_s
    text.upcase! if options.all_caps?
    Cow.say text
  end
end

In order to test your application, you can simply execute your script: 为了测试您的应用程序,您可以简单地执行脚本:

$ pwd
~/projects/cow
$ ./bin/cow moo
# output here...

However, this requires that you build your gem from the specification and install it locally every time you want to test your changes: 但是,这需要您从规范中构建您的gem,并在每次要测试更改时在本地安装它:

ruby -I ./lib -r cow -e 'puts Cow.version'
0.0.1
gem build cow.gemspec && gem install cow-0.0.1.gem

Bundler makes life easier by inserting your code into the load path automatically. Bundler通过将代码自动插入到加载路径中,使生活变得更轻松。 You just have to run your executable through Bundler: 您只需要通过Bundler运行可执行文件:

$ bundle exec ./bin/cow moo
# output here...

How about using http://visionmedia.github.com/commander/ ? 如何使用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. 但这是使开发命令行应用程序更加容易的瑰宝:)为您完成繁重的工作。

There is a bunch more over here: https://www.ruby-toolbox.com/categories/CLI_Option_Parsers 这里还有更多的东西: https : //www.ruby-toolbox.com/categories/CLI_Option_Parsers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM