Sha256: def8e5448995c63679e081c3a62a4c2e8a37f480e053a1e4f695a040ebcd0f49

Contents?: true

Size: 1.82 KB

Versions: 6

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'
require 'eac_ruby_utils/console/docopt_runner'

module EacCli
  module Docopt
    class DocBuilder
      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.if_present('') do |v|
          OPTION_DESC_SEP + v
        end
      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::Docopt::DocBuilder::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::Docopt::DocBuilder::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

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
avm-tools-0.98.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.97.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.96.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
eac_cli-0.16.3 lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.95.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
eac_cli-0.16.2 lib/eac_cli/docopt/doc_builder.rb