Class: Ned::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/ned.rb

Instance Method Summary collapse

Constructor Details

#initializeMain

Returns a new instance of Main.



42
43
44
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
71
72
73
74
# File 'lib/ned.rb', line 42

def initialize
  @options = { }

  @option_parser = OptionParser.new do |opts|
    opts.banner = "      Ned, a stream editor.\n\n      Usage: ned [[ned_arg ...] --] [[command] [: command ...]]\n\n      OPTIONS\n    EOF\n\n    opts.on('-i', '--inplace', 'Edit each file in place by processing each file independently.') do |inplace|\n      raise OptionParser::ParseError.new('duplicate flag') if @options[:inplace]\n\n      inplace\n    end\n\n    opts.on('-s', '--separate', 'Process input files one at a time rather than as one stream.') do |separate|\n      raise OptionParser::ParseError.new('duplicate flag') if @options[:separate]\n\n      separate\n    end\n\n    opts.on('-h', '--help [COMMAND]', 'Print this help.') do |help|\n      help || true\n    end\n\n    opts.on('-v', '--version', 'Print Ned version.') do |version|\n      version\n    end\n  end\nend\n"

Instance Method Details

#create_command(command_args, inputs, output, error) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/ned.rb', line 249

def create_command(command_args, inputs, output, error)
  previous = Ned::Read.new(inputs)
  command_args.each do |args|
    command = Ned::CommandRegistry.find(args.shift).new(previous)
    begin
      command.parse(args)
      raise OptionParser::InvalidArgument.new(args[0]) unless args.empty?
    rescue OptionParser::ParseError  => e
      error.puts "fatal: error while parsing options for #{command.class.long_name}: #{e.message}"
      error.puts
      error.puts command.class.help
      return 1
    end

    if command.options[:help]
      output.puts command.class.help
      return 0
    end

    if command.options[:version]
      output.puts "Ned version #{Ned::VERSION}"
      return 0
    end

    previous = command
  end

  [nil, previous]
end

#helpObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ned.rb', line 76

def help
  help = @option_parser.help

  help << "\nCOMMANDS\n"

  Ned::CommandRegistry.all.each do |command|
    help << "\n#{command.long_name} (#{command.short_name})\n\n"
    command.help.split("\n").each do |line|
      help << "  #{line}\n"
    end
  end
  help
end

#run(args, input, output, error) ⇒ Object



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