class Scallop::CommandBuilder
Implements command building interface with immutability.
Public Class Methods
new()
click to toggle source
# File lib/scallop/command_builder.rb, line 6 def initialize @params = {} @cmd = [] end
Public Instance Methods
cmd(*cmd)
click to toggle source
# File lib/scallop/command_builder.rb, line 19 def cmd(*cmd) dup .tap do |instance| instance.instance_eval { @cmd += cmd } end .freeze end
read_cmd()
click to toggle source
# File lib/scallop/command_builder.rb, line 27 def read_cmd @cmd end
run(args = {})
click to toggle source
# File lib/scallop/command_builder.rb, line 45 def run(args = {}) Executor.run(to_command, args) end
run!()
click to toggle source
# File lib/scallop/command_builder.rb, line 49 def run! Executor.run!(to_command) end
set(params)
click to toggle source
# 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)
click to toggle source
# 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_command()
click to toggle source
# 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)
click to toggle source
# File lib/scallop/command_builder.rb, line 41 def |(other) cmd(:|, other.read_cmd) end
Private Instance Methods
build_command()
click to toggle source
# File lib/scallop/command_builder.rb, line 68 def build_command [*@cmd] .flatten .map do |cmd_part| case cmd_part when Param value = @params[cmd_part.key] raise Errors::ValidationFailed, "value for param '#{cmd_part.key}' not set" if value.nil? Shellwords.escape(value.to_s) when :| cmd_part else Shellwords.escape(cmd_part.to_s) end end .join(' ') end
build_prefix()
click to toggle source
# File lib/scallop/command_builder.rb, line 61 def build_prefix case @sudo when true then 'sudo' when String, Symbol then "sudo -u #{@sudo}" end end