90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# File 'lib/ned.rb', line 90
def run(args, input, output, error)
args = args.map do |arg|
arg = arg.dup
Ned::Strings.unescape!(arg)
arg
end
ned_args = []
command_args = []
has_stdin = !input.tty?
if index = args.index('--')
ned_args.concat(args.shift(index))
args.shift
elsif args[0]&.start_with?('-')
ned_args = args
args = []
end
command_args.concat(args.shift(args.size))
begin
@option_parser.parse!(ned_args, into: @options)
rescue OptionParser::ParseError => e
error.puts "fatal: error while parsing options: #{e.message}"
return 1
end
if help_option = @options[:help]
if help_option.is_a? TrueClass
output.puts help
return 0
elsif help_option.is_a? String
command = Ned::CommandRegistry.find(help_option)
unless command
error.puts "fatal: \"#{help_option}\" is not a valid command"
return 1
end
output.puts command.help
return 0
end
end
if @options[:version]
output.puts "Ned version #{Ned::VERSION}"
return 0
end
inputs = ned_args.map do |current|
if current == '-'
input
else
unless File.exist? current
error.puts "fatal: file not found: \"#{current}\""
return 1
end
File.new(current)
end
end
inputs << input if inputs.empty?
inputs.unshift input if !inputs.index(input) && has_stdin
command_args = command_args.inject([[]]) do |array, arg|
if arg == ':'
array << []
else
array[-1] << arg
end
array
end
if command_args.size == 1 && command_args[0] == []
command_args.clear
end
if command_args.index([])
error.puts 'fatal: empty command found'
return 1
end
command_args.each do |array|
array.each_with_index do |arg, i|
array[i] = arg[1..-1] if arg == '\:'
end
end
command_args.each do |args|
unless Ned::CommandRegistry.find(args[0])
error.puts "fatal: \"#{args[0]}\" is not a valid command"
return 1
end
end
if inputs.count(input) > 1
error.puts 'fatal: can\'t read from stdin more than once'
return 1
end
if @options[:inplace] && inputs.index(input)
error.puts "fatal: option -i specified with stdin"
return 1
end
if @options[:inplace]
inputs.each do |i|
dup = command_args.map { |a| a.dup }
result, command = create_command(dup, [i], output, error)
return result if result
if inputs.index(input) && !has_stdin
error.puts 'fatal: can\'t read from stdin'
return 1
end
temp = Tempfile.new(File.expand_path(i))
print = Ned::Print.new(command, temp)
while line = print.execute; end
temp.rewind
IO.copy_stream(temp, File.expand_path(i))
temp.unlink
end
elsif @options[:separate]
inputs.each do |i|
dup = command_args.map { |a| a.dup }
result, command = create_command(dup, [i], output, error)
return result if result
if inputs.index(input) && !has_stdin
error.puts 'fatal: can\'t read from stdin'
return 1
end
print = Ned::Print.new(command, output)
while line = print.execute; end
end
else
result, command = create_command(command_args, inputs, output, error)
return result if result
if inputs.index(input) && !has_stdin
error.puts 'fatal: can\'t read from stdin'
return 1
end
print = Ned::Print.new(command, output)
while line = print.execute; end
end
0
end
|