Sha256: 98d73e5327fe342a29d3de23778ea83e55e5da4b4e5da1bb6b154edf76938aac

Contents?: true

Size: 1.94 KB

Versions: 54

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

require 'eac_cli/runner_with/help/builder/alternative'
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?
          ::EacCli::RunnerWith::Help::Builder::Alternative::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
        [::EacCli::RunnerWith::Help::Builder::Alternative::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
eac_cli-0.27.4 lib/eac_cli/definition/help_formatter.rb
avm-tools-0.117.1 sub/eac_cli/lib/eac_cli/definition/help_formatter.rb
avm-tools-0.117.0 sub/eac_cli/lib/eac_cli/definition/help_formatter.rb
avm-tools-0.116.2 sub/eac_cli/lib/eac_cli/definition/help_formatter.rb
eac_cli-0.27.3 lib/eac_cli/definition/help_formatter.rb
avm-tools-0.116.1 sub/eac_cli/lib/eac_cli/definition/help_formatter.rb
eac_cli-0.27.2 lib/eac_cli/definition/help_formatter.rb
avm-tools-0.116.0 sub/eac_cli/lib/eac_cli/definition/help_formatter.rb
eac_cli-0.27.1 lib/eac_cli/definition/help_formatter.rb
avm-tools-0.115.0 sub/eac_cli/lib/eac_cli/definition/help_formatter.rb
eac_cli-0.27.0 lib/eac_cli/definition/help_formatter.rb
avm-tools-0.114.2 sub/eac_cli/lib/eac_cli/definition/help_formatter.rb
avm-tools-0.114.1 sub/eac_cli/lib/eac_cli/definition/help_formatter.rb
eac_cli-0.26.2 lib/eac_cli/definition/help_formatter.rb
avm-tools-0.114.0 sub/eac_cli/lib/eac_cli/definition/help_formatter.rb
eac_cli-0.26.1 lib/eac_cli/definition/help_formatter.rb
eac_cli-0.26.0 lib/eac_cli/definition/help_formatter.rb
avm-tools-0.113.6 sub/eac_cli/lib/eac_cli/definition/help_formatter.rb
avm-tools-0.113.5 sub/eac_cli/lib/eac_cli/definition/help_formatter.rb
eac_cli-0.25.0 lib/eac_cli/definition/help_formatter.rb