optparse-plus

Start your command line scripts off right in Ruby

View the Project on GitHub davetron5000/optparse-plus

optparse-plus 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 optparse-plus-powered app

#!/usr/bin/env ruby

require 'optparse_plus'

include OptparsePlus::Main

main do 
 # whatever you want your app to do
end

go!

A more typical app

Not terribly interesting. Let's:

#!/usr/bin/env ruby

require 'optparse_plus'

include OptparsePlus::Main
include OptparsePlus::CLILogging

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

version     '0.0.1'
description 'Show off how great optparse-plus is!'
arg         :some_arg, :required

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

go!

Now, let's run our app:

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

Show off how great optparse-plus is!

v0.0.1

Options:
        --version                    Show help/version info
        --verbose                    Be verbose
    -i, --ip-address IP_ADDRESS      IP Address
                                     (default: 127.0.0.1)

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

$ our_app -i 192.268.0.1 foobar
foobar
$ our_app -i 192.268.0.1 --verbose foobar
foobar
192.168.1.0
$ 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

Bootstrapping

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

$ optparse_plus -h
Usage: optparse_plus [options] app_name

Bootstrap your Ruby command-line apps

v1.1.0

Options:
        --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
                                     (mit|apache|custom|NONE)
        --log-level LEVEL            Set the logging level
                                     (debug|info|warn|error|fatal)
                                     (Default: info)
        --version                    Show help/version info

Arguments:

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

v0.0.1

Options:
        --version                    Show help/version info
        --log-level LEVEL            Set the logging level
                                     (debug|info|warn|error|fatal)
                                     (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?