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

View File

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

View File

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