Class: HexaPDF::CLI::Inspect

Inherits:
Command
  • Object
show all
Defined in:
lib/hexapdf/cli/inspect.rb

Overview

Shows the internal structure of a PDF file.

Instance Method Summary collapse

Methods included from Command::Extensions

#help_banner

Constructor Details

#initializeInspect

:nodoc:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hexapdf/cli/inspect.rb', line 45

def initialize #:nodoc:
  super('inspect', takes_commands: false)
  short_desc("Dig into the internal structure of a PDF file")
  long_desc(<<~EOF)
    Inspects a PDF file for debugging or testing purposes. This command is useful when one
    needs to inspect the internal object structure or a stream of a PDF file. A PDF object is
    always shown in the PDF syntax.

    If no arguments are given, the interactive mode is started. Otherwise the arguments are
    interpreted as interactive mode commands and executed. It is possible to specify more than
    one command in this way by separating them with semicolons, or whitespace in case the
    number of command arguments is fixed.

  EOF

  options.on("--password PASSWORD", "-p", String,
             "The password for decryption. Use - for reading from standard input.") do |pwd|
    @password = (pwd == '-' ? read_password : pwd)
  end

  @password = nil
  @serializer = HexaPDF::Serializer.new
end

Instance Method Details

#execute(file, *commands) ⇒ Object

:nodoc:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/hexapdf/cli/inspect.rb', line 69

def execute(file, *commands) #:nodoc:
  with_document(file, password: @password) do |doc|
    @doc = doc
    if commands.empty?
      begin
        require 'reline'
        Reline.completion_proc = RELINE_COMPLETION_PROC
        Reline.completion_append_character = " "
      rescue LoadError
        if command_parser.verbosity_info?
          $stderr.puts("Library reline not available, history and line editing not available")
        end
      end
      while true
        input = read_input
        (puts; break) unless input
        commands = input.scan(/(["'])(.+?)\1|(\S+)/).map {|a| a[1] || a[2] }
        break if execute_commands(commands)
      end
    else
      execute_commands(commands)
    end
  end
end