Class: Pdfh::OptParser

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfh/utils/opt_parser.rb

Overview

Handles Argument options

Instance Method Summary collapse

Constructor Details

#initialize(argv:, console: nil) ⇒ self

Parameters:

  • argv (Array<String>)

    command line arguments (ie. ARGV)

  • console (Pdfh::Console, nil) (defaults to: nil)


11
12
13
14
15
16
17
18
19
20
# File 'lib/pdfh/utils/opt_parser.rb', line 11

def initialize(argv:, console: nil)
  @argv = argv
  @console = console || Console.new(false)
  @options = {
    verbose: false,
    dry: false,
    type: nil,
    files: []
  }
end

Instance Method Details

#parse_argvHash

Returns Parsed options including flags and file arguments.

Returns:

  • (Hash)

    Parsed options including flags and file arguments



23
24
25
26
27
28
29
30
31
32
# File 'lib/pdfh/utils/opt_parser.rb', line 23

def parse_argv
  option_parser = build_option_parser
  non_option_args = option_parser.parse!(@argv)
  @options[:files] = non_option_args
  @options.transform_keys { |key| key.to_s.tr("-", "_").to_sym }
rescue OptionParser::InvalidOption => e
  @console.error_print(e.message, exit_app: false)
  puts option_parser.help
  exit 1
end