lib/hexapdf/cli/command.rb in hexapdf-0.41.0 vs lib/hexapdf/cli/command.rb in hexapdf-0.44.0

- old
+ new

@@ -51,11 +51,11 @@ class Command < CmdParse::Command module Extensions #:nodoc: def help_banner #:nodoc: "hexapdf #{HexaPDF::VERSION} - Versatile PDF Manipulation Tool\n" \ - "Copyright (c) 2014-2023 Thomas Leitner; licensed under the AGPLv3\n\n" \ + "Copyright (c) 2014-2024 Thomas Leitner; licensed under the AGPLv3\n\n" \ "#{format(usage, indent: 7)}\n\n" end def help #:nodoc: super << format("See https://hexapdf.gettalong.org/documentation/hexapdf.1.html " \ @@ -165,16 +165,16 @@ end doc.write(out_file, validate: false, incremental: incremental) end end - # Checks whether the given output file exists and raises an error if it does and - # HexaPDF::CLI#force is not set. + # Checks whether the given output file exists and ask whether to overwrite the output file if + # it does. If HexaPDF::CLI#force is set, a possibly existing output file is always overwritten. def maybe_raise_on_existing_file(filename) if !command_parser.force && File.exist?(filename) - raise Error, "Output file '#{filename}' already exists, not overwriting. Use --force to " \ - "force writing" + response = read_from_console("Output file '#{filename}' already exists - overwrite? (y/n)") + exit(1) unless response =~ /y/i end end # Defines the optimization options. # @@ -375,13 +375,13 @@ # # The optional argument +prompt+ can be used to customize the prompt when reading from the # console. def read_password(prompt = "Password") if $stdin.tty? - read_from_console(prompt) + read_from_console(prompt, noecho: true) else - ($stdin.gets || read_from_console(prompt)).chomp + ($stdin.gets || read_from_console(prompt, noecho: true)).chomp end end # Removes unused pages and page tree nodes from the document. def remove_unused_pages(doc) @@ -405,14 +405,17 @@ end private # Displays the given prompt, reads from the console without echo and returns the read string. - def read_from_console(prompt) + def read_from_console(prompt, noecho: false) IO.console.write("#{prompt}: ") - str = IO.console.noecho {|io| io.gets.chomp } - puts - str + if noecho + IO.console.noecho {|io| io.gets.chomp } + puts + else + IO.console.gets.chomp + end end end end