Class: YARD::CLI::LinkStdlib::Search

Inherits:
Command
  • Object
show all
Includes:
CommandHelper
Defined in:
lib/yard/cli/link_stdlib/search.rb

Overview

Hooks into LinkStdlib::ObjectMap#grep to search for names using regular expressions.

Constant Summary collapse

DESCRIPTION =
"Find stdlib names that match Regexp patterns"
USAGE =
"yard stdlib search [OPTIONS] TERMS..."

Instance Method Summary collapse

Methods included from CommandHelper

#add_header, #add_version_opt, #check_args!, #description, #opts, #usage

Instance Method Details

#run(*args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/yard/cli/link_stdlib/search.rb', line 42

def run *args
  # Default format is `:plain`
  opts[ :format ] = :plain
  
  OptionParser.new { |op|
    add_header op, <<~END
      Examples:
      
        1.  {Pathname} instance methods
            
                $ yard stdlib search '^Pathname#'
      
        2.  All `#to_s` methods
            
                $ yard stdlib search '#to_s$'
        
        3.  Print results in serialized formats.
            
            All `#to_s` instance methods in JSON:
            
                $ yard stdlib search --format=json '#to_s$'
            
            Supports a short `-f` flag and first-letter formats too.
            
            Instance methods of {Array} in YAML:
            
                $ yard stdlib search -f y '^Array#'
    END
    
    add_version_opt op
    
    op.on(  '-u', '--urls',
            %(Print doc URLs along with names)
    ) { |urls| opts[ :urls ] = !!urls }
    
    op.on(  '-f FORMAT', '--format=FORMAT',
            %(Specify print format: (p)lain, (j)son or (y)aml)
    ) { |format|
      opts[ :format ] = \
        case format.downcase
        when 'p', 'plain'
          :plain
        when 'j', 'json'
          :json
        when 'y', 'yaml'
          :yaml
        else
          log.fatal \
            %(Unknown format - expected "plain", "json" or "yaml"; ) +
            %(given #{ format.inspect })
          exit false
        end
    }
    
  }.parse! args
  
  if args.empty?
    YARD::LinkStdlib::ObjectMap.
      current.
      names.
      sort_by( &:downcase ).
      each { |key| log.puts key }
    exit true
  end
  
  terms = args.map { |arg|
    begin
      Regexp.new arg
    rescue RegexpError => error
      Regexp.new \
        Regexp.escape( YARD::LinkStdlib.normalize_name( arg ) )
    end
  }
  
  log.debug "Terms:\n  " + terms.map( &:to_s ).join( "\n  " )
  
  names = YARD::LinkStdlib.grep *terms
  
  results = \
    if opts[ :urls ]
      names.each_with_object( {} ) { |name, hash|
        hash[ name ] = YARD::LinkStdlib::ObjectMap.current.url_for name
      }
    else
      names
    end
  
  case opts[ :format ]
  when :plain
    results.each do |entry|
      if entry.is_a? ::Array
        log.puts "#{ entry[0] } <#{ entry[ 1 ]}>"
      else
        log.puts entry
      end
    end
  when :json
    require 'json'
    log.puts JSON.pretty_generate( results )
  when :yaml
    require 'yaml'
    log.puts YAML.dump( results )
  end
  
  exit true
end