Class: Capiru::CLI

Inherits:
Application show all
Defined in:
lib/capiru/cli.rb

Constant Summary

Constants inherited from Application

Application::EXIT_FAILURE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, env) ⇒ CLI

Returns a new instance of CLI.



16
17
18
19
20
# File 'lib/capiru/cli.rb', line 16

def initialize(args, env)
  @environment = env
  set_default_options
  self.parser   = build_options_parser
end

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



8
9
10
# File 'lib/capiru/cli.rb', line 8

def environment
  @environment
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/capiru/cli.rb', line 8

def options
  @options
end

#parserObject

Returns the value of attribute parser.



8
9
10
# File 'lib/capiru/cli.rb', line 8

def parser
  @parser
end

#search_pathObject

Returns the value of attribute search_path.



8
9
10
# File 'lib/capiru/cli.rb', line 8

def search_path
  @search_path
end

#statusObject

Returns the value of attribute status.



8
9
10
# File 'lib/capiru/cli.rb', line 8

def status
  @status
end

Instance Method Details

#build_options_parserObject



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
# File 'lib/capiru/cli.rb', line 73

def build_options_parser
  OptionParser.new do |opts|
    opts.banner = "Usage: capiru [options]"

    # Display Help Information
    opts.on("-h", "--help", "Show usage information") do
      puts opts
      exit(EXIT_FAILURE)
    end

    # Set the search Query
    opts.on("-q", "--query TERM", "Search the documentation for a specific term") do |term|
      options.query = term
    end
    
    # Use a Regex Query
    opts.on("-r", "--regexp REGEXP", "Seach the documentatino for terms matching REGEXP") do |regexp|
      options.regexp = Regexp.new(regexp)
    end

    # Set the locale
    opts.on("-l", "--locale LOCALE", "Return documentation in the given locale") do |locale|
      options.locale = locale
    end

  end
end

#get_system_localeObject



105
106
107
108
109
# File 'lib/capiru/cli.rb', line 105

def get_system_locale
  locale = @environment['LANG']
  locale = locale[0..1] if locale && locale.is_a?(String)
  locale
end

#has_man?Boolean

Returns:

  • (Boolean)


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

def has_man?
  `which man`
  $?.success?
end

#parseObject



57
58
59
60
61
62
63
64
# File 'lib/capiru/cli.rb', line 57

def parse
  begin
    parser.parse!
  rescue => ex
    puts "Error: #{ex.message}"
    exit(EXIT_FAILURE)
  end
end

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/capiru/cli.rb', line 23

def run

  # Parse options and args/env vars
  parse
  
  # Set the search path for queries
  set_search_path_for_queries(options.locale)

  # Run Program
  begin
    if options.query
      match = run_single_query(options.query)
      cmd = "man #{match}"
      system(cmd)
      self.status = $?
    elsif options.regexp
      # TODO: Implement
    else
      # NOOP
    end
  rescue Capiru::TermNotFound => ex
    puts "Error: #{ex.message}"
    exit(EXIT_FAILURE)
  end
  self.status.exitstatus
end

#run_single_query(query) ⇒ Object



51
52
53
54
55
# File 'lib/capiru/cli.rb', line 51

def run_single_query(query)
  file = File.join(self.search_path, query)
  raise Capiru::TermNotFound, "No results for #{query} in #{options.locale} locale" unless File.exists?(file)
  file
end

#set_default_optionsObject



66
67
68
69
70
71
# File 'lib/capiru/cli.rb', line 66

def set_default_options
  self.options = OpenStruct.new
  self.options.query           = false
  self.options.regexp          = false
  self.options.locale          = get_system_locale || "en"
end

#set_search_path_for_queries(locale = "en") ⇒ Object



101
102
103
# File 'lib/capiru/cli.rb', line 101

def set_search_path_for_queries(locale="en")
  @search_path = File.join(MAN_ROOT, locale)
end