Class: Rwm::CLI

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

Constant Summary collapse

COMMANDS =
{
  "init"      => "Commands::Init",
  "bootstrap" => "Commands::Bootstrap",
  "new"       => "Commands::New",
  "info"      => "Commands::Info",
  "graph"     => "Commands::Graph",
  "check"     => "Commands::Check",
  "list"      => "Commands::List",
  "run"       => "Commands::Run",
  "affected"  => "Commands::Affected",
  "cache"     => "Commands::Cache"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



22
23
24
# File 'lib/rwm/cli.rb', line 22

def initialize(argv)
  @argv = argv.dup
end

Class Method Details

.run(argv) ⇒ Object



18
19
20
# File 'lib/rwm/cli.rb', line 18

def self.run(argv)
  new(argv).run
end

Instance Method Details

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rwm/cli.rb', line 26

def run
  parse_global_flags

  command_name = @argv.shift

  if command_name.nil? || %w[-h --help help].include?(command_name)
    print_help
    return 0
  end

  if %w[-v --version version].include?(command_name)
    puts "rwm #{Rwm::VERSION}"
    return 0
  end

  check_required_tools

  # Unknown commands are treated as task names: `rwm test` → `rwm run test`
  unless COMMANDS.key?(command_name)
    @argv.unshift(command_name)
    command_name = "run"
  end

  # Autoload the command
  require "rwm/commands/#{command_name}"
  const_name = COMMANDS[command_name]
  command_class = const_name.split("::").reduce(Rwm) { |mod, name| mod.const_get(name) }
  command_class.new(@argv).run
rescue Interrupt
  $stderr.puts "\nInterrupted."
  130
rescue Rwm::Error => e
  $stderr.puts "Error: #{e.message}"
  1
rescue StandardError => e
  $stderr.puts "Error: #{e.message}"
  Rwm.debug("#{e.class}: #{e.message}\n#{e.backtrace&.join("\n")}")
  1
end