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.
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/glark/app/options.rb', line 40
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 = OptionSet.new optdata
end
|
Instance Attribute Details
#colors ⇒ Object
Returns the value of attribute colors.
35
36
37
|
# File 'lib/glark/app/options.rb', line 35
def colors
@colors
end
|
#fileset ⇒ Object
Returns the value of attribute fileset.
36
37
38
|
# File 'lib/glark/app/options.rb', line 36
def fileset
@fileset
end
|
#info_options ⇒ Object
Returns the value of attribute info_options.
37
38
39
|
# File 'lib/glark/app/options.rb', line 37
def info_options
@info_options
end
|
#output_options ⇒ Object
Returns the value of attribute output_options.
38
39
40
|
# File 'lib/glark/app/options.rb', line 38
def output_options
@output_options
end
|
Instance Method Details
#all_option_sets ⇒ Object
109
110
111
|
# File 'lib/glark/app/options.rb', line 109
def all_option_sets
[ @colors, @match_options, @output_options, @info_options, @input_options ]
end
|
#config_fields ⇒ Object
125
126
127
128
129
|
# File 'lib/glark/app/options.rb', line 125
def config_fields
{
"local-config-files" => @local_config_files,
}
end
|
#dump_all_fields ⇒ Object
197
198
199
200
201
202
203
204
205
206
207
208
|
# File 'lib/glark/app/options.rb', line 197
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
131
132
133
|
# File 'lib/glark/app/options.rb', line 131
def dump_fields
config_fields
end
|
#one_file? ⇒ Boolean
86
87
88
89
90
|
# File 'lib/glark/app/options.rb', line 86
def one_file?
return false if @fileset.size > 1
first = @fileset.files.first
first.to_s != '-' && first.file?
end
|
#read_environment_variable ⇒ Object
144
145
146
147
148
149
|
# File 'lib/glark/app/options.rb', line 144
def read_environment_variable
options = Env.split "GLARKOPTS"
while options.size > 0
@optset.process_option options
end
end
|
#read_expression ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/glark/app/options.rb', line 151
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
92
93
94
95
96
97
|
# File 'lib/glark/app/options.rb', line 92
def read_home_rcfile
return unless hdir = Dir.home
hdpn = Pathname.new hdir
homerc = hdpn + '.glarkrc'
read_rcfile homerc
end
|
#read_local_rcfiles ⇒ Object
99
100
101
102
103
104
105
106
107
|
# File 'lib/glark/app/options.rb', line 99
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
173
174
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/glark/app/options.rb', line 173
def read_options
@info_options.show_version if @args.size == 1 && @args.first == "-v"
@match_options.expr = nil
@optset.process(@args)
unless @match_options.expr
read_expression
end
end
|
#read_rcfile(rcfname) ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/glark/app/options.rb', line 113
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
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
|
# File 'lib/glark/app/options.rb', line 59
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
135
136
137
138
139
140
141
142
|
# File 'lib/glark/app/options.rb', line 135
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
211
212
213
|
# File 'lib/glark/app/options.rb', line 211
def validate!
@input_options.range.validate!
end
|
#write_configuration ⇒ Object
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/glark/app/options.rb', line 186
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
|