Class: Clitopic::Command::Help

Inherits:
Base
  • Object
show all
Defined in:
lib/clitopic/command/help.rb

Class Method Summary collapse

Methods inherited from Base

cmd_options, fullname, load_defaults, load_options, option, register, topic, topic_options

Methods included from Parser::OptParser

#check_all_required, #check_required, #help, #parse, #parser, #process_options

Class Method Details

.callObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/clitopic/command/help.rb', line 118

def call
  if options[:all] == true
    display_all(true)
  elsif options.has_key?(:topic)
    display_topic_help(options[:topic])
  elsif arguments.size > 0
    if Clitopic::Topics.topics[arguments[0]] != nil
      display_topic_help(arguments[0])
    else
      cmd, topic = Clitopic::Commands.find_cmd(arguments[0])
      if cmd.nil?
        puts "Unknown command: #{arguments[0]}\n show all available commands with help --all"
      else
        display_cmd(cmd, true)
      end
    end
  else
    display_all
  end
end

.display_all(with_commands = false) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/clitopic/command/help.rb', line 109

def display_all(with_commands=false)
  puts "Usage: #{Clitopic.name} COMMAND [command-specific-options]\n\n"

  header(self)
  display_globals
  puts "\n"
  display_topics(with_commands)
end

.display_cmd(cmd, with_header = false) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/clitopic/command/help.rb', line 36

def display_cmd(cmd, with_header=false)
  if with_header
    header(cmd)
  end
  if cmd.hidden == false || options[:with_hidden] == true
    puts cmd.help
    puts "\n\n"
  end
end

.display_globalsObject



29
30
31
32
33
34
# File 'lib/clitopic/command/help.rb', line 29

def display_globals
  puts "Primary help topics, type \"#{Clitopic.name} help TOPIC\" for more details:\n\n"
  Clitopic::Commands.global_commands.each do |name, cmd|
    puts ("%-#{longest_global_cmd + 3}s  # %s" % [ "#{name}", "#{cmd.short_description}"]).indent(2)
  end
end

.display_topic(topic_name, with_commands = false, with_header = false) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/clitopic/command/help.rb', line 58

def display_topic(topic_name, with_commands=false, with_header=false)
  if with_commands
    longest = longest_cmd
  else
    longest = longest_topic
  end
  topic = Topics[topic_name]
  if with_header
    header(topic)
  end
  if topic.hidden == false || options[:with_hidden] == true
    if with_header
      if !topic.commands['index'].nil?
        display_cmd(topic.commands['index'])
      end
    else
      puts ("%-#{longest + 3}s  # %s" % [ "#{topic_name}", "#{topic.short_description}" ]).indent(2)
    end
    if with_commands
      puts "Additional commands, type \"#{Clitopic.name} help COMMAND\" for more details:\n\n" if with_header
      topic.commands.each do |cmd_name, cmd|
        puts ("   %-#{longest}s  # %s" % [ "#{cmd.fullname}", "#{cmd.short_description}"]).indent(2)
      end
      puts ""
    end
  end
end

.display_topic_help(topic_name) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/clitopic/command/help.rb', line 86

def display_topic_help(topic_name)
  topic = Topics[topic_name]
  longest = topic.commands.keys.map{|a| "#{topic_name}:#{a}".size}.max
  header(topic)

  if topic.hidden == false || options[:with_hidden] == true
    if !topic.commands['index'].nil?
      display_cmd(topic.commands['index'], true)
    end
    puts "Additional commands, type \"#{Clitopic.name} help COMMAND\" for more details:\n\n"
    topic.commands.each do |cmd_name, cmd|
      puts ("%-#{longest}s  # %s" % [ "#{cmd.fullname}", "#{cmd.short_description}"]).indent(2)
    end
  end
end

.display_topics(with_commands = false) ⇒ Object



102
103
104
105
106
107
# File 'lib/clitopic/command/help.rb', line 102

def display_topics(with_commands=false)
  puts "Additional topics:\n\n"
  Clitopic::Topics.topics.each do |topic_name, topic|
    display_topic(topic_name, with_commands)
  end
end

.header(obj) ⇒ Object



24
25
26
27
# File 'lib/clitopic/command/help.rb', line 24

def header(obj)
  puts obj.description
  puts "\n"
end

.longest_cmdObject



50
51
52
# File 'lib/clitopic/command/help.rb', line 50

def longest_cmd
  @longest_cmd ||= Clitopic::Commands.all_commands.map{|k| k.size}.max
end

.longest_global_cmdObject



46
47
48
# File 'lib/clitopic/command/help.rb', line 46

def longest_global_cmd
  @longest_global_cmd ||= Clitopic::Commands.global_commands.keys.map{|k| k.size}.max
end

.longest_topicObject



54
55
56
# File 'lib/clitopic/command/help.rb', line 54

def longest_topic
  @longest_topic ||= Topics.topics.keys.map{|k| k.size}.max
end