Better output for -h, --help and on option parse error.

This commit is contained in:
Chris Eppstein 2009-10-04 13:45:46 -07:00
parent 0fc9a0e3c8
commit dedff936b8
3 changed files with 51 additions and 18 deletions

View File

@ -6,12 +6,28 @@ module Compass
module CreateProjectOptionsParser
def set_options(opts)
opts.banner = %q{
if $command == "create"
opts.banner = %Q{
Usage: compass create path/to/project [options]
Description:
Create a new compass project at the path specified.
Options:
}.split("\n").map{|l| l.gsub(/^ */,'')}.join("\n")
else
opts.banner = %Q{
Usage: compass init project_type path/to/project [options]
Description:
Initialize an existing project at the path specified.
Supported Project Types:
* rails
Options:
}.split("\n").map{|l| l.gsub(/^ */,'')}.join("\n").strip
end
opts.on("--using FRAMEWORK", "Framework to use when creating the project.") do |framework|
framework = framework.split('/', 2)
@ -29,15 +45,28 @@ module Compass
register :init
class << self
def option_parser(arguments)
parser = Compass::Exec::CommandOptionParser.new(arguments)
parser.extend(Compass::Exec::GlobalOptionsParser)
parser.extend(Compass::Exec::ProjectOptionsParser)
parser.extend(CreateProjectOptionsParser)
end
def usage
option_parser([]).to_s
end
def parse!(arguments)
parser = parse_options!(arguments)
parser = option_parser(arguments)
parse_options!(parser, arguments)
parse_arguments!(parser, arguments)
set_default_arguments(parser)
parser.options
end
def parse_init!(arguments)
parser = parse_options!(arguments)
parser = option_parser(arguments)
parse_options!(parser, arguments)
if arguments.size > 0
parser.options[:project_type] = arguments.shift.to_sym
end
@ -46,11 +75,7 @@ module Compass
parser.options
end
def parse_options!(arguments)
parser = Compass::Exec::CommandOptionParser.new(arguments)
parser.extend(Compass::Exec::GlobalOptionsParser)
parser.extend(Compass::Exec::ProjectOptionsParser)
parser.extend(CreateProjectOptionsParser)
def parse_options!(parser, arguments)
parser.parse!
parser
end

View File

@ -1,16 +1,21 @@
module Compass::Exec
class CommandOptionParser
attr_accessor :options, :arguments
attr_accessor :options, :arguments, :opts
def initialize(arguments)
self.arguments = arguments
self.options = {}
end
def parse!
opts = OptionParser.new(&method(:set_options))
opts.parse!(arguments)
end
def opts
OptionParser.new(&method(:set_options))
end
def set_options(opts)
end
def to_s
opts.to_s
end
end
end

View File

@ -28,14 +28,17 @@ module Compass::Exec
protected
def perform!
command = args.shift
command_class = Compass::Commands[command]
options = if command_class.respond_to?("parse_#{command}!")
command_class.send("parse_#{command}!", args)
$command = args.shift
command_class = Compass::Commands[$command]
options = if command_class.respond_to?("parse_#{$command}!")
command_class.send("parse_#{$command}!", args)
else
command_class.parse!(args)
end
command_class.new(Dir.getwd, options).execute
rescue OptionParser::ParseError => e
puts "Error: #{e.message}"
puts command_class.usage
end
end