class SysCmd::Definition

This class has methods to build a command to be executed by the system

The target OS can be defined with the :os option, which accepts values :unix and :windows, and which defaults to the host system's type.

All the methods to define command arguments (option, file, etc.) accept options :only_on, :except_on to conditionally include the element depending on what the target OS is for the command.

Attributes

command[R]
shell[R]
stdin_data[R]

Public Class Methods

new(command, options = {}) click to toggle source
# File lib/sys_cmd.rb, line 20
def initialize(command, options = {})
  @shell = Shell.new(options)
  @options = options.dup
  @options.merge!(@options.delete(@shell.type) || {})
  @command = ''
  @command << (@options[command] || command)
  @last_arg = :command
  @stdin_data = nil
end

Public Instance Methods

argument(value, options = {}) click to toggle source

Add an unquoted argument to the command. This is not useful for commands executed directly, since the arguments are note interpreted by a shell in that case.

# File lib/sys_cmd.rb, line 185
def argument(value, options = {})
  return unless @shell.applicable?(options)
  value = value.to_s
  if @shell.requires_escaping?(value)
    value = @shell.escape_value(value)
  end
  @command << ' ' << value
  @last_arg = :argument
end
equal_file(filename, options = {}) click to toggle source

Add a filename to the command, attaching it with an equal sign to the previous option added.

option '-i'
equal_file 'path/output' # -i=path/output
# File lib/sys_cmd.rb, line 138
def equal_file(filename, options = {})
  return unless @shell.applicable?(options)
  raise "An option is required for equal_file" unless @last_arg == :option
  @command << '=' << @shell.escape_filename(filename)
  @last_arg = :file
end
equal_value(value, options = {}) click to toggle source

Add the value of an option, attaching it with an equal sign to the previous option added.

option '-x'
equal_value 123  # -x=123
# File lib/sys_cmd.rb, line 175
def equal_value(value, options = {})
  return unless @shell.applicable?(options)
  raise "An option is required for equal_value" unless @last_arg == :option
  @command << '=' << @shell.escape_value(value)
  @last_arg = :value
end
file(filename, options = {}) click to toggle source

Add a filename to the command.

file 'path/output'
# File lib/sys_cmd.rb, line 113
def file(filename, options = {})
  return unless @shell.applicable?(options)
  @command << ' ' << @shell.escape_filename(filename)
  @last_arg = :file
end
input(data) click to toggle source

Define data to be passed as standard input to the command

# File lib/sys_cmd.rb, line 196
def input(data)
  if @stdin_data
    @stdin_data += data.to_s
  else
    @stdin_data = data.to_s
  end
end
join_file(filename, options = {}) click to toggle source

Add a filename to the command, joinning it without a separator to the previous option added.

option '-i'
join_file 'path/output' # -ipath/output
# File lib/sys_cmd.rb, line 125
def join_file(filename, options = {})
  return unless @shell.applicable?(options)
  raise "An option is required for join_file" unless @last_arg == :option
  @command << @shell.escape_filename(filename)
  @last_arg = :file
end
join_value(value, options = {}) click to toggle source

Add the value of an option, joinning it without a separator to the previous option added.

option '-x'
join_value 123  # -x123
# File lib/sys_cmd.rb, line 162
def join_value(value, options = {})
  return unless @shell.applicable?(options)
  raise "An option is required for join_value" unless @last_arg == :option
  @command << @shell.escape_value(value)
  @last_arg = :value
end
option(option, *args) click to toggle source

Add an option to the command.

option '-x'    # flag-style option
option '--y'   # long option
option '/z'    # Windows-style option
options 'abc'  # unprefixed option

If the option :os_prefix is true then the default system option switch will be used.

option 'x', os_prefix: true # will produce -x or /x

A value can be given as an option and will be space-separated from the option name:

option '-x', value: 123 # -x 123

To avoid spacing the value use the :join_value option

option '-x', join_value: 123 # -x123

And to use an equal sign as separator, use :equal_value

option '-x', equal_value: 123 # -x=123

If the option value is a file name, use the analogous :file, :join_file or :equal_file options:

option '-i', file: 'path/filename'

Several of this options can be given simoultaneusly:

option '-d', join_value: 'x', equal_value: '1' # -dx=1
# File lib/sys_cmd.rb, line 70
def option(option, *args)
  options = args.pop if args.last.is_a?(Hash)
  options ||= {}
  raise "Invalid number of arguments (0 or 1 expected)" if args.size > 1
  return unless @shell.applicable?(options)
  value = args.shift || options[:value]
  if options[:os_prefix]
    option = @shell.option_switch + option
  else
    option = option.dup
  end
  if @shell.requires_escaping?(option)
    option = @shell.escape_value(option)
  end
  option << ' ' << @shell.escape_value(value) if value
  option << @shell.escape_value(options[:join_value]) if options[:join_value]
  option << '=' << @shell.escape_value(options[:equal_value]) if options[:equal_value]
  if file = options[:file]
    file_sep = ' '
  elsif file = options[:join_file]
    file_sep = ''
  elsif file = options[:equal_file]
    file_sep = '='
  end
  if file
    option << file_sep << @shell.escape_filename(file)
  end
  @command << ' ' << option
  @last_arg = :option
end
os_option(option, *args) click to toggle source

An os_option has automatically a OS-dependent prefix

# File lib/sys_cmd.rb, line 102
def os_option(option, *args)
  options = args.pop if args.last.is_a?(Hash)
  options ||= {}
  args.push options.merge(os_prefix: true)
  option option, *args
end
to_s() click to toggle source
# File lib/sys_cmd.rb, line 32
def to_s
  command
end
value(value, options = {}) click to toggle source

Add the value of an option (or a quoted argument)

option '-x'
join_value 123  # -x 123
# File lib/sys_cmd.rb, line 150
def value(value, options = {})
  return unless @shell.applicable?(options)
  @command << ' ' << @shell.escape_filename(value.to_s)
  @last_arg = :value
end