Class: Xcopier::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



12
13
14
# File 'lib/xcopier/cli.rb', line 12

def initialize(args)
  @args = args
end

Class Method Details

.start(args) ⇒ Object



8
9
10
# File 'lib/xcopier/cli.rb', line 8

def self.start(args)
  new(args).run
end

Instance Method Details

#parseObject

rubocop:disable Metrics/CyclomaticComplexity,Metrics/AbcSize,Metrics/PerceivedComplexity,Metrics/MethodLength



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/xcopier/cli.rb', line 23

def parse # rubocop:disable Metrics/CyclomaticComplexity,Metrics/AbcSize,Metrics/PerceivedComplexity,Metrics/MethodLength
  @options = { copier_name: @args.shift, args: {} }
  @options[:copier] = @options[:copier_name].classify.safe_constantize

  parser = OptionParser.new do |opts|
    opts.banner = "Usage: xcopier copier_name [--arg1=value1 ...] [options]"

    opts.separator "\nCOPIER ARGUMENTS" if @options[:copier]&._arguments&.any?
    @options[:copier]&._arguments&.each do |arg|
      @options[:args][arg[:name]] = nil
      banner = "--#{arg[:name].to_s.gsub("_", "-").parameterize}"
      banner += "=VALUE" if arg[:type] != :boolean
      help = "#{arg[:name]} copier argument"
      help << " (comma separated list)" if arg[:list]
      opts.on(banner, help) do |value|
        @options[:args][arg[:name]] = value
      end
    end

    opts.separator "\nOPTIONS"
    opts.on("-h", "--help", "Show help message") do
      @options[:help] = true
    end

    opts.on("-v", "--verbose", "Run verbosely") do
      @options[:verbose] = true
    end
  end
  parser.parse(@args)

  if @options[:help]
    puts parser
    return false
  end

  if @options[:copier].nil?
    puts "ERROR: Copier not found\n\n"
    puts parser
    return false
  end

  if @options[:args].values.any?(&:nil?)
    puts "ERROR: Missing argument values\n\n"
    puts parser
    return false
  end

  true
end

#runObject



16
17
18
19
20
21
# File 'lib/xcopier/cli.rb', line 16

def run
  valid = parse
  return unless valid

  @options[:copier].new(**@options[:args]).run
end