Sha256: ea0867d3d161b59e8864d3b23286a9d48b9167643bc08555dc1e549e7054f5ad

Contents?: true

Size: 2 KB

Versions: 33

Compression:

Stored size: 2 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'

module EacCli
  module RunnerWith
    module Help
      class Builder
        require_sub __FILE__
        common_constructor :definition

        SEP = ' '
        IDENT = SEP * 2
        OPTION_DESC_SEP = IDENT * 2

        class << self
          def option_long(option)
            b = option.long
            b += '=<value>' if option.argument?
            b
          end

          def option_short(option)
            b = option.short
            b += '=<value>' if option.argument?
            b
          end

          def option_usage_full(option)
            if option.long.present?
              [option.short, option_long(option)].reject(&:blank?).join(SEP)
            else
              option_short(option)
            end
          end
        end

        def option_definition(option)
          [self.class.option_usage_full(option), option.description,
           option.default_value? ? "[Default: \"#{option.default_value}\"]" : nil]
            .reject(&:blank?).join(OPTION_DESC_SEP)
        end

        def section(header, include_header = true)
          b = include_header ? "#{header.humanize}:\n" : ''
          b += send("self_#{header}") + "\n"
          definition.alternatives.each do |alternative|
            b += IDENT + ::EacCli::RunnerWith::Help::Builder::Alternative.new(alternative).to_s +
                 "\n"
          end
          b
        end

        def options_section
          "Options:\n" +
            definition.alternatives.flat_map(&:options)
                      .map { |option| IDENT + option_definition(option) + "\n" }.join
        end

        def usage_section
          "Usage:\n" +
            definition.alternatives.map do |alternative|
              IDENT + ::EacCli::RunnerWith::Help::Builder::Alternative.new(alternative).to_s + "\n"
            end.join
        end

        def to_s
          "#{definition.description}\n\n#{usage_section}\n#{options_section}\n"
        end
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 3 rubygems

Version Path
avm-tools-0.116.1 sub/eac_cli/lib/eac_cli/runner_with/help/builder.rb
eac_cli-0.27.2 lib/eac_cli/runner_with/help/builder.rb
avm-tools-0.116.0 sub/eac_cli/lib/eac_cli/runner_with/help/builder.rb
eac_cli-0.27.1 lib/eac_cli/runner_with/help/builder.rb
avm-tools-0.115.0 sub/eac_cli/lib/eac_cli/runner_with/help/builder.rb
eac_cli-0.27.0 lib/eac_cli/runner_with/help/builder.rb
avm-tools-0.114.2 sub/eac_cli/lib/eac_cli/runner_with/help/builder.rb
avm-tools-0.114.1 sub/eac_cli/lib/eac_cli/runner_with/help/builder.rb
eac_cli-0.26.2 lib/eac_cli/runner_with/help/builder.rb
avm-tools-0.114.0 sub/eac_cli/lib/eac_cli/runner_with/help/builder.rb
eac_cli-0.26.1 lib/eac_cli/runner_with/help/builder.rb
eac_cli-0.26.0 lib/eac_cli/runner_with/help/builder.rb
avm-tools-0.113.6 sub/eac_cli/lib/eac_cli/runner_with/help/builder.rb
avm-tools-0.113.5 sub/eac_cli/lib/eac_cli/runner_with/help/builder.rb
eac_cli-0.25.0 lib/eac_cli/runner_with/help/builder.rb
avm-tools-0.113.4 sub/eac_cli/lib/eac_cli/runner_with/help/builder.rb
avm-tools-0.113.3 sub/eac_cli/lib/eac_cli/runner_with/help/builder.rb
eac_cli-0.24.1 lib/eac_cli/runner_with/help/builder.rb
avm-tools-0.113.2 sub/eac_cli/lib/eac_cli/runner_with/help/builder.rb
eac_cli-0.24.0 lib/eac_cli/runner_with/help/builder.rb