class Wright::CLI

Wright command-line interface.

Attributes

commands[R]
dry_run[RW]
log_level[RW]
main[R]
parser[RW]
quit[RW]
requires[R]

Public Class Methods

new(main) click to toggle source
# File lib/wright/cli.rb, line 7
def initialize(main)
  @commands = []
  @requires = []
  @main = main
  set_up_parser
end

Public Instance Methods

run(argv) click to toggle source

Runs a wright script with the supplied arguments.

@param argv [Array<String>] the arguments passed to bin/wright

# File lib/wright/cli.rb, line 17
def run(argv)
  arguments = parse(argv)
  return if quit

  Wright.activate_dry_run if dry_run
  Wright.log.level = log_level if log_level
  main.extend Wright::DSL
  requires.each { |r| require r }

  run_script(arguments)
end

Private Instance Methods

parse(argv) click to toggle source
# File lib/wright/cli.rb, line 34
def parse(argv)
  # use OptionParser#order! instead of #parse! so CLI#run does not
  # consume --arguments passed to wright scripts
  parser.order!(argv)
end
run_script(arguments) click to toggle source
# File lib/wright/cli.rb, line 40
def run_script(arguments)
  if commands.empty? && arguments.any?
    script = File.expand_path(arguments.shift)
    load script
  else
    cmds = commands.empty? ? $stdin.read : commands.join("\n")
    main.instance_eval(cmds, '<main>', 1)
  end
end
set_up_command_option() click to toggle source
# File lib/wright/cli.rb, line 59
def set_up_command_option
  parser.on('-e COMMAND', 'Run COMMAND') do |e|
    commands << e
  end
end
set_up_dry_run_option() click to toggle source
# File lib/wright/cli.rb, line 72
def set_up_dry_run_option
  parser.on('-n', '--dry-run', 'Enable dry-run mode') do
    self.dry_run = true
  end
end
set_up_parser() click to toggle source
# File lib/wright/cli.rb, line 50
def set_up_parser
  self.parser = OptionParser.new
  set_up_command_option
  set_up_require_option
  set_up_dry_run_option
  set_up_verbosity_options
  set_up_version_option
end
set_up_require_option() click to toggle source
# File lib/wright/cli.rb, line 65
def set_up_require_option
  parser.on('-r LIBRARY',
            'Require LIBRARY before running the script') do |r|
    requires << r
  end
end
set_up_verbosity_options() click to toggle source
# File lib/wright/cli.rb, line 78
def set_up_verbosity_options
  parser.on('-v', '--verbose', 'Increase verbosity') do
    self.log_level = Wright::Logger::DEBUG
  end

  parser.on('-q', '--quiet', 'Decrease verbosity') do
    self.log_level = Wright::Logger::ERROR
  end
end
set_up_version_option() click to toggle source
# File lib/wright/cli.rb, line 88
def set_up_version_option
  parser.on_tail('--version', 'Show wright version') do
    puts "wright version #{Wright::VERSION}"
    self.quit = true
  end
end