Class: Executable::Help

Inherits:
Object
  • Object
show all
Defined in:
lib/executable/help.rb

Overview

Encpsulates command help for defining and displaying well formated help output in plain text, markdown or via manpages if found.

Creating text help in the fly is fine for personal projects, but for production app, ideally you want to have man pages. You can use the #markdown method to generate .ronn files and use the ronn tool to build manpages for your project. There is also the binman and md2man projects which can be used similarly.

Defined Under Namespace

Classes: Option

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli_class) ⇒ Help

Setup new help object.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/executable/help.rb', line 33

def initialize(cli_class)
  @cli_class = cli_class

  @name       = nil
  @usage      = nil
  @decription = nil
  @copying    = nil
  @see_also   = nil

  @options = {}
  @subcmds = {}
end

Instance Attribute Details

#cli_classObject (readonly)

The Executable subclass to which this help applies.



52
53
54
# File 'lib/executable/help.rb', line 52

def cli_class
  @cli_class
end

Class Method Details

.section(name, &default) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/executable/help.rb', line 17

def self.section(name, &default)
  define_method("default_#{name}", &default)
  class_eval %{
    def #{name}(text=nil)
      @#{name} = text.to_s unless text.nil?
      @#{name} ||= default_#{name}
    end
    def #{name}=(text)
      @#{name} = text.to_s
    end
  }
end

Instance Method Details

Get or set copyright text.



91
92
93
# File 'lib/executable/help.rb', line 91

section(:copyright) do
  'Copyright (c) ' + Time.now.strftime('%Y')
end

#description(text = nil) ⇒ Object

Get or set command description.



82
83
84
# File 'lib/executable/help.rb', line 82

section(:description) do
  nil
end

#inspectObject



47
# File 'lib/executable/help.rb', line 47

alias_method :inspect, :to_s

#manpage(dir = nil) ⇒ Object

Get man-page if there is one.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/executable/help.rb', line 143

def manpage(dir=nil)
  @manpage ||= (
    man = []
    dir = nil

    if dir
      raise unless File.directory?(dir)
    end

    if !dir && call_method
      file, line = call_method.source_location
      dir = File.dirname(file)
    end

    man_name = name.gsub(/\s+/, '-') + '.1'

    if dir
      glob = "man/{man1/,}#{man_name}"
      lookup(glob, dir)
    else
      nil
    end
  )
end

#markdownObject Also known as: md

Generate a RONN-style Markdown.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/executable/help.rb', line 217

def markdown
  commands = text_subcommands
  options  = text_options
  dashname = name.sub(/\s+/, '-')

  s = []

  h = "#{dashname}(1) - #{text_description}"
  s << h + "\n" + ("=" * h.size)

  s << "## SYNOPSIS"
  s << "`" + name + "` [options...] [subcommand]"

  s << "## DESCRIPTION"
  s << text_description

  if !commands.empty?
    s << "## COMMANDS"
    s << commands.map{ |cmd, desc|
      "  * `%s:`\n    %s" % [cmd, desc] 
    }.join("\n")
  end

  if !options.empty?
    s << "## OPTIONS"
    s << options.map{ |max, opts, desc|
      "  * `%s`:\n    %s" % [opts.join(' '), desc]
    }.join("\n\n")
  end

  if copyright && !copyright.empty?
    s << "## COPYRIGHT"
    s << copyright
  end

  if see_also && !see_also.empty?
    s << "## SEE ALSO"
    s << see_also
  end

  s.compact.join("\n\n")
end

#name(text = nil) ⇒ Object

TODO:

