Class: Tahweel::CLI::Options
- Inherits:
-
Object
- Object
- Tahweel::CLI::Options
- Defined in:
- lib/tahweel/cli/options.rb
Overview
Parses command-line arguments for the Tahweel CLI.
Constant Summary collapse
- POSITIVE_INTEGER =
/\A\+?[1-9]\d*(?:_\d+)*\z/
Class Method Summary collapse
-
.configure_parser(opts, options) ⇒ Object
Configures the OptionParser instance.
-
.default_options ⇒ Hash
Returns the default configuration options.
-
.parse(args) ⇒ Hash
Parses the command-line arguments.
-
.validate_args!(args, parser) ⇒ Object
Validates that arguments were provided.
Class Method Details
.configure_parser(opts, options) ⇒ Object
Configures the OptionParser instance.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/tahweel/cli/options.rb', line 54 def self.configure_parser(opts, ) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize opts.program_name = "tahweel" opts.version = Tahweel::VERSION opts.accept(POSITIVE_INTEGER) do |value| n = Integer(value) raise OptionParser::InvalidArgument, "must be a positive integer" if n < 1 n end opts.on( "-e", "--extensions EXTENSIONS", Array, "Comma-separated list of file extensions to process " \ "(default: #{Tahweel::CLI::FileCollector::SUPPORTED_EXTENSIONS.join(", ")})" ) do |value| [:extensions] = value end opts.on("--dpi DPI", POSITIVE_INTEGER, "DPI for PDF to Image conversion (default: #{[:dpi]})") do |value| [:dpi] = value end opts.on( "-p", "--processor PROCESSOR", Tahweel::Ocr::AVAILABLE_PROCESSORS, "OCR processor to use (default: google_drive). Available: #{Tahweel::Ocr::AVAILABLE_PROCESSORS.join(", ")}" ) do |value| [:processor] = value end opts.on( "-F", "--file-concurrency FILE_CONCURRENCY", POSITIVE_INTEGER, "Max concurrent files to process (default: CPUs - 2 = #{[:file_concurrency]})" ) do |value| [:file_concurrency] = value end opts.on( "-O", "--ocr-concurrency OCR_CONCURRENCY", POSITIVE_INTEGER, "Max concurrent OCR operations (default: #{[:ocr_concurrency]})" ) do |value| [:ocr_concurrency] = value end opts.on( "-f", "--formats FORMATS", Array, "Output formats (comma-separated, default: txt). Available: #{Tahweel::Writer::AVAILABLE_FORMATS.join(", ")}" ) do |value| [:formats] = value.map(&:to_sym) invalid_formats = [:formats] - Tahweel::Writer::AVAILABLE_FORMATS abort "Error: invalid format(s): #{invalid_formats.join(", ")}" if invalid_formats.any? end opts.on( "--page-separator SEPARATOR", String, "Separator between pages in TXT output (default: #{[:page_separator].gsub("\n", "\\n")})" ) do |value| [:page_separator] = value.gsub("\\n", "\n") end opts.on("-o", "--output DIR", String, "Output directory (default: current directory)") do |value| [:output] = value end end |
.default_options ⇒ Hash
Returns the default configuration options.
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tahweel/cli/options.rb', line 38 def self. { dpi: 150, processor: :google_drive, ocr_concurrency: Tahweel::Converter::DEFAULT_CONCURRENCY, file_concurrency: (Etc.nprocessors - 2).clamp(2..), output: nil, formats: %i[txt docx], page_separator: Tahweel::Writers::Txt::PAGE_SEPARATOR } end |
.parse(args) ⇒ Hash
Parses the command-line arguments.
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/tahweel/cli/options.rb', line 23 def self.parse(args) = parser = OptionParser.new { configure_parser(_1, ) } begin parser.parse!(args) rescue OptionParser::ParseError => e abort "Error: #{e.}" end validate_args!(args, parser) end |
.validate_args!(args, parser) ⇒ Object
Validates that arguments were provided.
124 125 126 127 128 129 |
# File 'lib/tahweel/cli/options.rb', line 124 def self.validate_args!(args, parser) return unless args.empty? puts parser exit 1 end |