Class: ObjectiveCommand::Commands::Command
- Defined in:
- lib/objective_command/commands/command.rb
Constant Summary collapse
- @@default_runners =
Running methods.
{}
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#command ⇒ Object
Returns the value of attribute command.
-
#dir ⇒ Object
Returns the value of attribute dir.
-
#error ⇒ Object
Returns the value of attribute error.
-
#input ⇒ Object
Returns the value of attribute input.
-
#open_mode ⇒ Object
Returns the value of attribute open_mode.
-
#output ⇒ Object
Returns the value of attribute output.
Instance Method Summary collapse
-
#+(rhs) ⇒ Object
Chain two commands (like a ‘;’ in shell), and return a new one.
-
#<(arg) ⇒ Object
Return a new command with the given command input.
-
#<<(arg) ⇒ Object
Add an argument to a command.
-
#>(arg) ⇒ Object
Return a new command with the given command output.
-
#>>(arg) ⇒ Object
Like > but open the output in append mode.
-
#[](*args) ⇒ Object
Supply return a new commands with these arguments added.
-
#arg_string ⇒ Object
Misc.
-
#exec(*options) ⇒ Object
Use the ExecRunner.
-
#expand ⇒ Object
Use Kernel#‘ and return the resulting string.
- #fork(*options) ⇒ Object
- #freeze ⇒ Object
-
#initialize(command_name, *args) ⇒ Command
constructor
Construction methods.
- #inspect_for_unified_matchers ⇒ Object
- #instanciate_args ⇒ Object
- #instanciate_args! ⇒ Object
- #popen(*options) ⇒ Object
-
#ruby ⇒ Object
FIXME design me! FIXME make me a runner.
-
#run(runner = @runner) ⇒ Object
Use a ObjectiveCommand::Runners::Runner and return a ObjectiveCommand::Datas::Data.
- #run_with_options(runner_class, options) ⇒ Object
-
#sh ⇒ Object
Use Kernel#system() but with a string to have the shell expansion.
-
#sh! ⇒ Object
Use Kernel#exec() but with a string to have the shell expansion.
- #sh_args ⇒ Object
-
#sys(*options) ⇒ Object
FIXME test me.
-
#system(*options) ⇒ Object
Simulate Kernel#system() to run the command (No shell expansion).
- #to_a ⇒ Object
- #to_ocmd ⇒ Object
-
#to_s ⇒ Object
Conversion methods.
- #to_sh ⇒ Object
- #whereis(path = ) ⇒ Object
-
#|(rhs) ⇒ Object
Pipe two commands.
Constructor Details
#initialize(command_name, *args) ⇒ Command
Construction methods.
24 25 26 27 28 29 30 |
# File 'lib/objective_command/commands/command.rb', line 24 def initialize ( command_name, *args ) @command = command_name @args = args.flatten @open_mode = :w @dir = nil @input, @output, @error = nil, nil, nil end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
17 18 19 |
# File 'lib/objective_command/commands/command.rb', line 17 def args @args end |
#command ⇒ Object
Returns the value of attribute command.
12 13 14 |
# File 'lib/objective_command/commands/command.rb', line 12 def command @command end |
#dir ⇒ Object
Returns the value of attribute dir.
13 14 15 |
# File 'lib/objective_command/commands/command.rb', line 13 def dir @dir end |
#error ⇒ Object
Returns the value of attribute error.
16 17 18 |
# File 'lib/objective_command/commands/command.rb', line 16 def error @error end |
#input ⇒ Object
Returns the value of attribute input.
14 15 16 |
# File 'lib/objective_command/commands/command.rb', line 14 def input @input end |
#open_mode ⇒ Object
Returns the value of attribute open_mode.
18 19 20 |
# File 'lib/objective_command/commands/command.rb', line 18 def open_mode @open_mode end |
#output ⇒ Object
Returns the value of attribute output.
15 16 17 |
# File 'lib/objective_command/commands/command.rb', line 15 def output @output end |
Instance Method Details
#+(rhs) ⇒ Object
Chain two commands (like a ‘;’ in shell), and return a new one.
132 133 134 135 136 137 138 139 |
# File 'lib/objective_command/commands/command.rb', line 132 def + ( rhs ) case rhs when Command Seq.new(self, rhs) else self[rhs] end end |
#<(arg) ⇒ Object
Return a new command with the given command input.
143 144 145 146 147 |
# File 'lib/objective_command/commands/command.rb', line 143 def < ( arg ) cmd = dup cmd.input = arg cmd end |
#<<(arg) ⇒ Object
Add an argument to a command. cmd = Command.new(‘ls’) cmd << ‘-la’ cmd.sh
178 179 180 181 |
# File 'lib/objective_command/commands/command.rb', line 178 def << ( arg ) @args << arg self end |
#>(arg) ⇒ Object
Return a new command with the given command output.
160 161 162 163 |
# File 'lib/objective_command/commands/command.rb', line 160 def > ( arg ) out, err = (arg.is_a?(Array))? arg : [arg, nil] add_outputs(out, err) end |
#>>(arg) ⇒ Object
Like > but open the output in append mode.
167 168 169 170 171 |
# File 'lib/objective_command/commands/command.rb', line 167 def >> ( arg ) out, err = (arg.is_a?(Array))? arg : [arg, nil] @open_mode = :a add_outputs(out, err) end |
#[](*args) ⇒ Object
Supply return a new commands with these arguments added.
124 125 126 127 128 |
# File 'lib/objective_command/commands/command.rb', line 124 def [] ( *args ) cmd = dup cmd.args += args.flatten cmd end |
#arg_string ⇒ Object
Misc
189 190 191 |
# File 'lib/objective_command/commands/command.rb', line 189 def arg_string @args.map { |arg| arg.to_s.to_sh }.join(' ') end |
#exec(*options) ⇒ Object
Use the ExecRunner.
54 55 56 |
# File 'lib/objective_command/commands/command.rb', line 54 def exec ( * ) (Runners::Exec, .flatten) end |
#expand ⇒ Object
Use Kernel#‘ and return the resulting string. FIXME make me a runner
81 82 83 |
# File 'lib/objective_command/commands/command.rb', line 81 def `#{to_sh}` end |
#fork(*options) ⇒ Object
91 92 93 |
# File 'lib/objective_command/commands/command.rb', line 91 def fork ( * ) (Runners::Fork, .flatten) end |
#freeze ⇒ Object
259 260 261 262 263 264 |
# File 'lib/objective_command/commands/command.rb', line 259 def freeze super for i in [@command, @dir, @input, @output, @error, @args] do i.freeze end end |
#inspect_for_unified_matchers ⇒ Object
216 217 218 |
# File 'lib/objective_command/commands/command.rb', line 216 def inspect_for_unified_matchers "#{to_s.inspect}.to_ocmd" end |
#instanciate_args ⇒ Object
236 237 238 239 240 |
# File 'lib/objective_command/commands/command.rb', line 236 def instanciate_args copy = dup copy.instanciate_args! copy end |
#instanciate_args! ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/objective_command/commands/command.rb', line 221 def instanciate_args! if defined? @input and @input and @args.include? '%i' @args.map! { |a| (a == '%i')? @input : a } @input = nil end if defined? @output and @output and @args.include? '%o' @args.map! { |a| (a == '%o')? @output : a } @output = nil end if defined? @error and @error and @args.include? '%e' @args.map! { |a| (a == '%e')? @error : a } @error = nil end end |
#popen(*options) ⇒ Object
86 87 88 |
# File 'lib/objective_command/commands/command.rb', line 86 def popen ( * ) (Runners::Popen, .flatten) end |
#ruby ⇒ Object
FIXME design me! FIXME make me a runner
98 99 |
# File 'lib/objective_command/commands/command.rb', line 98 def ruby end |
#run(runner = @runner) ⇒ Object
Use a ObjectiveCommand::Runners::Runner and return a ObjectiveCommand::Datas::Data.
103 104 105 106 107 108 |
# File 'lib/objective_command/commands/command.rb', line 103 def run ( runner=@runner ) unless runner.respond_to? :run raise ArgumentError, "need a runner not: #{runner.inspect}" end runner.run(self) end |
#run_with_options(runner_class, options) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/objective_command/commands/command.rb', line 37 def ( runner_class, ) if .empty? runner = @@default_runners[runner_class] ||= runner_class.new else runner = runner_class.new. end run(runner) end |
#sh ⇒ Object
Use Kernel#system() but with a string to have the shell expansion. FIXME make me a runner
67 68 69 |
# File 'lib/objective_command/commands/command.rb', line 67 def sh Kernel.system(to_sh) end |
#sh! ⇒ Object
Use Kernel#exec() but with a string to have the shell expansion. FIXME make me a runner
74 75 76 |
# File 'lib/objective_command/commands/command.rb', line 74 def sh! Kernel.exec(to_sh) end |
#sh_args ⇒ Object
242 243 244 245 246 247 248 |
# File 'lib/objective_command/commands/command.rb', line 242 def sh_args args = '' [ ['<', input], ['>', output], ['2>', error] ].each do |str, io| args += " #{str} #{io.to_s.to_sh}" if io and not (io.respond_to? :pipe? and io.pipe?) end args end |
#sys(*options) ⇒ Object
FIXME test me
60 61 62 |
# File 'lib/objective_command/commands/command.rb', line 60 def sys ( * ) (Runners::System, [:v, :r] + .flatten) end |
#system(*options) ⇒ Object
Simulate Kernel#system() to run the command (No shell expansion).
48 49 50 |
# File 'lib/objective_command/commands/command.rb', line 48 def system ( * ) (Runners::System, .flatten) end |
#to_a ⇒ Object
251 252 253 |
# File 'lib/objective_command/commands/command.rb', line 251 def to_a [@command.dup, *@args.map { |arg| arg.to_s }] end |
#to_ocmd ⇒ Object
255 256 257 |
# File 'lib/objective_command/commands/command.rb', line 255 def to_ocmd self end |
#to_s ⇒ Object
Conversion methods
202 203 204 205 206 207 |
# File 'lib/objective_command/commands/command.rb', line 202 def to_s str = @command.to_s.to_sh arg_str = arg_string str += ' ' + arg_str unless arg_str.empty? str end |
#to_sh ⇒ Object
210 211 212 213 |
# File 'lib/objective_command/commands/command.rb', line 210 def to_sh cmd = instanciate_args "#{cmd.to_s}#{cmd.sh_args}" end |
#whereis(path = ) ⇒ Object
193 194 195 |
# File 'lib/objective_command/commands/command.rb', line 193 def whereis ( path=ENV['PATH'] ) # FIXME end |