Class: SysCmd::Definition

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

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, options = {}) ⇒ Definition

Returns a new instance of Definition.



20
21
22
23
24
25
26
27
# 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
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



29
30
31
# File 'lib/sys_cmd.rb', line 29

def command
  @command
end

#shellObject (readonly)

Returns the value of attribute shell.



29
30
31
# File 'lib/sys_cmd.rb', line 29

def shell
  @shell
end

Instance Method Details

#argument(value, options = {}) ⇒ Object

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.



167
168
169
170
171
# File 'lib/sys_cmd.rb', line 167

def argument(value, options = {})
  return unless @shell.applicable?(options)
  @command << ' ' << value.to_s
  @last_arg = :argument
end

#equal_file(filename, options = {}) ⇒ Object

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


120
121
122
123
124
125
# File 'lib/sys_cmd.rb', line 120

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 = {}) ⇒ Object

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

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


157
158
159
160
161
162
# File 'lib/sys_cmd.rb', line 157

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 = {}) ⇒ Object

Add a filename to the command.

file 'path/output'


95
96
97
98
99
# File 'lib/sys_cmd.rb', line 95

def file(filename, options = {})
  return unless @shell.applicable?(options)
  @command << ' ' << @shell.escape_filename(filename)
  @last_arg = :file
end

#join_file(filename, options = {}) ⇒ Object

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

option '-i'
join_file 'path/output' # -ipath/output


107
108
109
110
111
112
# File 'lib/sys_cmd.rb', line 107

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 = {}) ⇒ Object

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

option '-x'
join_value 123  # -x123


144
145
146
147
148
149
# File 'lib/sys_cmd.rb', line 144

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) ⇒ Object

Add an option. If the option is nor prefixed by - or / then the default system option switch will be used.

option 'x' # will produce -x or /x
option '-x' # will always produce -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


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sys_cmd.rb', line 63

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 /\A[a-z]/i =~ option
    option = @shell.option_switch + option
  else
    option = option.dup
  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

#to_sObject



31
32
33
# File 'lib/sys_cmd.rb', line 31

def to_s
  command
end

#value(value, options = {}) ⇒ Object

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

option '-x'
join_value 123  # -x 123


132
133
134
135
136
# File 'lib/sys_cmd.rb', line 132

def value(value, options = {})
  return unless @shell.applicable?(options)
  @command << ' ' << @shell.escape_filename(value.to_s)
  @last_arg = :value
end