Class: Capiru::CLI
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
#environment ⇒ Object
Returns the value of attribute environment.
8
9
10
|
# File 'lib/capiru/cli.rb', line 8
def environment
@environment
end
|
#options ⇒ Object
Returns the value of attribute options.
8
9
10
|
# File 'lib/capiru/cli.rb', line 8
def options
@options
end
|
#parser ⇒ Object
Returns the value of attribute parser.
8
9
10
|
# File 'lib/capiru/cli.rb', line 8
def parser
@parser
end
|
#search_path ⇒ Object
Returns the value of attribute search_path.
8
9
10
|
# File 'lib/capiru/cli.rb', line 8
def search_path
@search_path
end
|
#status ⇒ Object
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_parser ⇒ Object
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]"
opts.on("-h", "--help", "Show usage information") do
puts opts
exit(EXIT_FAILURE)
end
opts.on("-q", "--query TERM", "Search the documentation for a specific term") do |term|
options.query = term
end
opts.on("-r", "--regexp REGEXP", "Seach the documentatino for terms matching REGEXP") do |regexp|
options.regexp = Regexp.new(regexp)
end
opts.on("-l", "--locale LOCALE", "Return documentation in the given locale") do |locale|
options.locale = locale
end
end
end
|
#get_system_locale ⇒ Object
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
10
11
12
13
|
# File 'lib/capiru/cli.rb', line 10
def has_man?
`which man`
$?.success?
end
|
#parse ⇒ Object
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
|
#run ⇒ Object
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
set_search_path_for_queries(options.locale)
begin
if options.query
match = run_single_query(options.query)
cmd = "man #{match}"
system(cmd)
self.status = $?
elsif options.regexp
else
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_options ⇒ Object
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
|