There’s no shortage of projects to build CLI apps in Ruby, but few ship with as many tools to help you build better command line interfaces as David Copeland’s Methadone. Features include bootstrapping, logging, command DSL, and even Cucumber support via Aruba.

The DSL aims to be a readable, convenience wrapper around OptionParser:

#!/usr/bin/env ruby

require 'optparse'
require 'methadone'

include Methadone::Main

main do |name,password|
  name # => guaranteed to be non-nil
  password # => nil if user omitted on command line
  options[:switch] # => true if user used --switch or -s
  options[:s]      # => ALSO true if user used --switch or -s
  options[:f]      # => value of FILE if used on command-line
  options[:flag]   # => ALSO value of FILE if used on command-line

  ...
end

description "One line summary of your awesome app"

on("--[no-]switch","-s","Some switch")
on("-f FILE","--flag","Some flag")
on("-x FOO") do |foo|
  # something more complex; this is exactly OptionParser opts.on 
end

arg :name
arg :password, :optional

go!

Since Methadone also scaffolds out your Cucumber features, you can test your CLI via Aruba:

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 run `newgem --help`
    Then the exit status should be 0
    And the output should contain:
    """
    Usage: newgem [options]
    """

Check out the README for advanced usage or the roadmap to see how you might contribute.


Have comments? Send a tweet to @TheChangelog on Twitter.

Subscribe to The Changelog Weekly – our weekly email covering everything that hits our open source radar.