Class: Gem::Commands::ManCommand

Inherits:
Gem::Command
  • Object
show all
Includes:
VersionOption
Defined in:
lib/rubygems/commands/man_command.rb

Overview

Much of this is stolen from the ‘open_gem` RubyGem’s “read” command - thanks Adam!

github.com/adamsanderson/open_gem/blob/dfddaa286e/lib/rubygems/commands/read_command.rb

Instance Method Summary collapse

Constructor Details

#initializeManCommand

Returns a new instance of ManCommand.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rubygems/commands/man_command.rb', line 8

def initialize
  super 'man', "Open a gem's manual",
    :command => nil,
    :version => Gem::Requirement.default,
    :latest  => false,
    :all     => false

  add_all_gems_option
  add_system_fallback_option
  add_latest_version_option
  add_version_option
  add_exact_match_option
end

Instance Method Details

#add_all_gems_optionObject



31
32
33
34
35
36
# File 'lib/rubygems/commands/man_command.rb', line 31

def add_all_gems_option
  add_option('-a', '--all',
    'List all installed gems that have manuals.') do |value, options|
    options[:all] = true
  end
end

#add_exact_match_optionObject



52
53
54
55
56
# File 'lib/rubygems/commands/man_command.rb', line 52

def add_exact_match_option
  add_option('-x', '--exact', 'Only list exact matches') do |value, options|
    options[:exact] = true
  end
end

#add_latest_version_optionObject



45
46
47
48
49
50
# File 'lib/rubygems/commands/man_command.rb', line 45

def add_latest_version_option
  add_option('-l', '--latest',
    'If there are multiple versions, open the latest') do |value, options|
    options[:latest] = true
  end
end

#add_system_fallback_optionObject



38
39
40
41
42
43
# File 'lib/rubygems/commands/man_command.rb', line 38

def add_system_fallback_option
  add_option('-s', '--system',
    'Falls back to searching for system-wide man pages.') do |value, options|
    options[:system] = true
  end
end

#argumentsObject



26
27
28
29
# File 'lib/rubygems/commands/man_command.rb', line 26

def arguments
  "SECTION       section of the manual to search\n" +
  "GEMNAME       gem whose manual you wish to read"
end

#executeObject



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
85
86
# File 'lib/rubygems/commands/man_command.rb', line 58

def execute
  if get_one_optional_argument =~ /^\d$/
    section = get_one_optional_argument
  end

  if options[:all]
    puts "These gems have man pages:", ''
    Gem.source_index.gems.each do |name, spec|
      puts "#{spec.name} #{spec.version}" if spec.has_manpage?
    end
  else
    # gem man 1 mustache
    section, name, _ = options[:args]

    if name.nil?
      # gem man mustache
      name, section = section, nil
    end

    # Try to read manpages.
    if spec = get_spec(name) { |s| s.has_manpage?(section) }
      read_manpage(spec, section)
    elsif options[:system]
      exec "man #{section} #{name}"
    else
      abort "No manual entry for #{name}"
    end
  end
end

#gem_path(spec) ⇒ Object



113
114
115
# File 'lib/rubygems/commands/man_command.rb', line 113

def gem_path(spec)
  File.join(spec.installation_path, "gems", spec.full_name)
end

#get_spec(name, &block) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rubygems/commands/man_command.rb', line 117

def get_spec(name, &block)
  dep = Gem::Dependency.new(name, options[:version])
  specs = Gem.source_index.search(dep)

  if block
    specs = specs.select { |spec| yield spec }
  end

  if specs.length == 0
    # If we have not tried to do a pattern match yet, fall back on it.
    if !options[:exact] && !name.is_a?(Regexp)
      pattern = /#{Regexp.escape name}/
      get_spec(pattern, &block)
    else
      nil
    end
  elsif specs.length == 1 || options[:latest]
    specs.last
  else
    choices = specs.map { |s| "#{s.name} #{s.version}" }
    c, i = choose_from_list "Open which gem?", choices
    specs[i] if i
  end
end

#read_manpage(spec, section = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rubygems/commands/man_command.rb', line 88

def read_manpage(spec, section = nil)
  return if spec.nil?

  paths = spec.manpages(section)
  return if paths.empty?

  # man/ron.1 => ron(1)
  names = paths.map do |path|
    path.sub(/.*\/(.+)\.(\d+)/, '\1(\2)')
  end

  if paths.size == 1
    manpath = paths[0]
  elsif paths.size > 1
    name, index = choose_from_list("View which manual?", names)
    manpath = paths[index]
  end

  if manpath
    exec "man #{File.join(gem_path(spec), manpath)}"
  else
    abort "no manuals found for #{spec.name}"
  end
end

#usageObject



22
23
24
# File 'lib/rubygems/commands/man_command.rb', line 22

def usage
  "gem man [SECTION] GEMNAME"
end