Class: Appear::Util::CommandBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/appear/util/command_builder.rb

Overview

Builds command strings.

Examples:

A tmux query command

tmux_panes = CommandBuilder.new(%w(tmux list-panes)).
  flags(:a => true, :F => '#{session_name} #{pane_index}')
output, status = Open3.capture2e(*tmux_panes.to_a)

Instance Method Summary collapse

Constructor Details

#initialize(command, opts = {}) ⇒ CommandBuilder

Returns a new instance of CommandBuilder.

Examples:

dashdash_after_flags

c = CommandBuilder.new('ssh', :dashdash_after_flags => true)
  .flags(:foo => 1, :b => true).args('a', 'b').to_s
"ssh --foo 1 -b -- a b"

Parameters:

  • command (#to_s, Array<#to_s>)

    the command. Use an array if you need multiple words before we start listing arguments, eg ‘%w(vagrant up)`

  • opts (Hash) (defaults to: {})

    options hash

Options Hash (opts):

  • :single_dash_long_flags (Boolean)

    When true, flags like :foo will be printed like “-foo” instead of the default “–foo”

  • :dashdash_after_flags (Boolean)

    When true, a “–” argument will be inserted after the flags but before the arguments.



24
25
26
27
28
29
30
31
32
# File 'lib/appear/util/command_builder.rb', line 24

def initialize(command, opts = {})
  @command = command
  @flags = opts.delete(:flags) || Hash.new { |h, k| h[k] = [] }
  @argv = opts.delete(:argv) || []
  @options = {
    :single_dash_long_flags => false,
    :dashdash_after_flags => false,
  }.merge(opts)
end

Instance Method Details

#==(other) ⇒ Object

Override the == method



136
137
138
# File 'lib/appear/util/command_builder.rb', line 136

def ==(other)
  other.class == self.class && other.state == self.state
end

#args(*args) ⇒ self

Add arguments to this command. Arguments always come after flags, and may be separated from flags with – if you pass :dashdash_after_flags option in the constructor.

Parameters:

  • args (Array<#to_s>)

    args to add

Returns:

  • (self)


72
73
74
75
# File 'lib/appear/util/command_builder.rb', line 72

def args(*args)
  @argv.concat(args)
  self
end

#dupCommandBuilder

Duplicate this Appear::Util::CommandBuilder instance.

Returns:



128
129
130
131
132
133
# File 'lib/appear/util/command_builder.rb', line 128

def dup
  opts = @options.dup
  opts[:argv] = @argv.dup
  opts[:flags] = @flags.dup
  self.class.new(@command.dup, opts)
end

#flag(name, val) ⇒ self

Add a flag to this command

Parameters:

  • name (#to_s)

    flag name, eg ‘cached’ for –cached

  • val (Boolean, #to_s)

    flag value, eg ‘3fdb21’. Can pass “true” for boolean, set-only flags.

Returns:

  • (self)


40
41
42
43
# File 'lib/appear/util/command_builder.rb', line 40

def flag(name, val)
  @flags[name] << val
  self
end

#flags(flag_map) ⇒ self

Add a bunch of flags at once, using a map of flag => argument.

Examples:

multiple duplicate args

CommandBuilder.new('foo').flags(:o => ['Val1', 'Val2]).to_s
# "foo -o Val1 -o Val2"

Parameters:

Returns:

  • (self)


53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/appear/util/command_builder.rb', line 53

def flags(flag_map)
  flag_map.each do |f, v|
    if v.is_a?(Array)
      v.each do |v_prime|
        flag(f, v_prime)
      end
    else
      flag(f, v)
    end
  end
  self
end

#subcommand(name, opts = {}) {|subc| ... } ⇒ self

Add a subcommand, with its own flags arguments, after the current command. This is useful for eg building calls to nested commands.

Examples:

eg, vagrant

v_up = CommandBuilder.new('vagrant').flags(:root => pwd).subcommand('up') do |up|
  up.flags(:provider => :virtualbox', 'no-provision' => true).args('apollo')
end

Parameters:

Yields:

  • (subc)

    Add flags and arguments to the subcommand

Yield Parameters:

Returns:

  • (self)


90
91
92
93
94
95
96
97
# File 'lib/appear/util/command_builder.rb', line 90

def subcommand(name, opts = {})
  # use our options as the defaults
  # then use the given options as the overrides
  subc = CommandBuilder.new(name, @options.merge(opts))
  yield(subc)
  args(*subc.to_a)
  self
end

#to_aArray<String>

Render this command to an array of strings, suitable for execution with ‘system` or other methods that take an ARGV array.

Returns:

  • (Array<String>)

    the command



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/appear/util/command_builder.rb', line 103

def to_a
  res = [@command].flatten
  @flags.each do |name, params|
    flag = flag_name_to_arg(name)
    params.each do |param|
      res << flag
      res << param.to_s unless param == true
    end
  end
  res << '--' if @options[:dashdash_after_flags]
  res.concat(@argv)
  res.map { |v| v.to_s }
end

#to_sString

Render this command as a string, suitable for execution with ‘sh` or `system` or other methods that take a command string.

Returns:

  • (String)

    the command



121
122
123
# File 'lib/appear/util/command_builder.rb', line 121

def to_s
  to_a.shelljoin
end