lib/brite/command.rb in brite-0.5 vs lib/brite/command.rb in brite-0.6.0

- old
+ new

@@ -1,83 +1,80 @@ -require 'brite/site' +require 'optparse' +require 'brite/controller' module Brite - # Webrite command line interface. + # Initialize and run Command. + def self.cli(*argv) + Command.new(*argv).call + end + # Webrite command line interface. class Command - def self.start - new.start + # New Command. + def initialize(*argv) + @output = nil #@argv.shift + @url = nil + @dryrun = false + @trace = false + + parser.parse!(argv) + + @location = argv.shift || '.' end - def initialize(argv=nil) - @argv ||= ARGV.dup + # Returns an OptionParser instance. + def parser + OptionParser.new do |opt| + opt.on("--url URL", "site URL") do |url| + @url = url + end - @noharm = @argv.delete('--dryrun') || @argv.delete('--noharm') - @debug = @argv.delete('--debug') + opt.on("--trace", "show extra operational information") do + @trace = true + end - @argv.reject!{ |e| e =~ /^-/ } + opt.on("--dryrun", "-n", "don't actually write to disk") do + @dryrun = true + end - @location = @argv.shift || '.' - #@output = @argv.shift + opt.on("--force", "force overwrites") do + $FORCE = true + end + + opt.on("--debug", "run in debug mode") do + $DEBUG = true + end + + opt.on("--warn", "show Ruby warnings") do + $VERBOSE = true + end + + opt.on_tail("--help", "display this help message") do + puts opt + exit + end + end end # - def start + def call begin - site.build + controller.build rescue => e - @debug ? raise(e) : puts(e.message) + $DEBUG ? raise(e) : puts(e.message) end end - def site - Site.new( + def controller + Controller.new( :location => @location, :output => @output, - :noharm => @noharm, + :url => @url, + :dryrun => @dryrun, :trace => @trace ) - end - end - - # - # Command to generate a single part to standard out. - # - - class PartCommand - - def self.start - new.start - end - - def initialize(argv=nil) - @argv ||= ARGV.dup - end - - def start - render(parts) - end - - # render a single part to stdout. - - def render(parts) - $stdout << Page.new(parts).to_html - end - - private - - def parts - parts = [] - @argv.each do |x| - if /^-/ =~ x - parts << [x.sub(/-{1,2}/,'')] - else - parts.last < x - end - end - Hash[*parts] end end end