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.

Defined Under Namespace

Classes: ContentProcessor

Instance Method Summary collapse

Methods included from Command::Extensions

#help, #help_banner

Constructor Details

#initializeInspect

:nodoc:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/hexapdf/cli/inspect.rb', line 94

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:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/hexapdf/cli/inspect.rb', line 118

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