Sha256: 5e9b517a13f984b9a9e90873ee5e7104c38ece1c38d39a2a8e7c1656ed110e91

Contents?: true

Size: 1009 Bytes

Versions: 13

Compression:

Stored size: 1009 Bytes

Contents

# frozen_string_literal: true

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

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

    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

13 entries across 13 versions & 1 rubygems

Version Path
lino-3.1.0.pre.1 lib/lino/subcommand_builder.rb
lino-3.0.0 lib/lino/subcommand_builder.rb
lino-3.0.0.pre.1 lib/lino/subcommand_builder.rb
lino-2.7.0 lib/lino/subcommand_builder.rb
lino-2.6.0.pre.3 lib/lino/subcommand_builder.rb
lino-2.6.0.pre.2 lib/lino/subcommand_builder.rb
lino-2.6.0.pre.1 lib/lino/subcommand_builder.rb
lino-2.5.0 lib/lino/subcommand_builder.rb
lino-2.4.0.pre.2 lib/lino/subcommand_builder.rb
lino-2.4.0.pre.1 lib/lino/subcommand_builder.rb
lino-2.3.0 lib/lino/subcommand_builder.rb
lino-2.2.0.pre.6 lib/lino/subcommand_builder.rb
lino-2.2.0.pre.5 lib/lino/subcommand_builder.rb