Class: Glark::AppOptions
Constant Summary
Constants included
from OptionUtil
OptionUtil::NONE
Instance Attribute Summary collapse
Attributes inherited from AppSpec
#input_spec, #local_config_files, #match_spec, #output_spec
Instance Method Summary
collapse
Methods included from OptionUtil
#add_opt, #add_opt_arg, #add_opt_blk, #add_opt_false, #add_opt_int, #add_opt_str, #add_opt_true, #colorize, #set, #set_var, #to_boolean
Constructor Details
Returns a new instance of AppOptions.
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/glark/app/options.rb', line 26
def initialize
optdata = Array.new
@colors = ColorOptions.new
@input_options = InputOptions.new optdata
@match_options = MatchOptions.new @colors, optdata
@output_options = OutputOptions.new @colors, optdata
@info_options = InfoOptions.new @colors, optdata
add_opt_blk(optdata, %w{ --config }) { write_configuration; exit }
add_opt_blk(optdata, %w{ --dump }) { dump_all_fields; exit 0 }
super @input_options, @match_options, @output_options
@optset = OptProc::OptionSet.new optdata
end
|
Instance Attribute Details
#colors ⇒ Object
Returns the value of attribute colors.
21
22
23
|
# File 'lib/glark/app/options.rb', line 21
def colors
@colors
end
|
#fileset ⇒ Object
Returns the value of attribute fileset.
22
23
24
|
# File 'lib/glark/app/options.rb', line 22
def fileset
@fileset
end
|
#info_options ⇒ Object
Returns the value of attribute info_options.
23
24
25
|
# File 'lib/glark/app/options.rb', line 23
def info_options
@info_options
end
|
#output_options ⇒ Object
Returns the value of attribute output_options.
24
25
26
|
# File 'lib/glark/app/options.rb', line 24
def output_options
@output_options
end
|
Instance Method Details
#all_option_sets ⇒ Object
95
96
97
|
# File 'lib/glark/app/options.rb', line 95
def all_option_sets
[ @colors, @match_options, @output_options, @info_options, @input_options ]
end
|
#config_fields ⇒ Object
111
112
113
114
115
|
# File 'lib/glark/app/options.rb', line 111
def config_fields
{
"local-config-files" => @local_config_files,
}
end
|
#dump_all_fields ⇒ Object
183
184
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/glark/app/options.rb', line 183
def dump_all_fields
fields = dump_fields
all_option_sets.each do |opts|
fields.merge! opts.dump_fields
end
len = fields.keys.collect { |f| f.length }.max
fields.keys.sort.each do |field|
printf "%*s : %s\n", len, field, fields[field]
end
end
|
#dump_fields ⇒ Object
117
118
119
|
# File 'lib/glark/app/options.rb', line 117
def dump_fields
config_fields
end
|
#one_file? ⇒ Boolean
72
73
74
75
76
|
# File 'lib/glark/app/options.rb', line 72
def one_file?
return false if @fileset.size > 1
first = @fileset.files.first
first.to_s != '-' && first.file?
end
|
#read_environment_variable ⇒ Object
130
131
132
133
134
135
|
# File 'lib/glark/app/options.rb', line 130
def read_environment_variable
options = Env.split "GLARKOPTS"
while options.size > 0
@optset.process_option options
end
end
|
#read_expression ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/glark/app/options.rb', line 137
def read_expression
if @args.size > 0
known_end = false
if @args[0] == "--"
@args.shift
known_end = true
end
if @args && @args.size > 0
return @match_options.read_expression @args, !known_end
end
end
if @args.size > 0
raise "No expression provided."
end
$stderr.puts "Usage: glark [options] expression file..."
$stderr.puts "Try `glark --help' for more information."
exit 1
end
|
#read_home_rcfile ⇒ Object
78
79
80
81
82
83
|
# File 'lib/glark/app/options.rb', line 78
def read_home_rcfile
return unless hdir = Dir.home
hdpn = Pathname.new hdir
homerc = hdpn + '.glarkrc'
read_rcfile homerc
end
|
#read_local_rcfiles ⇒ Object
85
86
87
88
89
90
91
92
93
|
# File 'lib/glark/app/options.rb', line 85
def read_local_rcfiles
hdir = Dir.home
dir = Pathname.new('.').expand_path
while !dir.root? && dir != hdir
rcfile = dir + '.glarkrc'
return if read_rcfile rcfile
dir = dir.dirname
end
end
|
#read_options ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/glark/app/options.rb', line 159
def read_options
@info_options.show_version if @args.size == 1 && @args.first == "-v"
@match_options.expr = nil
nil while @args.size > 0 && @optset.process_option(@args)
unless @match_options.expr
read_expression
end
end
|
#read_rcfile(rcfname) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/glark/app/options.rb', line 99
def read_rcfile rcfname
return nil unless rcfname.exist?
rcfile = RCFile.new rcfname
rcvalues = rcfile.names.collect { |name| [ name, rcfile.values(name) ] }
all_option_sets.each do |opts|
opts.update_fields rcvalues
end
update_fields rcvalues
true
end
|
#run(args) ⇒ Object
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
|
# File 'lib/glark/app/options.rb', line 45
def run args
@args = args
read_home_rcfile
if @local_config_files
read_local_rcfiles
end
read_environment_variable
if ENV["EMACS"]
@output_options.style = "grep"
end
read_options
validate!
@fileset = @input_options.create_fileset @args
if @output_options.show_file_names.nil?
@output_options.show_file_names = @output_options.label || !one_file?
end
end
|
#update_fields(fields) ⇒ Object
121
122
123
124
125
126
127
128
|
# File 'lib/glark/app/options.rb', line 121
def update_fields fields
fields.each do |name, values|
case name
when "local-config-files"
@local_config_files = to_boolean values.last
end
end
end
|
#validate! ⇒ Object
check options for collisions/data validity
197
198
199
|
# File 'lib/glark/app/options.rb', line 197
def validate!
@input_options.range.validate!
end
|
#write_configuration ⇒ Object
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/glark/app/options.rb', line 172
def write_configuration
fields = config_fields
all_option_sets.each do |opts|
fields.merge! opts.config_fields
end
fields.keys.sort.each do |fname|
puts "#{fname}: #{fields[fname]}"
end
end
|