Class: Urban::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/urban/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



11
12
13
# File 'lib/urban/cli.rb', line 11

def initialize
  @dictionary = Urban::Dictionary
end

Instance Attribute Details

#dictionaryObject

Returns the value of attribute dictionary.



9
10
11
# File 'lib/urban/cli.rb', line 9

def dictionary
  @dictionary
end

Instance Method Details

#run(args = ARGV) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/urban/cli.rb', line 15

def run(args = ARGV)
  begin
    options = parse(args)
    output = case
      when options.help           ; options.help_screen
      when options.version        ; "Urban #{Urban::VERSION} (c) Thomas Miller"
      when options.random         ; dictionary.random
      when !options.phrase.empty? ; dictionary.search(options.phrase)
      else                        ; options.help_screen
    end

    if output.respond_to?(:phrase)
      if output.definitions
        print_entry(output, options)
      else
        $stderr.puts "urban: no definitions found for #{entry.phrase.upcase}."
      end
    else
      puts output
    end

  rescue SocketError
    $stderr.puts 'urban: no internet connection available.'
  rescue OptionParser::InvalidOption => e
    $stderr.puts "urban: #{e.message}\nTry `urban --help' for more information."
  rescue Exception => e
    $stderr.puts e.message
  end
end