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?
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.
If I could, I would also like support for common 'options' formatting in the 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).
If anyone could help we with this it would be a great help. Thanks!
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.
There is a bunch more over here: https://www.ruby-toolbox.com/categories/CLI_Option_Parsers
You could create a regular executable Ruby file named
cow
containing:Note the first line, which is the shebang line. It tells your shell which program to use in order to interpret the script. This allows the user to simply call
cow
instead ofruby $(which cow)
.Put your script in the
bin
directory:Now, all you need to do is put that in your gem specification:
During installation, Rubygems will install your gem's binaries somewhere in the user's path, ensuring they can be found.
As for option parsing, the standard library includes
optparse
, but there are also many gems available.I created my own option parser, called Acclaim, which I use in my own utilities. Here's a sample:
In order to test your application, you can simply execute your script:
However, this requires that you build your gem from the specification and install it locally every time you want to test your changes:
Bundler makes life easier by inserting your code into the load path automatically. You just have to run your executable through Bundler: