Class: Pdfh::OptParser

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

Overview

Handles Argument options

Constant Summary collapse

OPT_PARSER =
OptionParser.new do |opts|
  opts.default_argv
  # Process ARGV
  opts.banner = "Usage: #{opts.program_name} [options] [file1 ...]"
  opts.separator ""
  opts.separator "Specific options:"

  opts.on("-tID", "--type=ID", "Document type id (requires a trailing file list)")
  opts.on_tail("-T", "--list-types", "List document types in configuration") { list_types || exit }
  opts.on_tail("-V", "--version", "Show version") { version || exit }
  opts.on_tail("-h", "--help", "help (this dialog)") { help || exit }

  opts.on("-v", "--verbose", "Show more output. Useful for debug")
  opts.on("-d", "--dry", "Dry run, does not write new pdf")
end

Class Method Summary collapse

Class Method Details

.helpnil

Returns:

  • (nil)


45
46
47
# File 'lib/pdfh/utils/opt_parser.rb', line 45

def help
  puts OPT_PARSER
end

.list_typesnil

Returns:

  • (nil)


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pdfh/utils/opt_parser.rb', line 50

def list_types
  settings = SettingsBuilder.build
  ident = 2
  max_width = settings.document_types.map { |t| t.gid.size }.max
  puts "#{" " * ident}#{"ID".ljust(max_width)}  Type Name"
  puts "#{" " * ident}#{"" * max_width}  #{"" * 23}"
  settings.document_types.each do |type|
    puts "#{" " * ident}#{type.gid.ljust(max_width).yellow}  #{type.name}"
  end
  nil
end

.parse_argvHash

Returns:

  • (Hash)


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pdfh/utils/opt_parser.rb', line 26

def parse_argv
  Pdfh.instance_variable_set(:@console, Console.new(false))

  options = { dry: false, verbose: false }
  OPT_PARSER.parse!(into: options)
  options[:files] = ARGV if ARGV.any?
  options.transform_keys { |key| key.to_s.tr("-", "_").to_sym }
rescue OptionParser::InvalidOption => e
  Pdfh.error_print(e.message, exit_app: false)
  puts OPT_PARSER.help
  exit 1
end

.versionnil

Returns:

  • (nil)


40
41
42
# File 'lib/pdfh/utils/opt_parser.rb', line 40

def version
  puts "#{OPT_PARSER.program_name} v#{Pdfh::VERSION}"
end