Class: PDFDirectPrint::CommandLine
- Inherits:
-
Object
- Object
- PDFDirectPrint::CommandLine
- Defined in:
- lib/pdfdprint/command_line.rb
Overview
Processes command line interactions from user
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize ⇒ CommandLine
constructor
A new instance of CommandLine.
- #parse_options ⇒ Object
- #process_directory ⇒ Object
- #process_file ⇒ Object
- #validate_options ⇒ Object
Constructor Details
#initialize ⇒ CommandLine
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
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/pdfdprint/command_line.rb', line 6 def @options end |
Instance Method Details
#parse_options ⇒ Object
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 OptionParser.new do |opts| opts. = 'Usage: pdfprint [options] [filename|directory]' opts.on('-f', '--format=FORMAT', 'Paper format (default: A4)') do |format| [:format] = format end opts.on('-m', '--move=DIRECTORY', 'Move PDF file to DIRECTORY after printing') do |move| [:move] = move end opts.on('-o', '--port=PORT', 'Printer TCP port (default: 9100)') do |port| [:port] = port end opts.on('-p', '--printer=PRINTER', 'Printer hostname or IP address') do |printer| [:printer] = printer end opts.on('-r', '--resolution=RESOLUTION', 'Print resolution in dpi (default: 300)') do |resolution| [:resolution] = resolution end opts.on('-t', '--tray=TRAY', 'Paper tray number (default: 0)') do |tray| [:tray] = tray end end.parse! end |
#process_directory ⇒ Object
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() 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}" .key?(:move) && move_file_to_dir(file_w_dir, [:move]) else puts "[ERROR]: Failed to print due to #{raw_printer.}" end sleep 1 end end |
#process_file ⇒ Object
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() if pdf.print(ARGV[0]) puts "[INFO]: Succesfully printed file #{ARGV[0]}" .key?(:move) && move_file_to_dir(ARGV[0], [:move]) else puts "[ERROR]: Failed to print due to #{pdf.}" end end |
#validate_options ⇒ Object
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 if .key?(:printer) puts "[INFO]: Using printer #{[:printer]} on port #{[: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 |