Class: Scallop::CommandBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/scallop/command_builder.rb

Overview

Implements command building interface with immutability.

Instance Method Summary collapse

Constructor Details

#initializeCommandBuilder


6
7
8
9
# File 'lib/scallop/command_builder.rb', line 6

def initialize
  @params = {}
  @cmd = []
end

Instance Method Details

#cmd(*cmd) ⇒ Object


19
20
21
22
23
24
25
# File 'lib/scallop/command_builder.rb', line 19

def cmd(*cmd)
  dup
    .tap do |instance|
      instance.instance_eval { @cmd += cmd }
    end
    .freeze
end

#read_cmdObject


27
28
29
# File 'lib/scallop/command_builder.rb', line 27

def read_cmd
  @cmd
end

#runObject


45
46
47
# File 'lib/scallop/command_builder.rb', line 45

def run
  Executor.run(to_command)
end

#run!Object


49
50
51
# File 'lib/scallop/command_builder.rb', line 49

def run!
  Executor.run!(to_command)
end

#set(params) ⇒ Object


31
32
33
34
35
36
37
38
39
# File 'lib/scallop/command_builder.rb', line 31

def set(params)
  new_params = @params.merge(params)

  dup
    .tap do |instance|
      instance.instance_eval { @params = new_params }
    end
    .freeze
end

#sudo(sudo = true) ⇒ Object


11
12
13
14
15
16
17
# File 'lib/scallop/command_builder.rb', line 11

def sudo(sudo = true)
  dup
    .tap do |instance|
      instance.instance_eval { @sudo = sudo }
    end
    .freeze
end

#to_commandObject


53
54
55
56
57
# File 'lib/scallop/command_builder.rb', line 53

def to_command
  raise Errors::ValidationFailed, 'cmd missing' if @cmd.empty?

  [build_prefix, build_command].compact.join(' ')
end

#|(other) ⇒ Object


41
42
43
# File 'lib/scallop/command_builder.rb', line 41

def |(other)
  cmd(:|, other.read_cmd)
end