Sha256: 9c124410d30f99bc1916a9292fb1156d8994007dcfe992e6b58dcd642eb051c6

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require 'optparse'
require 'wright'

module Wright
  # Wright command-line interface.
  class CLI
    def initialize(main)
      @commands = []
      @main = main
      @parser = option_parser
    end

    # Runs a wright script with the supplied arguments.
    #
    # @param argv [Array<String>] the arguments passed to bin/wright
    def run(argv)
      arguments = parse(argv)
      return if @quit

      Wright.log.level = @log_level if @log_level
      @main.extend Wright::DSL

      run_script(arguments)
    end

    private

    attr_reader :commands
    attr_reader :log_level

    def parse(argv)
      # use OptionParser#order! instead of #parse! so CLI#run does not
      # consume --arguments passed to wright scripts
      @parser.order!(argv)
    end

    def run_script(arguments)
      if @commands.empty? && arguments.any?
        script = File.expand_path(arguments.shift)
        load script
      else
        commands = @commands.empty? ? $stdin.read : @commands.join("\n")
        @main.instance_eval(commands, '<main>', 1)
      end
    end

    def option_parser
      OptionParser.new do |opts|
        opts.on('-e COMMAND', 'Run COMMAND') do |e|
          @commands << e
        end

        opts.on('-v', '--verbose', 'Increase verbosity') do
          @log_level = Wright::Logger::DEBUG
        end

        opts.on('-q', '--quiet', 'Decrease verbosity') do
          @log_level = Wright::Logger::ERROR
        end

        opts.on_tail('--version', 'Show wright version') do
          puts "wright version #{Wright::VERSION}"
          @quit = true
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wright-0.3.2 lib/wright/cli.rb