Module: Rubycom::CommandInterface

Defined in:
lib/rubycom/command_interface.rb

Class Method Summary collapse

Class Method Details

.build_details(command, command_doc) ⇒ String

Creates a structured list of either sub commands or parameters based on the class of command Calls #build_tags if command is a Method

Parameters:

  • command (Module|Method|String)

    the class will be used to determine the usage structure

  • command_doc (Hash)

    keys should include :parameters and each parameter should be a hash including keys :type, :param_name, :default

Returns:

  • (String)

    a structured text representing a list of sub commands or a list of parameters



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rubycom/command_interface.rb', line 64

def self.build_details(command, command_doc)
  if command.class == Module
    sub_commands = Rubycom::Helpers.format_command_list(command_doc[:sub_command_docs], 90, '  ').join()
    (sub_commands.empty?)? '' : "Sub Commands:\n#{sub_commands}"
  elsif command.class == Method
    tags = self.build_tags(Rubycom::Helpers.format_tags(command_doc[:tags]))
    "#{tags[:others]}#{tags[:params]}#{tags[:returns]}"
  else
    ""
  end
end

.build_interface(command, command_doc) ⇒ String

Uses #build_usage and #build_details to create a structured text output from the given command and doc hash

Parameters:

  • command (Module|Method|String)

    the command to be named in the output

  • command_doc (Hash)

    keys should include :full_doc and any keys required by #build_usage and #build_details

Returns:

  • (String)

    a structured string suitable for printing to the console as a command usage document

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
# File 'lib/rubycom/command_interface.rb', line 10

def self.build_interface(command, command_doc)
  raise ArgumentError, "command should not be nil" if command.nil?
  raise ArgumentError, "command_doc should not be nil" if command_doc.nil?
  "#{self.build_usage(command, command_doc)}\n"+
  "Description:\n"+
  "#{command_doc.fetch(:full_doc, '').split("\n").map{|line| "  #{line}"}.join("\n").chomp}\n"+
  "#{self.build_details(command, command_doc)}"
end

.build_options(command, command_doc) ⇒ String

Creates a structured text representation of usage patterns for the given command and doc hash

Parameters:

  • command (Module|Method|String)

    the class will be used to determine the overall usage pattern

  • command_doc (Hash)

    keys should include :parameters and each parameter should be a hash including keys :type, :param_name, :default

Returns:

  • (String)

    a structured text representation of usage patterns for the given command and doc hash



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubycom/command_interface.rb', line 40

def self.build_options(command, command_doc)
  return '' if command_doc.nil?
  if command.class == Module
    "<command> [args]"
  elsif command.class == Method
    args, opts = command_doc.fetch(:parameters).map { |param|
      if param.fetch(:type) == :req
        "<#{param[:param_name]}>"
      else
        "[--#{param[:param_name]} #{param[:default]}]"
      end
    }.group_by { |p| p.start_with?('<') }.map { |_, group| group.join(' ') }
    "#{args} #{opts}"
  else
    ""
  end
end

.build_tags(tags) ⇒ Hash

Creates a structured text representing the given documentation tags

Parameters:

  • tags (Hash)

    :params|:returns|:others => [Strings]

Returns:

  • (Hash)

    the given key => String representing the list of tags



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rubycom/command_interface.rb', line 80

def self.build_tags(tags)
  return '' if tags.nil? || tags.empty?
  tags.map{|k,val_arr|
    val = val_arr.map { |line| "  #{line}" }.join.chomp
    {
        k => if k == :params
               (val.empty?)? '' : "\nParameters:\n#{val}"
             elsif k == :returns
               (val.empty?)? '' : "\nReturns:\n#{val}"
             else
               (val.empty?)? '' : "\nTags:\n#{val}"
             end
    }
  }.reduce({}, &:merge)
end

.build_usage(command, command_doc) ⇒ String

Uses #build_options to create a usage banner for use in a command usage document

Parameters:

  • command (Module|Method|String)

    the command to be named in the output

  • command_doc (Hash)

    keys should include any keys required by #build_options

Returns:

  • (String)

    a structured text representation of usage patterns for the given command and doc hash



24
25
26
27
28
29
30
31
32
33
# File 'lib/rubycom/command_interface.rb', line 24

def self.build_usage(command, command_doc)
  return '' if command.nil?
  command_use = if File.basename($0, File.extname($0)).gsub("_", '') == command.name.to_s.downcase ||
                    File.read($0).match(/(class|module)\s+#{command.name}/)
                  File.basename($0)
                else
                  command.name.to_s
                end
  "Usage: #{command_use} #{self.build_options(command, command_doc)}"
end