Methadone - kick the bash habit and start your command line apps off right
Dave Copeland (davetron5000 at g mail dot com)
Copyright © 2011 by Dave Copeland
Distributes under the Apache License, see LICENSE.txt in the source distro
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
A simplified means of running external commands that has better error handling and diagnostics.
Simplified zero-config logging that is a better-than-
Cucumber Steps, built on Aruba, to allow you to test-drive your command-line app development.
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.
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
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
An executable using Methadone::Main to outline your new app
Test::Unittest 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
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.onjust to set a value in a
No exception handling - you have to explicitly call
exitand/or let exceptions' stack traces leak through.
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
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.
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
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
Methadone uses aruba for BDD-style testing with cucumber. This library has some awesome steps, and Methadone provides additional, more opinionated, steps.
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.
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.