Class: ArgParser

Inherits:
Object
  • Object
show all
Extended by:
Logging
Defined in:
lib/argparser.rb

Constant Summary collapse

@@log =

self.extend(Translating)

init_logger()

Class Method Summary collapse

Methods included from Logging

debout, formatter, init_logger, log_level=, log_level?, log_target=

Class Method Details

.parse(args) ⇒ Object

Returns a structure describing the options.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
# 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

.usageObject

Shows the usage-message



103
104
105
106
107
# 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