Module: Lino::Builders::Mixins::Options

Includes:
Defaulting, Validation
Included in:
CommandLine, Subcommand
Defined in:
lib/lino/builders/mixins/options.rb

Instance Method Summary collapse

Methods included from Validation

#empty?, #nil_or_empty?

Instance Method Details

#initialize(state) ⇒ Object



14
15
16
17
# File 'lib/lino/builders/mixins/options.rb', line 14

def initialize(state)
  @options = Hamster::Vector.new(state[:options] || [])
  super
end

#with_flag(flag) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/lino/builders/mixins/options.rb', line 71

def with_flag(flag)
  return self if flag.nil?

  with(options: @options.add(
    {
      type: :flag,
      components: [flag]
    }
  ))
end

#with_flags(flags) ⇒ Object



82
83
84
85
86
# File 'lib/lino/builders/mixins/options.rb', line 82

def with_flags(flags)
  return self if nil_or_empty?(flags)

  flags.inject(self) { |s, flag| s.with_flag(flag) }
end

#with_option(option, value, separator: nil, quoting: nil, placement: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lino/builders/mixins/options.rb', line 19

def with_option(
  option,
  value,
  separator: nil,
  quoting: nil,
  placement: nil
)
  return self if value.nil?

  with(options: @options.add(
    {
      type: :option,
      components: [option, value],
      separator:,
      quoting:,
      placement:
    }
  ))
end

#with_options(options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lino/builders/mixins/options.rb', line 39

def with_options(options)
  return self if nil_or_empty?(options)

  options.entries.inject(self) do |s, entry|
    s.with_option(
      or_nth(entry, :option, 0),
      or_nth(entry, :value, 1),
      separator: or_nil(entry, :separator),
      quoting: or_nil(entry, :quoting),
      placement: or_nil(entry, :placement)
    )
  end
end

#with_repeated_option(option, values, separator: nil, quoting: nil, placement: nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/lino/builders/mixins/options.rb', line 53

def with_repeated_option(
  option,
  values,
  separator: nil,
  quoting: nil,
  placement: nil
)
  values.inject(self) do |s, value|
    s.with_option(
      option,
      value,
      separator:,
      quoting:,
      placement:
    )
  end
end