Module: Rubycom::CommandInterface
- Defined in:
- lib/rubycom/command_interface.rb
Class Method Summary collapse
-
.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.
-
.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.
-
.build_options(command, command_doc) ⇒ String
Creates a structured text representation of usage patterns for the given command and doc hash.
-
.build_tags(tags) ⇒ Hash
Creates a structured text representing the given documentation tags.
-
.build_usage(command, command_doc) ⇒ String
Uses #build_options to create a usage banner for use in a command usage document.
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
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 = self.(Rubycom::Helpers.(command_doc[:tags])) "#{[:others]}#{[:params]}#{[: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
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
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.(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
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.() return '' if .nil? || .empty? .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
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.(command, command_doc)}" end |