Module: Hanami::CLI::Usage Private

Defined in:
lib/hanami/cli/usage.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Command(s) usage

Since:

  • 0.1.0

Constant Summary collapse

SUBCOMMAND_BANNER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

" [SUBCOMMAND]".freeze

Class Method Summary collapse

Class Method Details

.arguments(command) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hanami/cli/usage.rb', line 47

def self.arguments(command) # rubocop:disable Metrics/AbcSize
  return unless CLI.command?(command)

  required_arguments = command.required_arguments
  optional_arguments = command.optional_arguments

  required = required_arguments.map { |arg| arg.name.upcase }.join(' ') if required_arguments.any?
  optional = optional_arguments.map { |arg| "[#{arg.name.upcase}]" }.join(' ') if optional_arguments.any?
  result = [required, optional].compact

  " #{result.join(' ')}" unless result.empty?
end

.call(result, out) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0


16
17
18
19
20
21
22
23
24
# File 'lib/hanami/cli/usage.rb', line 16

def self.call(result, out)
  out.puts "Commands:"
  max_length, commands = commands_and_arguments(result)

  commands.each do |banner, node|
    usage = description(node.command) if node.leaf?
    out.puts "#{justify(banner, max_length, usage)}#{usage}"
  end
end

.command_name(result, name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0


83
84
85
# File 'lib/hanami/cli/usage.rb', line 83

def self.command_name(result, name)
  ProgramName.call([result.names, name])
end

.commands(result) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0


77
78
79
# File 'lib/hanami/cli/usage.rb', line 77

def self.commands(result)
  result.children.sort_by { |name, _| name }
end

.commands_and_arguments(result) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hanami/cli/usage.rb', line 28

def self.commands_and_arguments(result) # rubocop:disable Metrics/MethodLength
  max_length = 0
  ret        = commands(result).each_with_object({}) do |(name, node), memo|
    args = if node.leaf?
             arguments(node.command)
           else
             SUBCOMMAND_BANNER
           end

    partial       = "  #{command_name(result, name)}#{args}"
    max_length    = partial.bytesize if max_length < partial.bytesize
    memo[partial] = node
  end

  [max_length, ret]
end

.description(command) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0


62
63
64
65
66
# File 'lib/hanami/cli/usage.rb', line 62

def self.description(command)
  return unless CLI.command?(command)

  " # #{command.description}" unless command.description.nil?
end

.justify(string, padding, usage) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0


70
71
72
73
# File 'lib/hanami/cli/usage.rb', line 70

def self.justify(string, padding, usage)
  return string.chomp(" ") if usage.nil?
  string.ljust(padding + padding / 2)
end