Sha256: 6b647a588ec8539bb74e84efb450b446efa880f65b8d4b3bad0fbed6ac966997

Contents?: true

Size: 958 Bytes

Versions: 2

Compression:

Stored size: 958 Bytes

Contents

# frozen_string_literal: true

require 'hamster'
require_relative 'utilities'
require_relative 'switches'

module Lino
  class SubcommandBuilder
    include Lino::Utilities
    include Lino::Switches

    class <<self
      def for_subcommand(subcommand)
        SubcommandBuilder.new(subcommand: subcommand)
      end
    end

    def initialize(subcommand: nil, switches: [])
      @subcommand = subcommand
      @switches = Hamster::Vector.new(switches)
    end

    def build(option_separator, option_quoting)
      components = [
        @subcommand,
        map_and_join(
          @switches,
          &(quote_with(option_quoting) >> join_with(option_separator))
        )
      ]
      components.reject(&:empty?).join(' ')
    end

    private

    def with(**replacements)
      SubcommandBuilder.new(**state.merge(replacements))
    end

    def state
      {
        subcommand: @subcommand,
        switches: @switches
      }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lino-1.10.0.pre.2 lib/lino/subcommand_builder.rb
lino-1.10.0.pre.1 lib/lino/subcommand_builder.rb