~ubuntu-branches/ubuntu/vivid/ruby-cri/vivid

« back to all changes in this revision

Viewing changes to lib/cri/help_renderer.rb

  • Committer: Package Import Robot
  • Author(s): Cédric Boutillier
  • Date: 2014-04-14 15:15:48 UTC
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: package-import@ubuntu.com-20140414151548-xn2ozuu3jjl3538j
Tags: upstream-2.6.0
Import upstream version 2.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# encoding: utf-8
 
2
 
 
3
module Cri
 
4
 
 
5
  # The {HelpRenderer} class is responsible for generating a string containing
 
6
  # the help for a given command, intended to be printed on the command line.
 
7
  class HelpRenderer
 
8
 
 
9
    # Creates a new help renderer for the given command.
 
10
    #
 
11
    # @param [Cri::Command] cmd The command to generate the help for
 
12
    #
 
13
    # @option params [Boolean] :verbose true if the help output should be
 
14
    #   verbose, false otherwise.
 
15
    def initialize(cmd, params={})
 
16
      @cmd        = cmd
 
17
      @is_verbose = params.fetch(:verbose, false)
 
18
    end
 
19
 
 
20
    # @return [String] The help text for this command
 
21
    def render
 
22
      text = ''
 
23
 
 
24
      append_summary(text)
 
25
      append_usage(text)
 
26
      append_description(text)
 
27
      append_subcommands(text)
 
28
      append_options(text)
 
29
 
 
30
      text
 
31
    end
 
32
 
 
33
    private
 
34
 
 
35
    def append_summary(text)
 
36
      return if @cmd.summary.nil?
 
37
 
 
38
      text << "name".formatted_as_title << "\n"
 
39
      text << "    #{@cmd.name.formatted_as_command} - #{@cmd.summary}" << "\n"
 
40
      unless @cmd.aliases.empty?
 
41
        text << "    aliases: " << @cmd.aliases.map { |a| a.formatted_as_command }.join(' ') << "\n"
 
42
      end
 
43
    end
 
44
 
 
45
    def append_usage(text)
 
46
      return if @cmd.usage.nil?
 
47
 
 
48
      path = [ @cmd.supercommand ]
 
49
      path.unshift(path[0].supercommand) until path[0].nil?
 
50
      formatted_usage = @cmd.usage.gsub(/^([^\s]+)/) { |m| m.formatted_as_command }
 
51
      full_usage = path[1..-1].map { |c| c.name.formatted_as_command + ' ' }.join + formatted_usage
 
52
 
 
53
      text << "\n"
 
54
      text << "usage".formatted_as_title << "\n"
 
55
      text << full_usage.wrap_and_indent(78, 4) << "\n"
 
56
    end
 
57
 
 
58
    def append_description(text)
 
59
      return if @cmd.description.nil?
 
60
 
 
61
      text << "\n"
 
62
      text << "description".formatted_as_title << "\n"
 
63
      text << @cmd.description.wrap_and_indent(78, 4) + "\n"
 
64
    end
 
65
 
 
66
    def append_subcommands(text)
 
67
      return if @cmd.subcommands.empty?
 
68
 
 
69
      text << "\n"
 
70
      text << (@cmd.supercommand ? 'subcommands' : 'commands').formatted_as_title
 
71
      text << "\n"
 
72
 
 
73
      shown_subcommands = @cmd.subcommands.select { |c| !c.hidden? || @is_verbose }
 
74
      length = shown_subcommands.map { |c| c.name.formatted_as_command.size }.max
 
75
 
 
76
      # Command
 
77
      shown_subcommands.sort_by { |cmd| cmd.name }.each do |cmd|
 
78
        text << sprintf("    %-#{length+4}s %s\n",
 
79
          cmd.name.formatted_as_command,
 
80
          cmd.summary)
 
81
      end
 
82
 
 
83
      # Hidden notice
 
84
      if !@is_verbose
 
85
        diff = @cmd.subcommands.size - shown_subcommands.size
 
86
        case diff
 
87
        when 0
 
88
        when 1
 
89
          text << "    (1 hidden command omitted; show it with --verbose)\n"
 
90
        else
 
91
          text << "    (#{diff} hidden commands omitted; show them with --verbose)\n"
 
92
        end
 
93
      end
 
94
    end
 
95
 
 
96
    def append_options(text)
 
97
      groups = { 'options' => @cmd.option_definitions }
 
98
      if @cmd.supercommand
 
99
        groups["options for #{@cmd.supercommand.name}"] = @cmd.supercommand.global_option_definitions
 
100
      end
 
101
      length = groups.values.inject(&:+).map { |o| o[:long].to_s.size }.max
 
102
      groups.keys.sort.each do |name|
 
103
        defs = groups[name]
 
104
        append_option_group(text, name, defs, length)
 
105
      end
 
106
    end
 
107
 
 
108
    def append_option_group(text, name, defs, length)
 
109
      return if defs.empty?
 
110
 
 
111
      text << "\n"
 
112
      text << "#{name}".formatted_as_title
 
113
      text << "\n"
 
114
 
 
115
      ordered_defs = defs.sort_by { |x| x[:short] || x[:long] }
 
116
      ordered_defs.each do |opt_def|
 
117
        text << format_opt_def(opt_def, length)
 
118
        text << opt_def[:desc] << "\n"
 
119
      end
 
120
    end
 
121
 
 
122
    def format_opt_def(opt_def, length)
 
123
      opt_text = sprintf(
 
124
          "    %-2s %-#{length+6}s",
 
125
          opt_def[:short] ? ('-' + opt_def[:short]) : '',
 
126
          opt_def[:long]  ? ('--' + opt_def[:long]) : '')
 
127
      opt_text.formatted_as_option
 
128
    end
 
129
 
 
130
  end
 
131
 
 
132
end