Sha256: 17abb9b1fe44b7940cc2299abeb1b0558ce42e9614257211c01b311f62d41b5b

Contents?: true

Size: 1.77 KB

Versions: 11

Compression:

Stored size: 1.77 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_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

11 entries across 11 versions & 3 rubygems

Version Path
eac_cli-0.16.1 lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.94.3 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
eac_cli-0.16.0 lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.94.2 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.94.1 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
eac_cli-0.15.1 lib/eac_cli/docopt/doc_builder.rb
eac_cli-0.15.0 lib/eac_cli/docopt/doc_builder.rb
ehbrs-tools-0.20.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.94.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb
eac_cli-0.14.0 lib/eac_cli/docopt/doc_builder.rb
avm-tools-0.93.0 vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb