class ArgParser

Public Class Methods

parse(args) click to toggle source

Returns a structure describing the options.

# File lib/argparser.rb, line 40
def self.parse(args)
  if args.empty?
    puts usage 
    exit true
  end
  # The options specified on the command line will be collected in
  # <b>options</b>.  No defaults. Most options are optional and do not
  # have to be set at all.
  # The others must be named for each transformation or be set in the
  # configuration-file.
  options = OpenStruct.new
  options.target = nil

  op = OptionParser.new do |opts|
    opts.banner = usage

    opts.on("-d", "--debug", 'Be verbose') do  
      $log_level = Logger::DEBUG
      @@log.level = $log_level
    end

    opts.on("-sOURCE", "--source=SOURCE", 'Source document (html)') do |so|
      options.source = so
    end

    opts.on("-oUT", "--out=GLOSSAR", 'Glossar-file (html)') do |ta|
      options.target = ta
    end

    opts.on("-tEMPLATE", "--template=TEMPLATE", 'Template (html)') do |tpl|
      options.template = tpl
    end

    opts.on("-cONFIG", "--config=CONFIG", 'Configuration-file') do |cfg|
      options.config = cfg
    end

    opts.on("-h", "--help", 'Show this message') do  
      puts opts
      exit true
    end

    opts.on("-v", "--version", 'Show program version') do  
      puts APPNAME.dup << ", version " << VERSION
      exit true
    end
  end
  begin
    op.parse!(args)
  rescue OptionParser::ParseError => er
    msg = "ERROR! Unsuitable or incomplete program-arguments" << ": %s" %er.message   
    puts msg
    puts "Start this program with parameter -h or --help to see the usage-message."
    exit false
  end
  @@log.debug('options are ' << options.to_s)

  options
end
usage() click to toggle source

Shows the usage-message

# File lib/argparser.rb, line 103
def self::usage
  msg = "\n\tUsage: html2index -s input.html [-o output.html] [-c config-file] [-t template.html] [-d]"
  msg << "\n\n\t* Will print to stdout, if the output-file is not provided."
  msg << "\n\t* Adapt ~/.config/HTML2Index/config to your needs.\n\n"
end