Class: ArgParser

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

Constant Summary collapse

@@log =
init_logger()

Class Method Summary collapse

Methods included from Logging

init_logger, log_level=, log_target=

Methods included from File_Checking

#file_check, file_check

Methods included from Translating

language, trl, trl

Class Method Details

.parse(args) ⇒ Object

Returns a structure describing the options.



39
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
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
# File 'lib/argparser.rb', line 39

def self.parse(args)
  # 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

  op = OptionParser.new do |opts|
    opts.banner = "\n" << trl("Usage") << ":\t" << $APPNAME.dup << ' ' << trl("-s [SQLite-file] [options]") << "\n\t" << trl("or %s [Common options]") %($APPNAME) 

    opts.separator ""
    opts.separator trl("Specific options") << ':'

    opts.on('-' << trl("s"), '--' << trl("source [PATH]"), trl( "SQLite-file to read.")) do |source|
      options.source = source
    end

    opts.on('-' << trl('c'), '--' << trl("config [PATH]"), trl('Configuration file for this transformation')) do |config|
      options.config = config
    end

    opts.on('-' << trl('n'), '--' << trl("name [TABLE]"), trl('The name of the table from the SQLite-database to convert')) do |name|
      options.name = name
    end

    opts.on('-' << trl("t"), '--' << trl("target [PATH]"), trl( "Path to the dBase-file to be written.")) do |target|
      options.target = target
    end

    opts.on('-' << trl("l"), '--' << trl("list"), trl( "Show the list of available tables and exit")) do |target|
      options.list = true
    end

    opts.on('-' << trl('o'), '--' << trl("out [PATH]"), trl('Use the table-name as file-name for the DBF-result, store output in PATH')) do |path|
      options.name_like_table = true
      options.out = path
    end

    opts.on('--' << trl("time [list]"), trl( "Fields (table-columns) which shall be handled as timestamp values.")) do |list|
      options.time = list.gsub(/[,;]/, '').split
    end

    opts.on('--' << trl("date [list]"), trl( "Fields (table-columns) which shall be handled as date-time values.")) do |list|
      options.datetime = list.gsub(/[,;]/, '').split
    end

=begin

# This is for later. The evaluation of the file-content may be allocated to the
# Configuration singleton, as it does not mean a lot of I/O nor does it imply 
# much management.

opts.on("-c", "--config [CONFIG FILE]",
      "Read alternative configuration from this file") do |conf|
      $LOG.debug("config-file should be #{conf}") if $LOG
      options.config = File.expand_path(conf )
end
opts.on("-g", "--generic [CONFIG FILE]",
      "Write generic configuration options to this file") do |file|
      options.generic = File.expand_path(file )
end
=end
    opts.separator ""
    opts.separator trl("Common options") << ':'

    # No argument, shows at tail.  This will print an options summary.
    #
    opts.on_tail(trl("-d"), trl("--debug"), trl("Show debug-messages") ) do
      options.debug = true
      @@log.level = Logger::DEBUG
    end


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

    opts.on_tail(trl("-v"), trl("--version"), trl("Show version and program information") ) do
      puts "\t#{$APPNAME}"
      puts "\t© #{$YEARS}, #{$AUTHORS.join(', ')}"
      exit true
    end
  end
  begin
    op.parse!(args)
  rescue OptionParser::ParseError => er
    msg = trl("ERROR! Unsuitable or incomplete program-arguments") << (": %s" %er.message	)
    puts msg
    puts trl("Start this program with parameter -h or --help to see the usage-message.")
    exit false
  end

  options
end