#!/usr/bin/env ruby $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) require 'quickl' require 'erb' # # Quickl - Generate Ruby command line apps quickly # # SYNOPSIS # #{program_name} [options] COMMAND_NAME # # OPTIONS # #{summarized_options} # # DESCRIPTION # This tool helps you generating Ruby command line applications # class Quickl::Main < Quickl::Command(__FILE__, __LINE__) include Quickl::Naming OPTION_HELPERS = { :version => %q{ opt.on_tail('--version', 'Show version and exit'){ raise Quickl::Exit, "#{Quickl.program_name} #{VERSION}" } }, :help => %q{ opt.on_tail('--help', "Show this help message"){ raise Quickl::Help } } } # Name of the command to generate attr_reader :cmd_name # Generate require header attr_reader :header # Generate require footer attr_reader :footer # Creates a command instance def initialize @layout = :single @gen_options = [:help, :version] end # Install options options do |opt| opt.on("--layout=LAYOUT", [:single, :delegator], "Select specific command layout (single,delegator)") do |l| @layout = l end opt.on('--options x,y,z', Array, "Automatically generate options for... (#{OPTION_HELPERS.keys.join(', ')})") do |list| @gen_options = list.collect{|s| s.to_sym} unless (unknown = @gen_options.reject{|s| OPTION_HELPERS.has_key?(s)}).empty? raise Quickl::InvalidOption, "Unknown option helpers: #{unknown.join(',')}" end end @header = true opt.on('--[no-]header', "Generate header (require rubygems + quickl)"){|value| @header = value } @footer = true opt.on('--[no-]footer', "Generate footer (command execution)"){|value| @footer = value } # Show the help and exit opt.on_tail("--help", "Show help") do raise Quickl::Help end # Show version and exit opt.on_tail("--version", "Show version") do raise Quickl::Exit, "#{Quickl.program_name} #{Quickl::VERSION} #{Quickl::COPYRIGHT}" end end # Loads template to use def template @template ||= File.expand_path("../../templates/#{@layout}.erb", __FILE__) end # Execute the command def execute(args) if args.size == 1 @cmd_name = args.shift erb = ERB.new(File.read(template)) $stdout.puts erb.result(binding) else raise Quickl::InvalidArgument, "Invalid arguments: #{args.join(' ')}" end end ### ERB scoping # Class name of the command to generate def cmd_class_name command2module(cmd_name) end def indent(str, offset) if offset >= 0 str.gsub(/^/, ' ' * offset) else str.gsub(/^ {0,#{-offset}}/, "") end end def tabto(str, offset) if str =~ /^( *)\S/ indent(str, offset - $1.length) else str end end # Returns option helpers to include def option_helpers @gen_options.collect{|s| tabto(OPTION_HELPERS[s], 0).strip} end end # class Quickl::Command::Main Quickl::Main.run(ARGV, __FILE__)