Sha256: 126282a57e5159e3d6fcfc10477a32f97746bbc13dbb9b05b364f238bb16c25d

Contents?: true

Size: 1.87 KB

Versions: 54

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'

module EacCli
  class Definition
    class HelpFormatter
      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
      end

      common_constructor :definition

      def positional_argument(positional)
        if positional.subcommand?
          ::EacRubyUtils::Console::DocoptRunner::SUBCOMMANDS_MACRO
        else
          r = "<#{positional.name}>"
          r += '...' if positional.repeat?
          r = "[#{r}]" if positional.optional?
          r
        end
      end

      def section(header, include_header = true)
        b = include_header ? "#{header.humanize}:\n" : ''
        b += send("self_#{header}") + "\n"
        # TO-DO: implement alternatives
        b
      end

      def self_options
        definition.options.map { |option| IDENT + option_definition(option) }.join("\n")
      end

      def self_usage
        IDENT + self_usage_arguments.join(SEP)
      end

      def self_usage_arguments
        [::EacRubyUtils::Console::DocoptRunner::PROGRAM_MACRO] +
          definition.options_argument.if_present([]) { |_v| ['[options]'] } +
          self_usage_arguments_options +
          self_usage_arguments_positional
      end

      def self_usage_arguments_options
        definition.options.select(&:show_on_usage?).map do |option|
          self.class.option_long(option)
        end
      end

      def self_usage_arguments_positional
        definition.positional.map { |p| positional_argument(p) }
      end

      def to_banner
        "#{definition.description}\n\n#{section('usage')}"
      end
    end
  end
end

Version data entries

54 entries across 54 versions & 3 rubygems

Version Path
avm-tools-0.81.0 vendor/eac_cli/lib/eac_cli/definition/help_formatter.rb
eac_cli-0.12.3 lib/eac_cli/definition/help_formatter.rb
ehbrs-tools-0.16.3 vendor/eac_cli/lib/eac_cli/definition/help_formatter.rb
avm-tools-0.80.0 vendor/eac_cli/lib/eac_cli/definition/help_formatter.rb
avm-tools-0.79.0 vendor/eac_cli/lib/eac_cli/definition/help_formatter.rb
avm-tools-0.78.0 vendor/eac_cli/lib/eac_cli/definition/help_formatter.rb
ehbrs-tools-0.16.2 vendor/eac_cli/lib/eac_cli/definition/help_formatter.rb
eac_cli-0.12.2 lib/eac_cli/definition/help_formatter.rb
eac_cli-0.12.1 lib/eac_cli/definition/help_formatter.rb
avm-tools-0.77.0 vendor/eac_cli/lib/eac_cli/definition/help_formatter.rb
ehbrs-tools-0.16.1 vendor/eac_cli/lib/eac_cli/definition/help_formatter.rb
eac_cli-0.12.0 lib/eac_cli/definition/help_formatter.rb
eac_cli-0.11.1 lib/eac_cli/definition/help_formatter.rb
avm-tools-0.76.1 vendor/eac_cli/lib/eac_cli/definition/help_formatter.rb