Class: Lino::CommandLineBuilder

Inherits:
Object
  • Object
show all
Includes:
Appliables, Options, Utilities
Defined in:
lib/lino/command_line_builder.rb

Overview

rubocop:disable Metrics/ClassLength

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Appliables

#with_appliable, #with_appliables

Methods included from Utilities

#empty?, #join_with, #map_and_join, #nil?, #nil_or_empty?, #quote_with

Methods included from Options

#with_flag, #with_flags, #with_option, #with_options, #with_repeated_option

Constructor Details

#initialize(command: nil, subcommands: [], options: [], arguments: [], environment_variables: [], option_separator: ' ', option_quoting: nil, option_placement: :after_command) ⇒ CommandLineBuilder

rubocop:disable Metrics/ParameterLists



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lino/command_line_builder.rb', line 24

def initialize(
  command: nil,
  subcommands: [],
  options: [],
  arguments: [],
  environment_variables: [],
  option_separator: ' ',
  option_quoting: nil,
  option_placement: :after_command
)
  @command = command
  @subcommands = Hamster::Vector.new(subcommands)
  @options = Hamster::Vector.new(options)
  @arguments = Hamster::Vector.new(arguments)
  @environment_variables = Hamster::Vector.new(environment_variables)
  @option_separator = option_separator
  @option_quoting = option_quoting
  @option_placement = option_placement
end

Class Method Details

.for_command(command) ⇒ Object



18
19
20
# File 'lib/lino/command_line_builder.rb', line 18

def for_command(command)
  CommandLineBuilder.new(command: command)
end

Instance Method Details

#buildObject



127
128
129
130
131
132
133
134
135
136
# File 'lib/lino/command_line_builder.rb', line 127

def build
  components = formatted_components
  command_line =
    component_paths
    .collect { |path| path.inject(components) { |c, p| c && c[p] } }
    .reject(&:empty?)
    .join(' ')

  CommandLine.new(command_line)
end

#with_argument(argument) ⇒ Object



93
94
95
96
97
# File 'lib/lino/command_line_builder.rb', line 93

def with_argument(argument)
  return self if nil?(argument)

  with(arguments: @arguments.add({ components: [argument] }))
end

#with_arguments(arguments) ⇒ Object



99
100
101
102
103
# File 'lib/lino/command_line_builder.rb', line 99

def with_arguments(arguments)
  return self if nil_or_empty?(arguments)

  arguments.inject(self) { |s, argument| s.with_argument(argument) }
end

#with_environment_variable(environment_variable, value) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/lino/command_line_builder.rb', line 105

def with_environment_variable(environment_variable, value)
  with(
    environment_variables:
      @environment_variables.add(
        [
          environment_variable, value
        ]
      )
  )
end

#with_environment_variables(environment_variables) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/lino/command_line_builder.rb', line 116

def with_environment_variables(environment_variables)
  return self if nil_or_empty?(environment_variables)

  environment_variables.entries.inject(self) do |s, var|
    s.with_environment_variable(
      var.include?(:name) ? var[:name] : var[0],
      var.include?(:value) ? var[:value] : var[1]
    )
  end
end

#with_option_placement(option_placement) ⇒ Object



77
78
79
# File 'lib/lino/command_line_builder.rb', line 77

def with_option_placement(option_placement)
  with(option_placement: option_placement)
end

#with_option_quoting(character) ⇒ Object



73
74
75
# File 'lib/lino/command_line_builder.rb', line 73

def with_option_quoting(character)
  with(option_quoting: character)
end

#with_option_separator(option_separator) ⇒ Object



69
70
71
# File 'lib/lino/command_line_builder.rb', line 69

def with_option_separator(option_separator)
  with(option_separator: option_separator)
end

#with_options_after_argumentsObject



89
90
91
# File 'lib/lino/command_line_builder.rb', line 89

def with_options_after_arguments
  with_option_placement(:after_arguments)
end

#with_options_after_commandObject



81
82
83
# File 'lib/lino/command_line_builder.rb', line 81

def with_options_after_command
  with_option_placement(:after_command)
end

#with_options_after_subcommandsObject



85
86
87
# File 'lib/lino/command_line_builder.rb', line 85

def with_options_after_subcommands
  with_option_placement(:after_subcommands)
end

#with_subcommand(subcommand, &block) ⇒ Object

rubocop:enable Metrics/ParameterLists



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lino/command_line_builder.rb', line 46

def with_subcommand(subcommand, &block)
  return self if nil?(subcommand)

  with(
    subcommands: @subcommands.add(
      (block || ->(sub) { sub }).call(
        SubcommandBuilder.for_subcommand(subcommand)
      )
    )
  )
end

#with_subcommands(subcommands, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/lino/command_line_builder.rb', line 58

def with_subcommands(subcommands, &block)
  return self if nil_or_empty?(subcommands)

  without_block = subcommands[0...-1]
  with_block = subcommands.last

  without_block
    .inject(self) { |s, sc| s.with_subcommand(sc) }
    .with_subcommand(with_block, &block)
end