class Lino::CommandLineBuilder

rubocop:disable Metrics/ClassLength

Public Class Methods

for_command(command) click to toggle source
# File lib/lino/command_line_builder.rb, line 18
def for_command(command)
  CommandLineBuilder.new(command: command)
end
new( command: nil, subcommands: [], options: [], arguments: [], environment_variables: [], option_separator: ' ', option_quoting: nil, option_placement: :after_command ) click to toggle source

rubocop:disable Metrics/ParameterLists

# 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

Public Instance Methods

build() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/lino/command_line_builder.rb, line 73
def with_option_quoting(character)
  with(option_quoting: character)
end
with_option_separator(option_separator) click to toggle source
# File lib/lino/command_line_builder.rb, line 69
def with_option_separator(option_separator)
  with(option_separator: option_separator)
end
with_options_after_arguments() click to toggle source
# File lib/lino/command_line_builder.rb, line 89
def with_options_after_arguments
  with_option_placement(:after_arguments)
end
with_options_after_command() click to toggle source
# File lib/lino/command_line_builder.rb, line 81
def with_options_after_command
  with_option_placement(:after_command)
end
with_options_after_subcommands() click to toggle source
# File lib/lino/command_line_builder.rb, line 85
def with_options_after_subcommands
  with_option_placement(:after_subcommands)
end
with_subcommand(subcommand, &block) click to toggle source

rubocop:enable Metrics/ParameterLists

# 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) click to toggle source
# 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

Private Instance Methods

component_paths() click to toggle source
# File lib/lino/command_line_builder.rb, line 140
def component_paths
  [
    %i[environment_variables],
    %i[command],
    %i[options after_command],
    %i[subcommands],
    %i[options after_subcommands],
    %i[arguments],
    %i[options after_arguments]
  ]
end
formatted_arguments() click to toggle source
# File lib/lino/command_line_builder.rb, line 192
def formatted_arguments
  map_and_join(
    @arguments,
    &join_with(' ')
  )
end
formatted_components() click to toggle source
# File lib/lino/command_line_builder.rb, line 152
def formatted_components
  {
    environment_variables: formatted_environment_variables,
    command: @command,
    options: formatted_options,
    subcommands: formatted_subcommands,
    arguments: formatted_arguments
  }
end
formatted_environment_variables() click to toggle source
# File lib/lino/command_line_builder.rb, line 162
def formatted_environment_variables
  map_and_join(@environment_variables) do |var|
    "#{var[0]}=\"#{var[1].to_s.gsub(/"/, '\\"')}\""
  end
end
formatted_options() click to toggle source
# File lib/lino/command_line_builder.rb, line 175
def formatted_options
  %i[
    after_command
    after_subcommands
    after_arguments
  ].inject({}) do |options, placement|
    options
      .merge({ placement => formatted_options_with_placement(placement) })
  end
end
formatted_options_with_placement(placement) click to toggle source
# File lib/lino/command_line_builder.rb, line 168
def formatted_options_with_placement(placement)
  map_and_join(
    options_with_placement(placement),
    &(quote_with(@option_quoting) >> join_with(@option_separator))
  )
end
formatted_subcommands() click to toggle source
# File lib/lino/command_line_builder.rb, line 186
def formatted_subcommands
  map_and_join(@subcommands) do |sub|
    sub.build(@option_separator, @option_quoting)
  end
end
options_with_placement(placement) click to toggle source
# File lib/lino/command_line_builder.rb, line 199
def options_with_placement(placement)
  @options.select { |o| o[:placement] == placement } +
    if @option_placement == placement
      @options.select { |o| o[:placement].nil? }
    end
end
state() click to toggle source
# File lib/lino/command_line_builder.rb, line 210
def state
  {
    command: @command,
    subcommands: @subcommands,
    options: @options,
    arguments: @arguments,
    environment_variables: @environment_variables,
    option_separator: @option_separator,
    option_quoting: @option_quoting,
    option_placement: @option_placement
  }
end
with(**replacements) click to toggle source
# File lib/lino/command_line_builder.rb, line 206
def with(**replacements)
  CommandLineBuilder.new(**state.merge(replacements))
end