Class: CmdLine::CommandLine

Inherits:
Object
  • Object
show all
Includes:
Logue::Loggable
Defined in:
lib/cmdline/line.rb

Direct Known Subclasses

CachingCommandLine

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, debug: false) ⇒ CommandLine



16
17
18
# File 'lib/cmdline/line.rb', line 16

def initialize *args, debug: false
  @args = args.dup
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



11
12
13
# File 'lib/cmdline/line.rb', line 11

def args
  @args
end

#errorObject (readonly)

Returns the value of attribute error.



13
14
15
# File 'lib/cmdline/line.rb', line 13

def error
  @error
end

#outputObject (readonly)

Returns the value of attribute output.



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

def output
  @output
end

#statusObject (readonly)

Returns the value of attribute status.



14
15
16
# File 'lib/cmdline/line.rb', line 14

def status
  @status
end

Instance Method Details

#<<(arg) ⇒ Object



20
21
22
# File 'lib/cmdline/line.rb', line 20

def << arg
  @args << arg
end

#executeObject



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
# File 'lib/cmdline/line.rb', line 24

def execute
  cmd = to_command
  debug "cmd: #{cmd}"
  
  Open3.popen3(cmd) do |stdin, stdout, stderr, wthr|
    @output = stdout.readlines
    @error = stderr.readlines
    @status = wthr.value
  end

  if false && @output
    puts "output"
    @output.each_with_index do |line, idx|
      debug "output[#{idx}]: #{line}"
    end
  end
  
  if false && @error
    puts "error"
    @error.each_with_index do |line, idx|
      debug "error[#{idx}]: #{line}"
    end
  end

  debug "@status: #{@status}"
  
  @output
end

#to_commandObject



53
54
55
# File 'lib/cmdline/line.rb', line 53

def to_command
  @args.join ' '
end