Parse command line

filename = nil
lines = 0               # Default means no truncating

loop do
  begin
    opt, arg = gets
    break if not opt
    case opt
      when '-h'
        puts 'Usage: ...'
        break           # Stop processing if -h
      when '-f'
        filename = arg  # Save the file argument
      when '-l'
        if arg != ''
          lines = arg   # Save lines arg (if given)
        else
          lines = 100   # Default for truncating
        end
    end

  rescue => err
    puts err
    break
  end
end

puts 'filename = #{filename}'
puts 'lines    = #{lines}'

发表评论

电子邮件地址不会被公开。 必填项已用*标注