Class: Yobi::ArgvBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/yobi/argv_builder.rb,
sig/yobi.rbs

Overview

Builds a Restic command's argv incrementally. Not an Array subclass: only the methods below are available; #to_a hands back the plain Array once building is done.

Constant Summary collapse

FLAGS =

Maps a flag name Symbol to its "--dashed-string" form, e.g. :read_data_subset to "--read-data-subset".

Returns:

  • (Hash[Symbol, String])
Hash.new { |flags, name| flags[name] = "--#{name.to_s.tr("_", "-")}" }
SHORT_FLAGS =

Maps a short flag name Symbol to its "-name" form, e.g. :vv to "-vv".

Returns:

  • (Hash[Symbol, String])
Hash.new { |flags, name| flags[name] = "-#{name}" }

Instance Method Summary collapse

Constructor Details

#initializeArgvBuilder

Returns a new instance of ArgvBuilder.



17
18
19
20
# File 'lib/yobi/argv_builder.rb', line 17

def initialize
  @argv = []
  @end_of_options_called = false
end

Instance Method Details

#append(*values) ⇒ self

Appends one or more bare positional values. nil is dropped; an Enumerable is flattened in; everything else is converted with #to_s.

Parameters:

  • values (Array<Object>)

Returns:

  • (self)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/yobi/argv_builder.rb', line 32

def append(*values)
  values.each do |v|
    case v
    in nil
    in String
      @argv << v
    in Enumerable
      append(*v)
    else
      @argv << v.to_s
    end
  end
  self
end

#end_of_optionsself

Appends Restic's -- end-of-options marker.

Returns:

  • (self)

Raises:

  • (RuntimeError)

    if called more than once on the same builder



82
83
84
85
86
87
88
# File 'lib/yobi/argv_builder.rb', line 82

def end_of_options
  raise "end_of_options already called - Restic only honors the first \"--\"" if @end_of_options_called

  @end_of_options_called = true
  @argv << "--"
  self
end

#flag(name, value = nil) ⇒ self

Appends a flag, plus its value when one is given.

Parameters:

  • name (Symbol)

    flag name, looked up in FLAGS

  • value (Object, nil) (defaults to: nil)

    the flag's value; omit for a boolean flag

Returns:

  • (self)


52
53
54
55
56
# File 'lib/yobi/argv_builder.rb', line 52

def flag(name, value = nil)
  @argv << FLAGS[name]
  @argv << value.to_s unless value.nil?
  self
end

#repeat_flag(name, values) ⇒ self

Appends a repeatable flag once per value, e.g. --host a --host b.

Parameters:

  • name (Symbol)

    flag name, looked up in FLAGS

  • values (Array<Object>, Object, nil)

Returns:

  • (self)


63
64
65
66
67
# File 'lib/yobi/argv_builder.rb', line 63

def repeat_flag(name, values)
  name = FLAGS[name]
  array_of_strings(values).each { |value| @argv << name << value }
  self
end

#short_flag(name) ⇒ self

Appends a short flag, e.g. short_flag(:vv) for -vv.

Parameters:

  • name (Symbol)

    flag name, looked up in SHORT_FLAGS

Returns:

  • (self)


73
74
75
76
# File 'lib/yobi/argv_builder.rb', line 73

def short_flag(name)
  @argv << SHORT_FLAGS[name]
  self
end

#to_aArray<String>

Returns the argv built so far.

Returns:

  • (Array<String>)

    the argv built so far



23
24
25
# File 'lib/yobi/argv_builder.rb', line 23

def to_a
  @argv
end