-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathkintsugi.rb
48 lines (40 loc) · 1.04 KB
/
kintsugi.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true
# Copyright (c) 2022 Lightricks. All rights reserved.
# Created by Ben Yohay.
require_relative "kintsugi/cli"
require_relative "kintsugi/error"
require_relative "kintsugi/merge"
module Kintsugi
class << self
def run(arguments)
first_argument = arguments[0]
cli = CLI.new
command =
if name_of_subcommand?(cli.subcommands, first_argument)
arguments.shift
cli.subcommands[first_argument]
else
cli.root_command
end
options = parse_options!(command, arguments)
begin
command.action.call(options, arguments)
rescue ArgumentError => e
puts "#{e.class}: #{e}"
raise
rescue Kintsugi::MergeError => e
puts e
raise
end
end
private
def parse_options!(command, arguments)
options = {}
command.option_parser.parse!(arguments, into: options)
options
end
def name_of_subcommand?(subcommands, argument)
subcommands.include?(argument)
end
end
end