Project

swamp-cli

0.0
No commit activity in last 3 years
No release in over 3 years
A framework for writing simple CLI applications in Ruby.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Swamp

Swamp is a simple framework for developing command line applications in Ruby.

Getting Started

Swamp works by allowing you to define a commands in a directory of your application. These commands can then be executed when needed and options provided as needed.

To begin, just make a directory in your application and define your first command using the simple command DSL. Here's an example:

command :install do
  desc "Install an application"

  option "-f", "--force" do |value, opts|
    opts[:force] = true
  end

  action do |context|
    if context.args[0].nil?
      raise Error, "You must provide a command."
    end

    if context.options[:force]
      # We can look at the options if we want...
    end

    # ... continue with the rest of your method.
  end
end

Once you've defined your commands, you'll need to create an entry point for your application. Usually this is a file in the bin folder of your application. You'll usually want to require other parts of your application but the boiler plate required to dispatch actions to your Swamp defined commands is as follows:

require 'swamp/cli'

begin
  cli = Swamp::CLI.new(:bask, version: Bask::VERSION)
  cli.load_from_directory(File.expand_path('../../commands', __FILE__))
  cli.dispatch(ARGV)
rescue Swamp::Error => e
  $stderr.puts "Error: #{e.message}"
  exit 1
end

That's about it really.