Class: PDFDirectPrint::CommandLine

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

Overview

Processes command line interactions from user

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommandLine

Returns a new instance of CommandLine.



8
9
10
# File 'lib/pdfdprint/command_line.rb', line 8

def initialize
  @options = { format: 'A4', port: 9100, resolution: 300, tray: 0 }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/pdfdprint/command_line.rb', line 6

def options
  @options
end

Instance Method Details

#parse_optionsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pdfdprint/command_line.rb', line 12

def parse_options
  OptionParser.new do |opts|
    opts.banner = 'Usage: pdfprint [options] [filename|directory]'
    opts.on('-f', '--format=FORMAT', 'Paper format (default: A4)') do |format|
      options[:format] = format
    end
    opts.on('-m', '--move=DIRECTORY', 'Move PDF file to DIRECTORY after printing') do |move|
      options[:move] = move
    end
    opts.on('-o', '--port=PORT', 'Printer TCP port (default: 9100)') do |port|
      options[:port] = port
    end
    opts.on('-p', '--printer=PRINTER', 'Printer hostname or IP address') do |printer|
      options[:printer] = printer
    end
    opts.on('-r', '--resolution=RESOLUTION', 'Print resolution in dpi (default: 300)') do |resolution|
      options[:resolution] = resolution
    end
    opts.on('-t', '--tray=TRAY', 'Paper tray number (default: 0)') do |tray|
      options[:tray] = tray
    end
  end.parse!
end

#process_directoryObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pdfdprint/command_line.rb', line 53

def process_directory
  puts "[INFO]: Processing directory #{ARGV[0]}"
  raw_printer = RawPrint.new(options)
  pdf_files_from_dir(ARGV[0]).each do |pdf_file|
    file_w_dir = File.join(ARGV[0], pdf_file)
    puts "[INFO]: Processing file #{pdf_file}"
    if raw_printer.print(file_w_dir)
      puts "[INFO]: Succesfully printed file #{pdf_file}"
      options.key?(:move) && move_file_to_dir(file_w_dir, options[:move])
    else
      puts "[ERROR]: Failed to print due to #{raw_printer.error_message}"
    end
    sleep 1
  end
end

#process_fileObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/pdfdprint/command_line.rb', line 69

def process_file
  puts "[INFO]: Processing file #{ARGV[0]}"
  pdf = RawPrint.new(options)
  if pdf.print(ARGV[0])
    puts "[INFO]: Succesfully printed file #{ARGV[0]}"
    options.key?(:move) && move_file_to_dir(ARGV[0], options[:move])
  else
    puts "[ERROR]: Failed to print due to #{pdf.error_message}"
  end
end

#validate_optionsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pdfdprint/command_line.rb', line 36

def validate_options
  if options.key?(:printer)
    puts "[INFO]: Using printer #{options[:printer]} on port #{options[:port]}"
  else
    puts 'Please specify a printer to print to using the -p option'
    exit 1
  end
  if ARGV[0].nil?
    puts 'Please specify a directory or PDF file'
    exit 1
  end
  return if File.exist?(ARGV[0])

  puts "[ERROR]: File or directory #{ARGV[0]} does not exist"
  exit 1
end