Class: Wright::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/wright/cli.rb

Overview

Wright command-line interface.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(main) ⇒ CLI

Returns a new instance of CLI.


7
8
9
10
11
12
# File 'lib/wright/cli.rb', line 7

def initialize(main)
  @commands = []
  @requires = []
  @main = main
  set_up_parser
end

Instance Attribute Details

#commandsObject (readonly, private)

Returns the value of attribute commands


31
32
33
# File 'lib/wright/cli.rb', line 31

def commands
  @commands
end

#dry_runObject (readonly, private)

Returns the value of attribute dry_run


31
32
33
# File 'lib/wright/cli.rb', line 31

def dry_run
  @dry_run
end

#log_levelObject (readonly, private)

Returns the value of attribute log_level


31
32
33
# File 'lib/wright/cli.rb', line 31

def log_level
  @log_level
end

#requiresObject (readonly, private)

Returns the value of attribute requires


31
32
33
# File 'lib/wright/cli.rb', line 31

def requires
  @requires
end

Instance Method Details

#parse(argv) ⇒ Object (private)


33
34
35
36
37
# File 'lib/wright/cli.rb', line 33

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(argv) ⇒ Object

Runs a wright script with the supplied arguments.

Parameters:

  • argv (Array<String>)

    the arguments passed to bin/wright


17
18
19
20
21
22
23
24
25
26
27
# 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

#run_script(arguments) ⇒ Object (private)


39
40
41
42
43
44
45
46
47
# File 'lib/wright/cli.rb', line 39

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

#set_up_command_optionObject (private)


58
59
60
61
62
# File 'lib/wright/cli.rb', line 58

def set_up_command_option
  @parser.on('-e COMMAND', 'Run COMMAND') do |e|
    @commands << e
  end
end

#set_up_dry_run_optionObject (private)


71
72
73
74
75
# File 'lib/wright/cli.rb', line 71

def set_up_dry_run_option
  @parser.on('-n', '--dry-run', 'Enable dry-run mode') do
    @dry_run = true
  end
end

#set_up_parserObject (private)


49
50
51
52
53
54
55
56
# File 'lib/wright/cli.rb', line 49

def set_up_parser
  @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_optionObject (private)


64
65
66
67
68
69
# File 'lib/wright/cli.rb', line 64

def set_up_require_option
  @parser.on('-r LIBRARY',
             'Require LIBRARY before running the script') do |r|
    @requires << r
  end
end

#set_up_verbosity_optionsObject (private)


77
78
79
80
81
82
83
84
85
# File 'lib/wright/cli.rb', line 77

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

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

#set_up_version_optionObject (private)


87
88
89
90
91
92
# File 'lib/wright/cli.rb', line 87

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