module Transcriptic::Command # list commands and display help # class Help < Base PRIMARY_NAMESPACES = %w( analyze data history run status sequences) # help [COMMAND] # # list available commands or display help for a specific command # def index if command = args.shift help_for_command(command) else help_for_root end end alias_command "-h", "help" alias_command "--help", "help" def self.usage_for_command(command) command = new.send(:commands)[command] "Usage: transcriptic #{command[:banner]}" if command end private def commands_for_namespace(name) Transcriptic::Command.commands.values.select do |command| command[:namespace] == name && command[:command] != name end end def namespaces namespaces = Transcriptic::Command.namespaces namespaces.delete("app") namespaces end def commands Transcriptic::Command.commands end def legacy_help_for_namespace(namespace) instance = Transcriptic::Command::Help.groups.map do |group| [ group.title, group.select { |c| c.first =~ /^#{namespace}/ }.length ] end.sort_by { |l| l.last }.last return nil unless instance return nil if instance.last.zero? instance.first end def primary_namespaces PRIMARY_NAMESPACES.map { |name| namespaces[name] }.compact end def additional_namespaces (namespaces.values - primary_namespaces) end def summary_for_namespaces(namespaces) size = longest(namespaces.map { |n| n[:name] }) namespaces.sort_by {|namespace| namespace[:name]}.each do |namespace| name = namespace[:name] namespace[:description] puts " %-#{size}s # %s" % [ name, namespace[:description] ] end end def help_for_root puts "Usage: transcriptic COMMAND [command-specific-options]" puts puts "Primary help topics, type \"transcriptic help TOPIC\" for more details:" puts summary_for_namespaces(primary_namespaces) puts puts "Additional topics:" puts summary_for_namespaces(additional_namespaces) puts end def help_for_namespace(name) namespace_commands = commands_for_namespace(name) unless namespace_commands.empty? size = longest(namespace_commands.map { |c| c[:banner] }) namespace_commands.sort_by { |c| c[:banner].to_s }.each do |command| next if command[:help] =~ /DEPRECATED/ command[:summary] puts " %-#{size}s # %s" % [ command[:banner], command[:summary] ] end end end def help_for_command(name) if command_alias = Transcriptic::Command.command_aliases[name] display("Alias: #{name} redirects to #{command_alias}") name = command_alias end if command = commands[name] puts "Usage: transcriptic #{command[:banner]}" if command[:help].strip.length > 0 puts command[:help].split("\n")[1..-1].join("\n") end puts end if commands_for_namespace(name).size > 0 puts "Additional commands, type \"transcriptic help COMMAND\" for more details:" puts help_for_namespace(name) puts elsif command.nil? error "#{name} is not a transcriptic command. See `transcriptic help`." end end end end