Class: SysCmd::Definition
- Inherits:
-
Object
- Object
- SysCmd::Definition
- 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
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#shell ⇒ Object
readonly
Returns the value of attribute shell.
-
#stdin_data ⇒ Object
readonly
Returns the value of attribute stdin_data.
Instance Method Summary collapse
-
#argument(value, options = {}) ⇒ Object
Add an unquoted argument to the command.
-
#equal_file(filename, options = {}) ⇒ Object
Add a filename to the command, attaching it with an equal sign to the previous option added.
-
#equal_value(value, options = {}) ⇒ Object
Add the value of an option, attaching it with an equal sign to the previous option added.
-
#file(filename, options = {}) ⇒ Object
Add a filename to the command.
-
#initialize(command, options = {}) ⇒ Definition
constructor
A new instance of Definition.
-
#input(data) ⇒ Object
Define data to be passed as standard input to the command.
-
#join_file(filename, options = {}) ⇒ Object
Add a filename to the command, joinning it without a separator to the previous option added.
-
#join_value(value, options = {}) ⇒ Object
Add the value of an option, joinning it without a separator to the previous option added.
-
#option(option, *args) ⇒ Object
Add an option to the command.
-
#os_option(option, *args) ⇒ Object
An
os_option
has automatically a OS-dependent prefix. - #to_s ⇒ Object
-
#value(value, options = {}) ⇒ Object
Add the value of an option (or a quoted argument).
Constructor Details
#initialize(command, options = {}) ⇒ Definition
Returns a new instance of Definition.
20 21 22 23 24 25 26 27 28 |
# File 'lib/sys_cmd.rb', line 20 def initialize(command, = {}) @shell = Shell.new() @options = .dup @options.merge!(@options.delete(@shell.type) || {}) @command = '' @command << (@options[command] || command) @last_arg = :command @stdin_data = nil end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
30 31 32 |
# File 'lib/sys_cmd.rb', line 30 def command @command end |
#shell ⇒ Object (readonly)
Returns the value of attribute shell.
30 31 32 |
# File 'lib/sys_cmd.rb', line 30 def shell @shell end |
#stdin_data ⇒ Object (readonly)
Returns the value of attribute stdin_data.
30 31 32 |
# File 'lib/sys_cmd.rb', line 30 def stdin_data @stdin_data 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.
185 186 187 188 189 190 191 192 193 |
# File 'lib/sys_cmd.rb', line 185 def argument(value, = {}) return unless @shell.applicable?() 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 = {}) ⇒ 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
138 139 140 141 142 143 |
# File 'lib/sys_cmd.rb', line 138 def equal_file(filename, = {}) return unless @shell.applicable?() 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
175 176 177 178 179 180 |
# File 'lib/sys_cmd.rb', line 175 def equal_value(value, = {}) return unless @shell.applicable?() 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'
113 114 115 116 117 |
# File 'lib/sys_cmd.rb', line 113 def file(filename, = {}) return unless @shell.applicable?() @command << ' ' << @shell.escape_filename(filename) @last_arg = :file end |
#input(data) ⇒ Object
Define data to be passed as standard input to the command
196 197 198 199 200 201 202 |
# 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 = {}) ⇒ 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
125 126 127 128 129 130 |
# File 'lib/sys_cmd.rb', line 125 def join_file(filename, = {}) return unless @shell.applicable?() 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
162 163 164 165 166 167 |
# File 'lib/sys_cmd.rb', line 162 def join_value(value, = {}) return unless @shell.applicable?() 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 to the command.
option '-x' # flag-style option
option '--y' # long option
option '/z' # Windows-style option
'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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/sys_cmd.rb', line 70 def option(option, *args) = args.pop if args.last.is_a?(Hash) ||= {} raise "Invalid number of arguments (0 or 1 expected)" if args.size > 1 return unless @shell.applicable?() value = args.shift || [:value] if [: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([:join_value]) if [:join_value] option << '=' << @shell.escape_value([:equal_value]) if [:equal_value] if file = [:file] file_sep = ' ' elsif file = [:join_file] file_sep = '' elsif file = [: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) ⇒ Object
An os_option
has automatically a OS-dependent prefix
102 103 104 105 106 107 |
# File 'lib/sys_cmd.rb', line 102 def os_option(option, *args) = args.pop if args.last.is_a?(Hash) ||= {} args.push .merge(os_prefix: true) option option, *args end |
#to_s ⇒ Object
32 33 34 |
# File 'lib/sys_cmd.rb', line 32 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
150 151 152 153 154 |
# File 'lib/sys_cmd.rb', line 150 def value(value, = {}) return unless @shell.applicable?() @command << ' ' << @shell.escape_filename(value.to_s) @last_arg = :value end |