Sha256: 9224667d59407fadb99d6edb28c478628e6a3763e10afa5809f16f15a6ee7071

Contents?: true

Size: 1.44 KB

Versions: 34

Compression:

Stored size: 1.44 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
      end

      def option_definition(option)
        option.short + SEP + self.class.option_long(option) + OPTION_DESC_SEP + option.description
      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

34 entries across 34 versions & 3 rubygems

Version Path
eac_cli-0.13.0 lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.92.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.91.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.90.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.89.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.88.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
eac_cli-0.12.6 lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.87.1 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
ehbrs-tools-0.19.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
ehbrs-tools-0.18.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
eac_cli-0.12.5 lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.87.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
ehbrs-tools-0.17.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.86.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.85.1 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.85.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
ehbrs-tools-0.16.5 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.84.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.83.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
eac_cli-0.12.4 lib/eac_cli/docopt/doc_builder.rb