class Pdfmult::Optionparser
Parser for the command line options. The class method parse! does the job.
Public Class Methods
parse!(argv)
click to toggle source
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.
# File lib/pdfmult.rb, line 58 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 %q{ pdfmult is a command line tool that rearranges multiple copies of a PDF page (shrunken) on one page. The paper size of the produced PDF file is A4, the input file is also assumed to be in A4 format. The input PDF file may consist of several pages. If pdfmult succeeds in obtaining the page count it will rearrange all pages, if not, only the first page is processed (unless the page count was specified via command line option). pdfmult uses pdflatex with the pdfpages package, so both have to be installed on the system. If the --latex option is used, though, pdflatex is not run and a LaTeX file is created instead of a PDF. Options }.gsub(/^ +/, '') # 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