Class: Pdfmult::Optionparser

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfmult.rb

Overview

Parser for the command line options. The class method parse! does the job.

Class Method Summary collapse

Class Method Details

.parse!(argv) ⇒ Object

Parses the command line options from argv. (argv is cleared). Might print out help or version information.

argv - array with the command line options

Returns a hash containing the option parameters.

Raises:

  • (ArgumentError)


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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/pdfmult.rb', line 56

def self.parse!(argv)

  options = {
    :force   => false,
    :infile  => nil,
    :latex   => false,
    :number  => 2,
    :outfile => nil,
    :silent  => false,
    :pages   => nil
  }

  opt_parser = OptionParser.new do |opt|
    opt.banner = "Usage: #{PROGNAME} [options] file"
    opt.separator ''
    opt.separator 'pdfmult is a command line tool that'
    opt.separator 'rearranges multiple copies of a PDF page (shrunken) on one page.'
    opt.separator ''
    opt.separator 'The paper size of the produced PDF file is A4,'
    opt.separator 'the input file is also assumed to be in A4 format.'
    opt.separator 'The input PDF file may consist of several pages.'
    opt.separator 'If pdfmult succeeds in obtaining the page count it will rearrange all pages,'
    opt.separator 'if not, only the first page is processed'
    opt.separator '(unless the page count was specified via command line option).'
    opt.separator ''
    opt.separator 'pdfmult uses pdflatex with the pdfpages package,'
    opt.separator 'so both have to be installed on the system.'
    opt.separator 'If the --latex option is used, though, pdflatex is not run'
    opt.separator 'and a LaTeX file is created instead of a PDF.'
    opt.separator ''
    opt.separator 'Options'
    opt.separator ''

    # process --version and --help first,
    # exit successfully (GNU Coding Standards)
    opt.on_tail('-h', '--help', 'Print a brief help message and exit.') do
      puts opt_parser
      puts "\nReport bugs on the #{PROGNAME} home page: <#{HOMEPAGE}>"
      exit
    end

    opt.on_tail('-v', '--version',
                'Print a brief version information and exit.') do
      puts "#{PROGNAME} #{VERSION}"
      puts COPYRIGHT
      exit
    end

    opt.on('-n', '--number NUMBER', ['2', '4', '8', '9', '16'], Integer,
           'Number of copies to put on one page: 2 (default), 4, 8, 9, 16.') do |n|
      options[:number] = n
    end

    opt.on('-f', '--[no-]force', 'Do not prompt before overwriting.') do |f|
      options[:force] = f
    end

    opt.on('-l', '--latex', 'Create a LaTeX file instead of a PDF file (default: file_2.tex).') do
      options[:latex] = true
    end

    opt.on('-o', '--output FILE', String,
           'Output file (default: file_2.pdf). Use - to output to stdout.') do |f|
      options[:outfile] = f
    end

    opt.on('-p', '--pages NUMBER', Integer,
           'Number of pages to convert.',
           "If given, #{PROGNAME} does not try to obtain the page count from the source PDF.") do |p|
      raise(OptionParser::InvalidArgument, p)  unless p > 0
      options[:pages] = p
    end

    opt.on('-s', '--[no-]silent', 'Do not output progress information.') do |s|
      options[:silent] = s
    end

    opt.separator ''
  end
  opt_parser.parse!(argv)

  # only input file should be left in argv
  raise(ArgumentError, 'wrong number of arguments')  if (argv.size != 1 || argv[0].empty?)

  options[:infile] = argv.pop

  # set output file unless set by option
  ext = options[:latex] ? 'tex' : 'pdf'
  infile_without_ext = options[:infile].gsub(/(.pdf)\Z/, '')
  options[:outfile] ||= "#{infile_without_ext}_#{options[:number].to_s}.#{ext}"

  options
end