Class: Rubsh::Command

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

Overview

Represents an un-run system program, like "ls" or "cd". Because it represents the program itself (and not a running instance of it), it should hold very little state. In fact, the only state it does hold is baked options.

When a Command object is called, the result that is returned is a RunningCommand object, which represents the Command put into an execution state.

Instance Method Summary collapse

Constructor Details

#initialize(sh, prog) ⇒ Command

Returns a new instance of Command.



9
10
11
12
13
14
# File 'lib/rubsh/command.rb', line 9

def initialize(sh, prog)
  @sh = sh
  @prog = prog.to_s
  @progpath = resolve_progpath(@prog)
  @baked_opts = []
end

Instance Method Details

#bake(*args, **kwargs) ⇒ Command

Returns a new instance of Command with baked options.

Parameters:

  • args (String, Symbol, #to_s, Hash)
  • kwargs (Hash)

Returns:

  • (Command)

    a new instance of Command with baked options.



42
43
44
45
46
# File 'lib/rubsh/command.rb', line 42

def bake(*args, **kwargs)
  cmd = Command.new(@sh, @prog)
  cmd.__bake!(*@baked_opts, *args, **kwargs)
  cmd
end

#call(*args, **kwargs) ⇒ RunningCommand Also known as: call_with

Returns An new instance of RunningCommand with execution state.

Examples:


sh = Rubsh::Shell.new
git = Rubsh::Command.new(sh, "git")
git.call()                                  # => ["git"]
git.call("")                                # => ["git", ""]
git.call("status")                          # => ["git", "status"]
git.call(:status)                           # => ["git", "status"]
git.call(:status, "-v")                     # => ["git", "status", "-v"]
git.call(:status, v: true)                  # => ["git", "status", "-v"]
git.call(:status, { v: true }, "--", ".")   # => ["git", "status", "-v", "--", "."]
git.call(:status, { v: proc{ true }, short: true }, "--", ".")  # => ["git", "status", "-v", "--short=true", "--", "."]
git.call(:status, { untracked_files: "normal" }, "--", ".")     # => ["git", "status", "--untracked-files=normal", "--", "."]

Parameters:

  • args (String, Symbol, #to_s, Hash)
  • kwargs (Hash)

Returns:

  • (RunningCommand)

    An new instance of RunningCommand with execution state.



32
33
34
35
36
# File 'lib/rubsh/command.rb', line 32

def call(*args, **kwargs)
  rcmd = RunningCommand.new(@sh, @prog, @progpath, *@baked_opts, *args, **kwargs)
  rcmd.__run
  rcmd
end

#to_sString

Returns:

  • (String)


49
50
51
# File 'lib/rubsh/command.rb', line 49

def to_s
  @progpath
end