Sha256: 38c04db46480c6c2d3302e1e172070006e14054d55b6dea22229cb1d26d100c3

Contents?: true

Size: 950 Bytes

Versions: 6

Compression:

Stored size: 950 Bytes

Contents

# frozen_string_literal: true

require 'hamster'
require_relative 'utilities'
require_relative 'options'

module Lino
  class SubcommandBuilder
    include Lino::Utilities
    include Lino::Options

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

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

    def build(option_separator, option_quoting)
      components = [
        @subcommand,
        map_and_join(
          @options,
          &(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,
        options: @options
      }
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lino-2.2.0.pre.4 lib/lino/subcommand_builder.rb
lino-2.2.0.pre.3 lib/lino/subcommand_builder.rb
lino-2.2.0.pre.2 lib/lino/subcommand_builder.rb
lino-2.2.0.pre.1 lib/lino/subcommand_builder.rb
lino-2.1.0 lib/lino/subcommand_builder.rb
lino-2.0.0.pre.1 lib/lino/subcommand_builder.rb