Kick the bash habit - start your command line scripts off right in Ruby

View the Project on GitHub davetron5000/methadone

Methadone is the best way to create simple command-line applications in Ruby. It sports a concise, readable set of helper methods, without sacrificing any of the power you can get from OptionParser.

If you're looking to make a "command-suite" app, please check out GLI, which makes it easy to support commands

The simplest Methadone-powered app

#!/usr/bin/env ruby

require 'methadone'

include Methadone::Main

main do 
 # whatever you want your app to do


A more typical app

Not terribly interesting. Let's:

#!/usr/bin/env ruby

require 'methadone'

include Methadone::Main
include Methadone::CLILogging

main do |some_arg| 
  puts some_arg
  puts options['ip-address'] if options[:verbose]

version     '0.0.1'
description 'Show off how great Methadone is!'
arg         :some_arg, :required

on("--verbose","Be verbose")
options['ip-address'] = ''
on("-i IP_ADDRESS","--ip-address","IP Address",/^\d+\.\d+\.\d+\.\d+$/)


Now, let's run our app:

$ our_app --help
Usage: our_app [options] some_arg

Show off how great Methadone is!


        --version                    Show help/version info
        --verbose                    Be verbose
    -i, --ip-address IP_ADDRESS      IP Address

Woot! Perfectly formatted help. We also have easy access to the parsed options:

$ our_app -i foobar
$ our_app -i --verbose foobar
$ our_app -i localhost foobar
invalid argument: -i localhost
$ our_app
parse error: 'some_arg' is required

Options are all parsed, and the regexp we passed to on is used to validate the argument to -i, and we also get checking for requiring some_arg


Typing all that in to get going is no fun. Let's use bin/methadone to bootstrap our app:

$ methadone -h
Usage: methadone [options] app_name

Kick the bash habit by bootstrapping your Ruby command-line apps


        --force                      Overwrite files if they exist
        --[no-]readme                [Do not ]produce a README file
        --rspec                      Generate RSpec unit tests instead of Test::Unit
    -l, --license LICENSE            Specify the license for your project
        --log-level LEVEL            Set the logging level
                                     (Default: info)
        --version                    Show help/version info


        Name of your app, which is used for the gem name and executable name
$ methadone --readme -l mit my_app
$ cd my_app/
$ ls
Gemfile         LICENSE.txt     README.rdoc     bin/            lib/            test/
LICENSE       Rakefile        features/       my_app.gemspec
$ bundle install
$ bundle exec bin/my_app --help
Usage: my_app [options]


        --version                    Show help/version info
        --log-level LEVEL            Set the logging level
                                     (Default: info)
$ rake
Run options: 

# Running tests:


Finished tests in 0.000521s, 1919.3858 tests/s, 1919.3858 assertions/s.

1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
Feature: My bootstrapped app kinda works
  In order to get going on coding my awesome app
  I want to have aruba and cucumber setup
  So I don't have to do it myself

  Scenario: App just runs
    When I get help for "my_app"
    Then the exit status should be 0
    And the banner should be present
    And the banner should document that this app takes options
    And the following options should be documented:
      | --version |
    And the banner should document that this app takes no arguments

In seconds, we have everything we need to start writing our app, including unit and integration tests. Nice!

What else is there?

Now what?