Should this instead default to ‘File.basename($0)` ?

Get or set command name.

By default the name is assumed to be the class name, substituting dashes for double colons.



64
65
66
# File 'lib/executable/help.rb', line 64

section(:name) do
  cli_class.usage_name
end

#option(name, description) ⇒ Object

Set description of an option.



107
108
109
# File 'lib/executable/help.rb', line 107

def option(name, description)
  @options[name.to_s] = description
end

#option_listObject



330
331
332
333
334
335
336
337
338
339
# File 'lib/executable/help.rb', line 330

def option_list
  @option_list ||= (
    method_list.map do |meth|
      case meth.name
      when /^(.*?)[\!\=]$/
        Option.new(meth)
      end
    end.compact.sort
  )
end

#see_also(text = nil) ⇒ Object

Get or set “see also” text.



100
101
102
# File 'lib/executable/help.rb', line 100

section(:see_also) do
  nil
end

#show_help(hint = nil) ⇒ Object Also known as: show

TODO:

man-pages will probably fail on Windows.

Show help.



123
124
125
126
127
128
129
# File 'lib/executable/help.rb', line 123

def show_help(hint=nil)
  if file = manpage(hint)
    show_manpage(file)
  else
    puts self
  end
end

#show_manpage(file) ⇒ Object



135
136
137
138
# File 'lib/executable/help.rb', line 135

def show_manpage(file)
  #exec "man #{file}"
  system "man #{file}"
end

#subcommand(name, description) ⇒ Object

Set desciption of a subcommand.



114
115
116
# File 'lib/executable/help.rb', line 114

def subcommand(name, description)
  @subcmds[name.to_s] = description
end

#textObject Also known as: txt

Generate plain text output.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/executable/help.rb', line 184

def text
  commands = text_subcommands
  options  = text_options

  s = []

  s << usage
  s << text_description

  if !commands.empty?
    s << "COMMANDS\n" + commands.map{ |cmd, desc|
      "  %-17s %s" % [cmd, desc] 
    }.join("\n")
  end

  if !options.empty?
    s << "OPTIONS\n" + options.map{ |max, opts, desc|
      "  %-#{max}s %s" % [opts.join(' '), desc]
    }.join("\n")
  end

  s << copyright
  s << see_also

  s.compact.join("\n\n")
end

#text_descriptionString, NilClass

Description of command in printable form. But will return nil if there is no description.

Returns:

  • (String, NilClass)

    command description



269
270
271
272
273
274
# File 'lib/executable/help.rb', line 269

def text_description
  return description if description
  #return Source.get_above_comment(@file, @line) if @file

  call_method ? call_method.comment : nil
end

#text_optionsArray<Fixnum, Options>

List of options coverted to a printable string. But will return nil if there are no options.

Returns:

  • (Array<Fixnum, Options>)

    option list for output



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/executable/help.rb', line 296

def text_options
  option_list.each do |opt|
    if @options.key?(opt.name)
      opt.description = @options[opt.name]
    end
  end    

  # if two options have the same description, they must aliases
  aliased_options = option_list.group_by{ |opt| opt.description }

  list = aliased_options.map do |desc, opts|
    [opts.map{ |o| "%s%s" % [o.mark, o.usage] }, desc]
  end

  max = list.map{ |opts, desc| opts.join(' ').size }.max.to_i + 2

  list.map do |opts, desc|
    [max, opts, desc]
  end
end

#text_subcommandsString, NilClass

List of subcommands converted to a printable string. But will return nil if there are no subcommands.

Returns:

  • (String, NilClass)

    subcommand list text



282
283
284
285
286
287
288
# File 'lib/executable/help.rb', line 282

def text_subcommands
  commands = @cli_class.subcommands
  commands.map do |cmd, klass|
    desc = klass.help.text_description.to_s.split("\n").first
    [cmd, desc]
  end
end

#to_s(format = nil) ⇒ Object

Render help text to a given format. If no format it given then renders to plain text.



172
173
174
175
176
177
178
179
# File 'lib/executable/help.rb', line 172

def to_s(format=nil)
  case format
  when :markdown, :md
    markdown
  else
    text
  end
end

#usage(text = nil) ⇒ Object

Get or set command usage string.



73
74
75
# File 'lib/executable/help.rb', line 73

section(:usage) do
  "Usage: " + name + ' [options...] [subcommand]'
end