Method: Cmds#initialize

Defined in:
lib/cmds.rb

#initialize(template, **options) ⇒ Cmds

Construct a Cmds instance.

Parameters:

  • template (String)

    String template to use when creating the command string to send to the shell via #prepare.

    Allows ERB (positional and keyword), %s (positional) and %{name} (keyword) placeholders.

    Available as the #template attribute.

  • args: (Array<Object>)

    Positional arguments to interpolate into the template on #prepare.

    Available as the #args attribute.

  • assert: (Boolean)

    When true, execution will raise an error if the command doesn't exit successfully (if the command exits with any status other than 0).

    Available as the #assert attribute.

  • chdir: (nil | String | Pathname)

    Optional directory to change into when executing.

    Available as the #chdir attribute.

  • env: (Hash{(String | Symbol) => String})

    Hash of environment variables to set when executing the command.

    Available as the #env attribute.

  • env_mode: (:inline, :spawn_arg)

    Controls how the env vars are added to the command.

    • :inline adds them to the top of the prepared string. This is nice if you want do print the command out and paste it into a terminal. This is the default.

    • :spawn_arg passes them as an argument to Process.spawn. In this case they will not be included in the output of #prepare (or #render).

    Available as the #env_mode attribute.

  • format: (nil, :squish, :pretty, #call)

    Dictates how to format the rendered template string before passing off to the shell.

    This feature lets you write templates in a more relaxed manner without \ line-endings all over the place.

    • nil performs *no formatting at all.

    • :squish reduces any consecutive whitespace (including newlines) to a single space. This is the default.

    • :pretty tries to keep the general formatting but make it acceptable to the shell by adding \ at the end of lines. See pretty_format.

    • An object that responds to #call will be called with the command string as it's only argument for custom formatting.

    See format for more details.

    Available as the #format attribute.

  • input: (nil | String | #read)

    Input to send to the command on execution. Can be a string or an IO-like object that responds to #read.

    Available as the #input attribute.

  • kwds: (Hash{Symbol => Object})

    Keyword arguments to shell escape and interpolate into the template on #prepare.

    Available as the #kwds attribute.



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/cmds.rb', line 237

def initialize  template, **options
  options = defaults options
  
  if options.key? :opts
    options[:kwds][:opts] = options.delete :opts
  end
  
  logger.trace "Cmd constructing...",
    template: template,
    options: options

  @template = template
  
  # Assign options to instance variables
  options.each { |key, value|
    instance_variable_set "@#{ key }", value
  }
  
  # An internal cache of the last result of calling {#prepare}, or `nil` if
  # {#prepare} has never been called. Kinda funky but ends up being useful.
  @last_prepared_cmd = nil
end