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

- old
+ new

@@ -3,80 +3,125 @@ module Brite # Initialize and run Command. def self.cli(*argv) - Command.new(*argv).call + Command.call(*argv) end - # Webrite command line interface. - class Command + # Brite command module. + module Command + extend self - # New Command. - def initialize(*argv) - @output = nil #@argv.shift - @url = nil - @dryrun = false - @trace = false + # Execute command. + # + # @public + def call(*argv) + options = parse(argv) - parser.parse!(argv) + begin + controller(options).build + rescue => e + $DEBUG ? raise(e) : puts(e.message) + end + end - @location = argv.shift || '.' + # Create an instance of Brite::Controller given controller options. + # + # @private + # + # @return [Controller] New controller instance. + def controller(options) + Controller.new(options) end - # Returns an OptionParser instance. - def parser - OptionParser.new do |opt| - opt.on("--url URL", "site URL") do |url| - @url = url - end + # Parse controller options from command line arguments. + # + # @private + # + # @return [Hash] controller options + def parse(argv) + parser = OptionParser.new - opt.on("--trace", "show extra operational information") do - @trace = true - end + options = { + :output => nil, + :url => nil + } - opt.on("--dryrun", "-n", "don't actually write to disk") do - @dryrun = true - end + options_url parser, options + options_general parser, options + options_help parser, options - opt.on("--force", "force overwrites") do - $FORCE = true - end + parser.parse!(argv) - opt.on("--debug", "run in debug mode") do - $DEBUG = true - end + options[:location] = argv.shift || '.' - opt.on("--warn", "show Ruby warnings") do - $VERBOSE = true - end + options + end - opt.on_tail("--help", "display this help message") do - puts opt - exit - end + # Add `--url` option to command line parser. + # + # @param [OptionParser] parser + # An instance of option parser. + # + # @param [Hash] options + # An options hash to be passed to Controller. + # + # @private + def options_url(parser, options) + parser.on("--url URL", "website fully qualified URL") do |url| + options[:url] = url end end + # Add `--trace`, `--dryrun`, `--force`, `--debug` and `--warn` options + # to command line interface. These are all "global" options which means + # they set global variables if used. # - def call - begin - controller.build - rescue => e - $DEBUG ? raise(e) : puts(e.message) + # @param [OptionParser] parser + # An instance of option parser. + # + # @param [Hash] options + # An options hash to be passed to Controller. + # + # @private + def options_general(parser, options) + parser.on("--trace", "show extra operational information") do + $TRACE = true end + + parser.on("--dryrun", "-n", "don't actually write to disk") do + $DRYRUN = true + end + + parser.on("--force", "force overwrites") do + $FORCE = true + end + + parser.on("--debug", "run in debug mode") do + $DEBUG = true + end + + parser.on("--warn", "show Ruby warnings") do + $VERBOSE = true + end end - def controller - Controller.new( - :location => @location, - :output => @output, - :url => @url, - :dryrun => @dryrun, - :trace => @trace - ) + # Add `--help` option to command line parser. + # + # @param [OptionParser] parser + # An instance of option parser. + # + # @param [Hash] options + # An options hash to be passed to Controller. + # + # @private + def options_help(parser, options) + parser.on_tail("--help", "display this help message") do + puts opt + exit + end end end end -