Sha256: 97ca641ea3e19b6153dd0ee0bd6fbf5c4ba7914a09f79088aca92f86a3dcfdf5

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 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, separator: nil)
      with(switches: @switches.add({components: [switch, value], separator: separator}))
    end

    def with_flag(flag)
      with(switches: @switches.add({components: [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.2.0.pre.1 lib/lino/subcommand_builder.rb
lino-1.1.0 lib/lino/subcommand_builder.rb