Methadone - kick the bash habit and start your command line apps off right

Author

Dave Copeland (davetron5000 at g mail dot com)

Copyright

Copyright © 2011 by Dave Copeland

License

Distributes under the Apache License, see LICENSE.txt in the source distro

Build
Status

A smattering of tools to make your command-line apps easily awesome; kick the bash habit without sacrificing any of the power.

The goal of this project is to make it as easy as possible to write awesome and powerful command-line applications.

Toward that end, this gem provides:

  • A command-line app to bootstrap a new command-line app.

  • A lightweight DSL to create your command-line interface, that loses none of OptionParser's power.

  • A simplified means of running external commands that has better error handling and diagnostics.

  • Simplified zero-config logging that is a better-than-puts puts.

  • Cucumber Steps, built on Aruba, to allow you to test-drive your command-line app development.

Tutorial

Tutorial for your iPad - This is a free iBooks interactive ebook that contains a step-by-step tutorial on making an awesome app with Methadone, including screencasts.

Links

Platforms

  • The build runs on Travis for:

    • MRI Ruby 1.9.2

    • MRI Ruby 1.9.3

    • MRI Ruby 2.0.0

    • MRI Ruby 2.1.0

    • JRuby in both 1.8 and 1.9 mode

Bootstrapping a new CLI App

The methadone command-line app will bootstrap a new command-line app, setting up a proper gem structure, unit tests, and cucumber-based tests with aruba:

$ methadone --help
Usage: methadone [options] app_name
        --force                      Overwrite files if they exist
$ methadone newgem
$ cd newgem
$ bundle
$ rake
1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
1 scenario (1 passed)
3 steps (3 passed)
$ cat features/newgem.feature
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]
    """

Basically, this sets you up with all the boilerplate that you should be using to write a command-line app. Specifically, you get:

  • Gemified project (based on bundle gem)

  • An executable using Methadone::Main to outline your new app

  • Test::Unit test task set up and an empty unit test.

  • Aruba/Cucumber set up with a basic feature that exercise your executable

  • The outline of a README

  • An optional license included

DSL for your bin file

A canonical OptionParser driven app has a few problems with it structurally that Methadone can solve:

  • Backwards organization - main logic is at the bottom of the file, not the top

  • Verbose to use opts.on just to set a value in a Hash

  • No exception handling - you have to explicitly call exit and/or let exceptions' stack traces leak through.

Methadone provides Methadone::Main to help make a clean and easy-to-maintain bin file. See the rdoc for an example, and see my blog on the derivation of this module.

Wrapper for running external commands with good logging

While backtick and %x[] are nice for compact, bash-like scripting, they have some failings:

  • You have to check the return value via $?

  • You have no access to the standard error

  • You really want to log: the command, the output, and the error so that for cron-like tasks, you can sort out what happened

Enter Methadone::SH

sh "cp foo.txt /tmp"
# => logs command at DEBUG level
#    if the command exited zero:
#        logs the standard output at DEBUG
#        logs the standard error at WARN
#    if the command exited nonzero:
#        logs the standard output at INFO
#        logs the standard error at WARN
#        returns the exit code for your examination
#
#        there's a LOT MORE

See the rdoc for more detailed examples and usage.

This isn't a replacement for Open3 or ChildProcess, but a way to easily “do the right thing” for most cases.

Zero-Config Logging

Chances are, your code is littered with STDERR.puts on a good day, and nothing on a bad day. You probably also have a bunch of debug puts calls that you have commented out. Logging is extremely helpful in understand how your app is behaving (or how it behaved in the past). Logging can be a pain to set up, and can also make it hard to give the user at the command-prompt a good experience.

Methadone::CLILogger is designed to handle this. It's a proper subclass of Ruby's built-in Logger with a few enhancements:

  • Messages don't get formatting if they are destined for a TTY (e.g. the user sitting at her terminal)

  • Errors and warnings go to the standard error.

  • Debug and info messages go to the standard output.

  • When these are redirected to a file, the log messages are properly date/time stamped as you'd expect

  • You can mix-in Methadone::CLILogging to get access to a global logger instances without resorting to an explicit global variable

See CLILogger’s rdoc and then CLILogging’s for more.

Currently, there are classes that assist in directing output logger-style to the right place; basically ensuring that errors go to STDERR and everything else goes to STDOUT. All of this is, of course, configurable

Cucumber Steps

Methadone uses aruba for BDD-style testing with cucumber. This library has some awesome steps, and Methadone provides additional, more opinionated, steps.

Example

Here's an example from methadone's own tests:

Scenario: Help is properly documented
  When I get help for "methadone"
  Then the exit status should be 0
  And the following options should be documented:
    |--force|
  And the banner should be present
  And the banner should include the version
  And the banner should document that this app takes options
  And the banner should document that this app's arguments are:
    |app_name|which is required|
    |dir_name|which is optional|

See Methadone::Cucumber or its rdoc for a list of all the steps provided.

Contributing

  • Feel free to file an issue, even if you don't have time to submit a patch

  • Please try to include a test for any patch you submit. If you don't include a test, I'll have to write one, and it'll take longer to get your code in.