Class: Pamyu::CLI

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

Constant Summary collapse

COPY =
"-"

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ CLI

Returns a new instance of CLI.



6
7
8
# File 'lib/pamyu/cli.rb', line 6

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

Instance Method Details

#executeObject



10
11
12
13
14
# File 'lib/pamyu/cli.rb', line 10

def execute
  command, options = parse_argv
  out, err = open_files(options)
  Executor.new(command, out, err).execute
end

#new_option_parser(options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/pamyu/cli.rb', line 26

def new_option_parser(options = {})
  OptionParser.new do |op|
    op.on "-o", "--out FILE" do |file|
      options[:out] = file
    end
    op.on "-e", "--err FILE" do |file|
      options[:err] = file
    end
  end
end

#open(file) ⇒ Object



63
64
65
# File 'lib/pamyu/cli.rb', line 63

def open(file)
  File.open(file, "wb")
end

#open_files(options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pamyu/cli.rb', line 39

def open_files(options)
  op_out = options.delete(:out)
  op_err = options.delete(:err)

  if op_out && op_err
    if op_out == op_err
      out = err = open(op_out)
    elsif op_err == COPY
      out = err = open(op_out)
    elsif op_out == COPY
      out = err = open(op_err)
    else
      out = open(op_out)
      err = open(op_err)
    end
  elsif op_out
    out = open(op_out)
  elsif op_err
    err = open(op_err)
  end

  [out, err]
end

#parse_argvObject



16
17
18
19
20
21
22
23
24
# File 'lib/pamyu/cli.rb', line 16

def parse_argv
  options = {}
  op = new_option_parser(options)
  op.permute!(@argv)

  abort op.help if @argv.empty?

  [@argv, options]
end