Class: CTioga2::Commands::Documentation::CommandLineHelp

Inherits:
Object
  • Object
show all
Defined in:
lib/ctioga2/commands/doc/help.rb

Overview

Displays help about command-line options and such.

Constant Summary collapse

DefaultStyles =

The default value for the #styles attribute.

{
  'switch' => "01",
  'title' => "01;04",
  'arguments' => '32',
  'options' => '34'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CommandLineHelp

Creates an object to display command-line help. Available values for the options are given by the hash CommandLineHelpOptions. Their meaning is:

  • ‘pager’: disables or enables the use of a pager when sending output to a terminal



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ctioga2/commands/doc/help.rb', line 64

def initialize(options)
  @options_column_width = 20
  @to_pager = if options.key? 'pager'
                options['pager']
              else
                true
              end

  @styles = DefaultStyles.dup
  @color = true
end

Instance Attribute Details

#colorObject

Color output ?



48
49
50
# File 'lib/ctioga2/commands/doc/help.rb', line 48

def color
  @color
end

#options_column_widthObject

How much space to leave for the options ?



31
32
33
# File 'lib/ctioga2/commands/doc/help.rb', line 31

def options_column_width
  @options_column_width
end

#stylesObject

Styles, ie a hash ‘object’ (option, argument…) => ANSI color code.



45
46
47
# File 'lib/ctioga2/commands/doc/help.rb', line 45

def styles
  @styles
end

#to_pagerObject

Whether we should send output to pager if output has terminal support.



41
42
43
# File 'lib/ctioga2/commands/doc/help.rb', line 41

def to_pager
  @to_pager
end

#to_ttyObject

Whether output has (moderate) terminal capabilities



37
38
39
# File 'lib/ctioga2/commands/doc/help.rb', line 37

def to_tty
  @to_tty
end

#total_widthObject

How many columns do we have at all ?



34
35
36
# File 'lib/ctioga2/commands/doc/help.rb', line 34

def total_width
  @total_width
end

Instance Method Details

Prints short help text suitable for a –help option about available commands, by groups (ungrouped last). It takes a list of all commands (cmds) and the list of groups to display.

todo maybe the part about sending to the pager should be factorized into a neat utility class ?



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ctioga2/commands/doc/help.rb', line 83

def print_commandline_options(cmds, groups)
  @to_tty = false
  if STDOUT.tty? 
    begin
      require 'curses'
      Curses.init_screen
      @total_width = Curses.cols
      Curses.close_screen
      @to_tty = true
    rescue
    end
  end
  @total_width ||= 80   # 80 by default

  # Disable color output if not a to a terminal
  if ! @to_tty
    @color = false
  end

  if @to_tty and @to_pager
    # We pass -R as default value...
    ENV['LESS'] = 'R'
    output = IO::popen("pager", "w")
    pager = true
  else
    output = $stdout
    pager = false
  end
  
  for group in groups
    output.puts unless group == groups[0]
    name = (group && group.name) || "Ungrouped commands"
    if group && group.blacklisted 
      name << " (blacklisted)"
    end
    output.puts style(name, 'title')
    for cmd in cmds[group].sort {|a,b|
        a.long_option <=> b.long_option
      }

      output.puts format_one_entry(cmd)
    end
  end
  output.close
end