Skip to content

Commit

Permalink
Refactor commands.
Browse files Browse the repository at this point in the history
Make -d and -v options behave like command equivalents.


git-svn-id: http://code.macournoyer.com/svn/thin/trunk@347 ab7993de-5426-0410-a80b-baa00616f331
  • Loading branch information
macournoyer committed Nov 28, 2007
1 parent 353cdc6 commit c12955e
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 50 deletions.
2 changes: 1 addition & 1 deletion bin/thin
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# Run <tt>thin help</tt> to get help.

require File.dirname(__FILE__) + '/../lib/thin'
require 'thin/commands'
require 'thin/command'

Thin.define_commands do
program_name 'thin'
Expand Down
2 changes: 1 addition & 1 deletion bin/thin_cluster
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# Run <tt>thin_cluster help</tt> to get help.

require File.dirname(__FILE__) + '/../lib/thin'
require 'thin/commands'
require 'thin/command'

Thin.define_commands do
program_name 'thin_cluster'
Expand Down
45 changes: 45 additions & 0 deletions lib/thin/command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'transat/parser'

module Thin
def self.define_commands(&block)
begin
Transat::Parser.parse_and_execute(ARGV, &block)
rescue CommandError => e
puts "Error: #{e}"
end
end

class CommandError < StandardError; end

class Command
attr_reader :args

def initialize(non_options, options)
@args = non_options

options.each do |option, value|
self.send("#{option}=", value)
end
end

def self.command_name
self.name.match(/::(\w+)$/)[1].downcase
end

def self.detailed_help
<<-EOF
usage: #{File.basename($PROGRAM_NAME)} #{command_name}
#{help}
EOF
end

protected
def error(message)
raise CommandError, message
end
end

module Commands; end
Dir[File.dirname(__FILE__) + '/commands/**/*.rb'].each { |l| require l }
end
46 changes: 0 additions & 46 deletions lib/thin/commands.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/thin/commands/cluster/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'yaml'

module Thin::Commands::Cluster
class Base < Thin::Commands::Command
class Base < Thin::Command
def self.config_attributes
[:address, :port, :environment, :log_file, :pid_file, :cwd, :servers, :user, :group]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/thin/commands/server/base.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Thin::Commands::Server
class Base < Thin::Commands::Command
class Base < Thin::Command
def cwd
args.first || '.'
end
Expand Down
8 changes: 8 additions & 0 deletions lib/transat/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ def option(name, options={})
@option_parser.on(*opt_args.compact) do |value|
@received_options[name] = value
end

@option_parser.on_tail('-h', '--help') do
raise HelpNeeded, nil
end

@option_parser.on_tail('-v', '--version') do
raise VersionNeeded
end
end

def command(name, klass, options={})
Expand Down

0 comments on commit c12955e

Please sign in to comment.