Sha256: 8943a5333a57eedae942bd5a00f2bc8e45eaa609ed499172ea67336876aacf91

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

require 'hamster'
require_relative 'utilities'

module Lino
  class SubcommandBuilder
    include Lino::Utilities

    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 with_option(switch, value)
      with(switches: @switches.add([switch, value]))
    end

    def with_flag(flag)
      with(switches: @switches.add([flag]))
    end

    def build(option_separator)
      components = [
          @subcommand,
          map_and_join(@switches, &join_with(option_separator))
      ]

      components
          .reject { |item| item.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.0.0 lib/lino/subcommand_builder.rb
lino-0.1.0 lib/lino/subcommand_builder.rb