Skip to content

Instantly share code, notes, and snippets.

@ggilder
Created December 15, 2012 19:16
Show Gist options
  • Save ggilder/4298299 to your computer and use it in GitHub Desktop.
Save ggilder/4298299 to your computer and use it in GitHub Desktop.
Demo of default command with commander
→ ./tester.rb
initialized with .
Compile called
→ ./tester.rb --dir .
initialized with .
Compile called
→ ./tester.rb deploy
initialized with .
Deploy called
→ ./tester.rb deploy --dir .
initialized with .
Deploy called
#! /usr/bin/env ruby
class Woody
VERSION = "1.0"
def initialize(dir)
puts "initialized with #{dir}"
end
def compile *args
puts "Compile called"
end
def deploy *args
puts "Deploy called"
end
end
require 'commander/import'
program :name, 'woody'
program :version, Woody::VERSION
program :description, 'Podcast static site generator'
default_command :compile
$dir = "."
global_option '--dir', '--dir path/to/site', "Specifies path to Woody site directory, if not the current directory" do |dir|
$dir = dir
end
command :compile do |c|
c.description = "Compiles the site"
c.option "--no-add", "Don't ask to add new metadata"
c.action do |args, options|
site = Woody.new($dir)
site.compile(options)
end
end
alias_command :c, :compile
command :deploy do |c|
c.description = "Deploys the site"
c.action do |args, options|
site = Woody.new($dir)
site.deploy
end
end
alias_command :d, :deploy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment