Class: CommandBuilder
- Inherits:
-
Object
- Object
- CommandBuilder
- Defined in:
- lib/command-builder.rb
Overview
Represents one command line command with arguments and parameters.
Instance Attribute Summary collapse
-
#args ⇒ Array
Holds arguments array.
-
#command ⇒ String, Symbol
Holds command name.
-
#separators ⇒ Array
Holds separators matrix.
Instance Method Summary collapse
-
#[](name) ⇒ Array
Returns array of argument pairs with given name.
-
#add(option, value = nil) ⇒ Object
(also: #<<)
Adds an item to command.
-
#argument(name, value = nil) ⇒ Object
(also: #arg, #[]=)
Adds argument to command.
-
#empty? ⇒ Boolean
Returns whether this command has any either arguments or parameters set.
-
#execute(&block) ⇒ String
(also: #exec, #exec!, #execute!)
Executes the command.
-
#initialize(command, separators = ["-", " ", "--", "="]) ⇒ CommandBuilder
constructor
Constructor.
-
#parameter(value) ⇒ Object
(also: #param)
Adds parameter to command.
-
#parameters(values = nil) ⇒ Object
(also: #params)
Adds multiple parameters to command at once.
-
#quote(value) ⇒ String
Quotes value for use in command line.
-
#reset! ⇒ CommandBuilder
(also: #reset)
Resets the arguments and parameters so prepares it for new build.
-
#to_s ⇒ String
Converts command to string.
Constructor Details
#initialize(command, separators = ["-", " ", "--", "="]) ⇒ CommandBuilder
Constructor.
65 66 67 68 69 |
# File 'lib/command-builder.rb', line 65 def initialize(command, separators = ["-", " ", "--", "="]) @command = command @separators = separators self.reset! end |
Instance Attribute Details
#args ⇒ Array
Holds arguments array. Each item is array pair with argument name and value.
48 49 50 |
# File 'lib/command-builder.rb', line 48 def args @args end |
#command ⇒ String, Symbol
Holds command name.
20 21 22 |
# File 'lib/command-builder.rb', line 20 def command @command end |
#separators ⇒ Array
Holds separators matrix. It’s four-items array:
-
short argument separator,
-
short argument name/value separator,
-
long argument separator,
-
short argument name/value separator.
Here is some example in the same order as in command:
# ["-", " ", "--", "="]
command -s something --long=something
38 39 40 |
# File 'lib/command-builder.rb', line 38 def separators @separators end |
Instance Method Details
#[](name) ⇒ Array
Returns array of argument pairs with given name.
110 111 112 |
# File 'lib/command-builder.rb', line 110 def [](name) @args.select { |k, v| name == k } end |
#add(option) ⇒ Object #add(options) ⇒ Object #add(option, value = nil) ⇒ Object Also known as: <<
Adds an item to command. If option is Symbol or value isn’t nil, it will apply the item as an argument, in otherwise, it will treat as an symbol.
176 177 178 179 180 181 182 183 184 |
# File 'lib/command-builder.rb', line 176 def add(option, value = nil) if option.kind_of? Symbol or not value.nil? self.argument(option, value) elsif option.kind_of? Array self.parameters(option) else self.parameter(option) end end |
#argument(name, value = nil) ⇒ Object Also known as: arg, []=
Adds argument to command.
One-letter arguments will be treated as “short” arguments for the syntax purposes, other as “long”. See #separators.
95 96 97 98 |
# File 'lib/command-builder.rb', line 95 def argument(name, value = nil) @args << [name, value] @order << :argument end |
#empty? ⇒ Boolean
Returns whether this command has any either arguments or parameters set.
281 282 283 |
# File 'lib/command-builder.rb', line 281 def empty? @args.empty? and @params.empty? end |
#execute(&block) ⇒ String Also known as: exec, exec!, execute!
Executes the command. If block given, takes it output of the command and runs it asynchronously using EventMachine.
221 222 223 224 225 226 227 228 229 230 |
# File 'lib/command-builder.rb', line 221 def execute(&block) callback = nil if not block.nil? callback = Proc::new do |out| block.call(out, out.strip.empty?) end end Pipe::run(self.to_s, &callback) end |
#parameter(value) ⇒ Object Also known as: param
Adds parameter to command.
119 120 121 122 |
# File 'lib/command-builder.rb', line 119 def parameter(value) @params << value @order << :parameter end |
#parameters ⇒ Array #parameters(values) ⇒ Object Also known as: params
Adds multiple parameters to command at once. If no values are given, returns the current parameters array.
139 140 141 142 143 144 145 146 147 |
# File 'lib/command-builder.rb', line 139 def parameters(values = nil) if values.nil? return @params else @params += values orders = [:parameter] * values.length @order.push(*orders) end end |
#quote(value) ⇒ String
Quotes value for use in command line.
Uses some heuristic for setting the right quotation. If both “ and ‘ are found, escapes ’ by \ and quotes by ”. If only one of them found, escapes by the second one. If space found in the string, quotes by “ too.
207 208 209 210 |
# File 'lib/command-builder.rb', line 207 def quote(value) value = value.to_s Shellwords::escape(value) end |
#reset! ⇒ CommandBuilder Also known as: reset
Resets the arguments and parameters so prepares it for new build.
266 267 268 269 270 271 |
# File 'lib/command-builder.rb', line 266 def reset! @args = [ ] @params = [ ] @order = [ ] self end |
#to_s ⇒ String
Converts command to string.
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/command-builder.rb', line 241 def to_s cmd = @command.to_s.gsub(" ", "\\ ") args = @args.dup params = @params.dup @order.each do |type| if type == :argument name, value = args.shift __add_arg(cmd, name, value) else param = params.shift cmd << " " << self.quote(param.to_s) end end return cmd end |